本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("OC Buy/Sell Signals", shorttitle="OC Signals", overlay=true)
// ── Inputs ────────────────────────────────────────────────
nonRepaint = input.bool(true, "Non-repaint (confirm signals on bar close)", tooltip="ON: signals appear only after the bar closes and never disappear or move. OFF: signals can appear while the bar is still forming and may vanish or flip before the bar closes (repainting). Alerts and the info box statistics are more reliable with this ON.")
grpSig = "Signals"
bodyLen = input.int(50, "Average body length", minval=1, group=grpSig, tooltip="Number of bars used to calculate the average candle body (absolute distance between open and close). The signal candle's body is compared against this average. No signals can appear until this many bars of history exist.")
bodyMult = input.float(3.0, "Min body vs average (x)", minval=0.0, step=0.5, group=grpSig, tooltip="Minimum size of the signal candle's body as a multiple of the average body. 3.0 means the body must be at least 3 times the average. Higher values give fewer, stronger signals. Set to 0 to remove the size filter.")
buyCol = input.color(#4CAF50, "Buy color", group=grpSig, tooltip="Color of the BUY label. A BUY fires when a bullish candle closes above the previous candle's body high and passes the body size filter.")
sellCol = input.color(#F23645, "Sell color", group=grpSig, tooltip="Color of the SELL label. A SELL fires when a bearish candle closes below the previous candle's body low and passes the body size filter.")
showLabels = input.bool(true, "Show BUY/SELL labels", group=grpSig, tooltip="Shows or hides the BUY and SELL labels. Alerts and the info box are not affected.")
grpLB = "Lookback P/L"
lookback = input.int(20, "Lookback (last N closed trades)", minval=1, group=grpLB, tooltip="Number of most recent closed trades included in the info box statistics. Older trades drop out as new ones close. Each signal closes the open trade and opens the opposite one (stop-and-reverse), using the signal bar's close as the price.")
showTable = input.bool(true, "Show info box", group=grpLB, tooltip="Turns the lookback P/L info box on or off.")
tablePos = input.string("Top Right", "Info box position", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], group=grpLB, tooltip="Chart location of the info box.")
tableSize = input.string("Small", "Info box size", options=["Tiny", "Small", "Normal", "Large", "Huge"], group=grpLB, tooltip="Text size used inside the info box.")
tPos = switch tablePos
"Top Left" => position.top_left
"Top Center" => position.top_center
"Middle Left" => position.middle_left
"Middle Center" => position.middle_center
"Middle Right" => position.middle_right
"Bottom Left" => position.bottom_left
"Bottom Center" => position.bottom_center
"Bottom Right" => position.bottom_right
=> position.top_right
tSize = switch tableSize
"Tiny" => size.tiny
"Normal" => size.normal
"Large" => size.large
"Huge" => size.huge
=> size.small
// ── Open/Close-only logic ─────────────────────────────────
bodyHi = math.max(open, close)
bodyLo = math.min(open, close)
body = math.abs(close - open)
avgBody = ta.sma(body, bodyLen)
// Bullish candle closing above the prior body, with a body at least the required size
bullBreak = close > open and close > bodyHi[1] and body >= avgBody * bodyMult
// Bearish candle closing below the prior body, with a body at least the required size
bearBreak = close < open and close < bodyLo[1] and body >= avgBody * bodyMult
// Signals alternate (no repeated buys or sells); with non-repaint ON they fire on confirmed bars only
var int dir = 0
buy = (not nonRepaint or barstate.isconfirmed) and bullBreak and dir != 1
sell = (not nonRepaint or barstate.isconfirmed) and bearBreak and dir != -1
// ── Trade tracking (stop-and-reverse, entry/exit at signal bar close) ──
var float entryPx = na
var array<float> pnls = array.new_float(0)
if buy or sell
if dir != 0
float tradePnl = dir == 1 ? (close - entryPx) / entryPx * 100 : (entryPx - close) / entryPx * 100
array.push(pnls, tradePnl)
while array.size(pnls) > lookback
array.shift(pnls)
dir := buy ? 1 : -1
entryPx := close
// ── Plots ─────────────────────────────────────────────────
plotshape(showLabels and buy, "Buy", shape.labelup, location.belowbar, buyCol, text="BUY", textcolor=color.white, size=size.small)
plotshape(showLabels and sell, "Sell", shape.labeldown, location.abovebar, sellCol, text="SELL", textcolor=color.white, size=size.small)
// ── Info box ──────────────────────────────────────────────
fmt(float x) =>
na(x) ? "n/a" : (x >= 0 ? "+" : "") + str.tostring(x, "0.00") + "%"
posCol(float x) =>
na(x) ? color.white : x >= 0 ? color.new(#4CAF50, 0) : color.new(#F23645, 0)
var table infoBox = table.new(tPos, 2, 11, bgcolor=color.new(#131722, 10), frame_color=color.new(color.gray, 40), frame_width=1, border_color=color.new(color.gray, 60), border_width=1)
if barstate.isfirst
table.merge_cells(infoBox, 0, 0, 1, 0)
table.merge_cells(infoBox, 0, 4, 1, 4)
if showTable and barstate.islast
int n = array.size(pnls)
int winCnt = 0
int lossCnt = 0
float winSum = 0.0
float lossSum = 0.0
float eq = 1.0
float peak = 1.0
float maxDD = 0.0
if n > 0
for i = 0 to n - 1
float p = array.get(pnls, i)
if p > 0
winCnt += 1
winSum += p
else if p < 0
lossCnt += 1
lossSum += p
eq := eq * (1 + p / 100)
peak := math.max(peak, eq)
maxDD := math.min(maxDD, (eq / peak - 1) * 100)
float netPnl = n > 0 ? (eq - 1) * 100 : na
float winRate = n > 0 ? winCnt / n * 100 : na
float avgWin = winCnt > 0 ? winSum / winCnt : na
float avgLoss = lossCnt > 0 ? lossSum / lossCnt : na
float openPnl = dir == 1 ? (close - entryPx) / entryPx * 100 : dir == -1 ? (entryPx - close) / entryPx * 100 : na
table.cell(infoBox, 0, 0, "OC BUY/SELL SIGNALS", text_color=color.white, bgcolor=color.new(#3B5B92, 0), text_halign=text.align_center, text_size=tSize)
table.cell(infoBox, 0, 1, "Non-Repaint", text_color=color.white, text_halign=text.align_left, text_size=tSize)
table.cell(infoBox, 1, 1, nonRepaint ? "ON" : "OFF", text_color=nonRepaint ? color.new(#4CAF50, 0) : color.new(#F23645, 0), text_halign=text.align_right, text_size=tSize)
table.cell(infoBox, 0, 2, "Avg Body", text_color=color.white, text_halign=text.align_left, text_size=tSize)
table.cell(infoBox, 1, 2, str.tostring(bodyLen), text_color=color.white, text_halign=text.align_right, text_size=tSize)
table.cell(infoBox, 0, 3, "Min Body", text_color=color.white, text_halign=text.align_left, text_size=tSize)
table.cell(infoBox, 1, 3, str.tostring(bodyMult, "0.0") + "x", text_color=color.white, text_halign=text.align_right, text_size=tSize)
table.cell(infoBox, 0, 4, "LOOKBACK P/L", text_color=color.white, bgcolor=color.new(#3B5B92, 0), text_halign=text.align_center, text_size=tSize)
table.cell(infoBox, 0, 5, "Trades Counted", text_color=color.white, text_halign=text.align_left, text_size=tSize)
table.cell(infoBox, 1, 5, str.tostring(n) + " / " + str.tostring(lookback), text_color=color.white, text_halign=text.align_right, text_size=tSize)
table.cell(infoBox, 0, 6, "Net P/L", text_color=color.white, text_halign=text.align_left, text_size=tSize)
table.cell(infoBox, 1, 6, fmt(netPnl), text_color=posCol(netPnl), text_halign=text.align_right, text_size=tSize)
table.cell(infoBox, 0, 7, "Win Rate", text_color=color.white, text_halign=text.align_left, text_size=tSize)
table.cell(infoBox, 1, 7, na(winRate) ? "n/a" : str.tostring(winRate, "0") + "% (" + str.tostring(winCnt) + "W/" + str.tostring(lossCnt) + "L)", text_color=color.white, text_halign=text.align_right, text_size=tSize)
table.cell(infoBox, 0, 8, "Avg Win / Loss", text_color=color.white, text_halign=text.align_left, text_size=tSize)
table.cell(infoBox, 1, 8, fmt(avgWin) + " / " + fmt(avgLoss), text_color=color.white, text_halign=text.align_right, text_size=tSize)
table.cell(infoBox, 0, 9, "Max Drawdown", text_color=color.white, text_halign=text.align_left, text_size=tSize)
table.cell(infoBox, 1, 9, n > 0 ? fmt(maxDD) : "n/a", text_color=maxDD < 0 ? color.new(#F23645, 0) : color.white, text_halign=text.align_right, text_size=tSize)
table.cell(infoBox, 0, 10, "Open Trade P/L", text_color=color.white, text_halign=text.align_left, text_size=tSize)
table.cell(infoBox, 1, 10, fmt(openPnl), text_color=posCol(openPnl), text_halign=text.align_right, text_size=tSize)
// ── Alerts ────────────────────────────────────────────────
alertcondition(buy, "Buy Signal", "BUY: close broke above the prior body")
alertcondition(sell, "Sell Signal", "SELL: close broke below the prior body")