本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// Opening Candle Breakout [OCB] — Strategy
// Companion strategy to the OCB indicator. Same logic, simulated orders for backtesting.
// Inspired by Zarattini & Aziz (2023), "Can Day Trading Really Be Profitable?".
// © Swissy 2026 — Mozilla Public License 2.0
//@version=6
strategy("Opening Candle Breakout [OCB] Strategy", shorttitle = "OCB Strat", overlay = true, initial_capital = 25000, currency = currency.USD, default_qty_type = strategy.fixed, default_qty_value = 1, pyramiding = 0, commission_type = strategy.commission.cash_per_contract, commission_value = 0.5, slippage = 2, calc_on_every_tick = false, margin_long = 0, margin_short = 0)
// ─── Inputs ────────────────────────────────────────────────────────────────
string sessFirst = input.session("0930-0935", "First session candle", group = "Session", tooltip = "Must match exactly one chart candle (e.g. 0930-0935 on a 5-minute chart).")
string sessFlat = input.session("1555-1600", "Flat before close", group = "Session", tooltip = "Any open position is closed in this window.")
string tz = input.string("America/New_York", "Timezone of the session", group = "Session")
bool tradeLongs = input.bool(true, "Longs", inline = "s", group = "Session")
bool tradeShorts = input.bool(true, "Shorts", inline = "s", group = "Session")
float minBodyATR = input.float(0.5, "Minimum candle body (x ATR)", minval = 0, step = 0.05, group = "Setup", tooltip = "0 = no filter.")
int atrLen = input.int(14, "ATR period (bars)", minval = 5, group = "Setup")
float minStopATR = input.float(0.10, "Minimum stop (x ATR)", minval = 0.01, step = 0.05, group = "Setup")
float maxStopATR = input.float(2.0, "Maximum stop (x ATR)", minval = 0.25, step = 0.25, group = "Setup")
float tpR = input.float(10.0, "Target (R)", minval = 0.5, step = 0.5, group = "Exit", tooltip = "Position is closed at the target or at the end of the session, whichever comes first.")
bool useTP = input.bool(true, "Use target (unchecked = hold until session close)", group = "Exit")
string riskMode = input.string("Fixed USD", "Risk mode", options = ["Fixed USD", "% of equity"], group = "Risk")
float riskUSD = input.float(250, "Risk per trade (USD)", minval = 10, step = 25, group = "Risk")
float riskPct = input.float(1.0, "Risk per trade (% of equity)", minval = 0.1, step = 0.1, group = "Risk")
int maxQty = input.int(10, "Max contracts", minval = 1, group = "Risk")
bool allowMin = input.bool(false, "Allow 1 contract even if it exceeds the risk budget", group = "Risk")
// ─── Logic ─────────────────────────────────────────────────────────────────
bool isFirst = not na(time(timeframe.period, sessFirst, tz))
bool inFlat = not na(time(timeframe.period, sessFlat, tz))
bool newDay = dayofmonth(time, tz) != dayofmonth(time[1], tz)
float atr = ta.atr(atrLen)
bool bodyOk = minBodyATR <= 0 or math.abs(close - open) >= atr * minBodyATR
bool sigLong = isFirst and close > open and bodyOk and tradeLongs
bool sigShort = isFirst and close < open and bodyOk and tradeShorts
float rawRisk = sigLong ? close - low : sigShort ? high - close : na
float risk = math.min(math.max(rawRisk, atr * minStopATR), atr * maxStopATR)
f_qty(float r) =>
float budget = riskMode == "% of equity" ? strategy.equity * riskPct / 100.0 : riskUSD
float perContract = r * syminfo.pointvalue
int q = perContract > 0 ? int(math.floor(budget / perContract)) : 0
q := math.min(q, maxQty)
q < 1 and allowMin ? 1 : q
var bool tradedToday = false
if newDay
tradedToday := false
var float slLvl = na
var float tpLvl = na
// ─── Orders ────────────────────────────────────────────────────────────────
if strategy.position_size != 0 and inFlat
strategy.close_all(comment = "Session close")
if not tradedToday and strategy.position_size == 0 and (sigLong or sigShort)
int q = f_qty(risk)
if q >= 1
tradedToday := true
if sigLong
slLvl := close - risk
tpLvl := close + risk * tpR
strategy.entry("L", strategy.long, qty = q)
strategy.exit("XL", "L", stop = slLvl, limit = useTP ? tpLvl : na, comment_loss = "Stop", comment_profit = "Target")
else
slLvl := close + risk
tpLvl := close - risk * tpR
strategy.entry("S", strategy.short, qty = q)
strategy.exit("XS", "S", stop = slLvl, limit = useTP ? tpLvl : na, comment_loss = "Stop", comment_profit = "Target")
// ─── Plots ─────────────────────────────────────────────────────────────────
plot(strategy.position_size != 0 ? slLvl : na, "Stop", color = color.new(#FF0066, 0), style = plot.style_linebr, linewidth = 2)
plot(strategy.position_size != 0 and useTP ? tpLvl : na, "Target", color = color.new(#00C853, 0), style = plot.style_linebr, linewidth = 1)
plotshape(sigLong, "Long setup", shape.triangleup, location.belowbar, color = #009988, size = size.small)
plotshape(sigShort, "Short setup", shape.triangledown, location.abovebar, color = #CC3311, size = size.small)