本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// Opening Candle Breakout (OCB)
// Direction of the first session candle, stop at its opposite side, targets in R multiples.
// Inspired by Zarattini & Aziz (2023), "Can Day Trading Really Be Profitable?".
// © Swissy 2026 — Mozilla Public License 2.0
//@version=6
indicator("Opening Candle Breakout [OCB]", shorttitle = "OCB", overlay = true, max_lines_count = 200, max_labels_count = 100)
// ─── Inputs ────────────────────────────────────────────────────────────────
string sessFirst = input.session("0930-0935", "First session candle", tooltip = "Must match exactly one chart candle (e.g. 0930-0935 on a 5-minute chart).")
string sessEnd = input.session("1555-1600", "Session close")
string sessDay = input.session("0930-1600", "Trading session (line extension)")
string tz = input.string("America/New_York", "Timezone of the session")
float minBodyATR = input.float(0.5, "Minimum candle body (x ATR)", minval = 0, step = 0.05, tooltip = "0 = no filter. 0.5 = skip candles without a clear direction.")
int atrLen = input.int(14, "ATR period (bars)", minval = 5)
float minStopATR = input.float(0.10, "Minimum stop (x ATR)", minval = 0.01, step = 0.05)
float maxStopATR = input.float(2.0, "Maximum stop (x ATR)", minval = 0.25, step = 0.25)
float tp1R = input.float(2.0, "Target 1 (R)", minval = 0.5, step = 0.5, inline = "tp")
float tp2R = input.float(5.0, "Target 2 (R)", minval = 0.5, step = 0.5, inline = "tp")
float tp3R = input.float(10.0, "Target 3 (R)", minval = 0.5, step = 0.5, inline = "tp")
int keepDays = input.int(5, "Days to display", minval = 1, maxval = 30)
bool showLabels = input.bool(true, "Show labels")
color colLong = input.color(#009988, "Long", inline = "c")
color colShort = input.color(#CC3311, "Short", inline = "c")
color colStop = input.color(#FF0066, "Stop", inline = "c")
color colTarget = input.color(#00C853, "Targets", inline = "c")
// ─── Logic ─────────────────────────────────────────────────────────────────
bool isFirst = not na(time(timeframe.period, sessFirst, tz))
bool isEnd = not na(time(timeframe.period, sessEnd, tz))
bool inSession = not na(time(timeframe.period, sessDay, tz))
float atr = ta.atr(atrLen)
float body = math.abs(close - open)
bool bodyOk = minBodyATR <= 0 or body >= atr * minBodyATR
bool sigLong = isFirst and close > open and bodyOk
bool sigShort = isFirst and close < open and bodyOk
bool sigSkip = isFirst and not (sigLong or sigShort)
float rawRisk = sigLong ? close - low : sigShort ? high - close : na
float risk = math.min(math.max(rawRisk, atr * minStopATR), atr * maxStopATR)
int dir = sigLong ? 1 : sigShort ? -1 : 0
float entryLv = close
float stopLv = entryLv - dir * risk
float t1 = entryLv + dir * risk * tp1R
float t2 = entryLv + dir * risk * tp2R
float t3 = entryLv + dir * risk * tp3R
// ─── Drawing ───────────────────────────────────────────────────────────────
var line[] lines = array.new<line>()
var label[] labels = array.new<label>()
var int drawDay = -1
f_line(float y, color c, int w, string sty) =>
line.new(bar_index, y, bar_index + 1, y, color = c, width = w, style = sty)
f_cleanup() =>
// 5 lines and 5 labels per day
while array.size(lines) > keepDays * 5
line.delete(array.shift(lines))
while array.size(labels) > keepDays * 5
label.delete(array.shift(labels))
if dir != 0
color c = dir == 1 ? colLong : colShort
drawDay := dayofmonth(time, tz)
array.push(lines, f_line(entryLv, c, 2, line.style_solid))
array.push(lines, f_line(stopLv, colStop, 2, line.style_solid))
array.push(lines, f_line(t1, colTarget, 1, line.style_dashed))
array.push(lines, f_line(t2, colTarget, 1, line.style_dashed))
array.push(lines, f_line(t3, colTarget, 1, line.style_dashed))
if showLabels
array.push(labels, label.new(bar_index, entryLv, dir == 1 ? "LONG" : "SHORT", style = dir == 1 ? label.style_label_up : label.style_label_down, color = c, textcolor = color.white, size = size.small))
array.push(labels, label.new(bar_index, stopLv, "SL -1R", style = label.style_label_left, color = color.new(colStop, 20), textcolor = color.white, size = size.tiny))
array.push(labels, label.new(bar_index, t1, "T1 " + str.tostring(tp1R) + "R", style = label.style_label_left, color = color.new(colTarget, 40), textcolor = color.white, size = size.tiny))
array.push(labels, label.new(bar_index, t2, "T2 " + str.tostring(tp2R) + "R", style = label.style_label_left, color = color.new(colTarget, 40), textcolor = color.white, size = size.tiny))
array.push(labels, label.new(bar_index, t3, "T3 " + str.tostring(tp3R) + "R", style = label.style_label_left, color = color.new(colTarget, 40), textcolor = color.white, size = size.tiny))
f_cleanup()
/// Extend today's lines while today's session is open, and keep level labels at the right end
if array.size(lines) > 0 and inSession and dayofmonth(time, tz) == drawDay
int n = array.size(lines)
int start = math.max(0, n - 5)
for i = start to n - 1
line.set_x2(array.get(lines, i), bar_index)
if showLabels and array.size(labels) >= 5
int m = array.size(labels)
for j = m - 4 to m - 1
label.set_x(array.get(labels, j), bar_index + 1)
plotshape(sigLong, "Long setup", shape.triangleup, location.belowbar, color = colLong, size = size.small)
plotshape(sigShort, "Short setup", shape.triangledown, location.abovebar, color = colShort, size = size.small)
plotshape(sigSkip, "No setup", shape.xcross, location.abovebar, color = color.gray, size = size.tiny)
// ─── Alerts ────────────────────────────────────────────────────────────────
alertcondition(sigLong, "OCB Long", "OCB LONG {{ticker}} entry {{close}}")
alertcondition(sigShort, "OCB Short", "OCB SHORT {{ticker}} entry {{close}}")
alertcondition(sigSkip, "OCB No setup", "OCB no setup today on {{ticker}}")
alertcondition(isEnd, "OCB Close", "OCB session close {{ticker}}")
// ─── Statistics (estimate on chart candles, not a backtest) ────────────────
bool showStats = input.bool(true, "Show statistics", group = "Statistics")
float statTP = input.float(10.0, "Stats exit: target (R), otherwise session close", minval = 0.5, step = 0.5, group = "Statistics")
var int sDir = 0
var float sEntry = na
var float sStop = na
var float sRisk = na
var bool sPending = false
var int pDir = 0
var float pStop = na
var int sTrades = 0
var int sWins = 0
var float sSumR = 0.0
var float sGW = 0.0
var float sGL = 0.0
// entry at the open of the candle following the signal
if sPending
sDir := pDir
sEntry := open
sStop := pStop
sRisk := math.abs(open - pStop)
sPending := false
// simulated trade management
float rOut = na
if sDir != 0 and sRisk > 0
float tgt = sEntry + sDir * sRisk * statTP
bool hitSL = sDir == 1 ? low <= sStop : high >= sStop
bool hitTP = sDir == 1 ? high >= tgt : low <= tgt
if hitSL
rOut := -1.0
else if hitTP
rOut := statTP
else if isEnd
rOut := sDir * (close - sEntry) / sRisk
if not na(rOut)
sTrades += 1
sSumR += rOut
if rOut > 0
sWins += 1
sGW += rOut
else
sGL += math.abs(rOut)
sDir := 0
// new signal (one per day)
if dir != 0 and sDir == 0
sPending := true
pDir := dir
pStop := stopLv
var table st = table.new(position.top_right, 2, 7, bgcolor = color.new(color.black, 25), border_width = 1, border_color = color.new(color.gray, 70))
if showStats and barstate.islast
float wr = sTrades > 0 ? 100.0 * sWins / sTrades : 0.0
float pf = sGL > 0 ? sGW / sGL : 0.0
float avg = sTrades > 0 ? sSumR / sTrades : 0.0
table.cell(st, 0, 0, "OCB · estimate", text_color = color.white, text_size = size.small, bgcolor = color.new(color.blue, 60))
table.cell(st, 1, 0, "not a backtest", text_color = color.white, text_size = size.tiny, bgcolor = color.new(color.blue, 60))
table.cell(st, 0, 1, "Trades", text_color = color.white, text_size = size.small)
table.cell(st, 1, 1, str.tostring(sTrades), text_color = color.white, text_size = size.small)
table.cell(st, 0, 2, "Win rate", text_color = color.white, text_size = size.small)
table.cell(st, 1, 2, str.tostring(wr, "0.0") + " %", text_color = color.white, text_size = size.small)
table.cell(st, 0, 3, "Net R", text_color = color.white, text_size = size.small)
table.cell(st, 1, 3, str.tostring(sSumR, "0.0"), text_color = color.white, text_size = size.small)
table.cell(st, 0, 4, "Profit factor", text_color = color.white, text_size = size.small)
table.cell(st, 1, 4, str.tostring(pf, "0.00"), text_color = color.white, text_size = size.small)
table.cell(st, 0, 5, "Avg R / trade", text_color = color.white, text_size = size.small)
table.cell(st, 1, 5, str.tostring(avg, "0.00"), text_color = color.white, text_size = size.small)
table.cell(st, 0, 6, "Exit", text_color = color.white, text_size = size.tiny)
table.cell(st, 1, 6, str.tostring(statTP) + "R or session close", text_color = color.white, text_size = size.tiny)