本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("Drop's Opening Range Breakout (ORB)", "DORB",
overlay=true,
initial_capital=100000,
default_qty_type=strategy.fixed,
default_qty_value=1,
commission_type=strategy.commission.cash_per_contract,
commission_value=2.50, // Pessimistic friction (commissions)
slippage=2, // Pessimistic friction (slippage)
margin_long=0,
margin_short=0)
// ==========================================
// 1. INPUTS & PARAMETERS
// ==========================================
gp_orb = "Opening Range Settings"
rangeSession = input.session("0930-0945", title="Range Definition Session", tooltip="The 15-minute window used to define the High and Low boundaries (New York Time).", group=gp_orb)
tradeSession = input.session("0945-1130", title="Trading Execution Session", tooltip="The window where the strategy is allowed to enter breakout trades.", group=gp_orb)
cutoffTime = input.string("1130", title="Hard Time Cutoff (HHMM)", tooltip="The time of day when all active positions are forcefully closed to avoid the afternoon chop.", group=gp_orb)
gp_risk = "Risk Management"
useHalfwayStop = input.bool(true, title="Use Range Midpoint for Stop?", tooltip="If enabled, the Stop Loss is placed at the 50% mark of the opening range (tight stop). If disabled, it is placed at the opposite side of the range (wide stop).", group=gp_risk)
riskReward = input.float(1.0, title="Risk-to-Reward Ratio (1:X)", minval=0.5, step=0.1, group=gp_risk)
gp_quant = "Quant Institutional Upgrades"
usePullbackLimit = input.bool(true, title="Use Pullback Limit Entry?", tooltip="Instead of chasing breakouts with market orders, this rests a Limit Order at the breakout boundary. Dramatically reduces slippage and fakeouts.", group=gp_quant)
useDynamicSizing = input.bool(false, title="Use Dynamic Risk Sizing?", tooltip="Enables professional risk parity. Sizes contract volume dynamically based on a fixed percentage of account equity.", group=gp_quant)
riskPerTrade = input.float(1.0, title="Risk Per Trade (% of Equity)", minval=0.1, maxval=5.0, step=0.1, group=gp_quant)
maxQtyCap = input.int(10, title="Max Contract Limit Cap", minval=1, group=gp_quant)
useDailyTrend = input.bool(true, title="Align with Daily Trend?", tooltip="Only allows Long breakouts if price is above the Daily 200 EMA, and Shorts if below. Prevents trading against institutional flows.", group=gp_quant)
gp_filters = "Advanced Filters"
useVolFilter = input.bool(false, title="Require Volume Expansion?", tooltip="Breakout candle volume must exceed the average volume of the last 20 bars to confirm institutional commitment.", group=gp_filters)
volMult = input.float(1.2, title="Volume Multiplier", minval=0.5, step=0.1, group=gp_filters)
useRangeFilter = input.bool(false, title="Filter Extreme Range Sizes?", tooltip="Prevents trading if the opening range is too wide (exhausted momentum) or too narrow (noise).", group=gp_filters)
maxRangeAtrMult = input.float(2.0, title="Max Range ATR Mult", tooltip="Do not trade if opening range is wider than this multiple of the 14-period ATR.", group=gp_filters)
minRangeAtrMult = input.float(0.4, title="Min Range ATR Mult", tooltip="Do not trade if opening range is narrower than this multiple of the 14-period ATR.", group=gp_filters)
useAutoBE = input.bool(true, title="Move Stop to Breakeven?", tooltip="Once trade reaches a 1:1 risk-to-reward ratio, move stop loss to entry.", group=gp_filters)
// Force New York Timezone to make backtest independent of the user's local chart settings
tz = "America/New_York"
// ==========================================
// 2. DAILY TREND SECURITY RESOLUTION
// ==========================================
// Pull Daily SMA/EMA dynamically to establish institutional macro trend filter
dailyClose = request.security(syminfo.tickerid, "D", close)
dailyEma = request.security(syminfo.tickerid, "D", ta.ema(close, 200))
dailyBullish = dailyClose > dailyEma
// ==========================================
// 3. SESSION TIMING LOGIC
// ==========================================
if not timeframe.isintraday
runtime.error("This strategy is designed for intraday charts only (e.g. 1m, 2m, 5m, 15m).")
inRangePeriod = not na(time(timeframe.period, rangeSession + ":23456", tz))
inTradeWindow = not na(time(timeframe.period, tradeSession + ":23456", tz))
currentHour = hour(time, tz)
currentMinute = minute(time, tz)
currentTimeFormatted = currentHour * 100 + currentMinute
cutoffFormatted = str.tonumber(cutoffTime)
isPastCutoff = currentTimeFormatted >= cutoffFormatted and currentTimeFormatted < 1600
// ==========================================
// 4. CAPTURING THE RANGE BOUNDARIES
// ==========================================
var float rangeHigh = na
var float rangeLow = na
var bool rangeDefined = false
var bool tradedToday = false
var bool isRangeValid = true
isNewDay = ta.change(time("D", tz)) != 0
// Reset variables at the start of each trading day
if isNewDay
rangeHigh := na
rangeLow := na
rangeDefined := false
tradedToday := false
isRangeValid := true
atrVal = ta.atr(14)
if inRangePeriod
if na(rangeHigh)
rangeHigh := high
rangeLow := low
else
rangeHigh := math.max(rangeHigh, high)
rangeLow := math.min(rangeLow, low)
if not inRangePeriod and not na(rangeHigh) and not rangeDefined
rangeDefined := true
rangeSize = rangeHigh - rangeLow
if useRangeFilter and not na(atrVal)
isRangeValid := (rangeSize <= (atrVal * maxRangeAtrMult)) and (rangeSize >= (atrVal * minRangeAtrMult))
// ==========================================
// 5. TRIGGER CONDITIONS
// ==========================================
volSma = ta.sma(volume, 20)
volExpansion = not useVolFilter or (volume > volSma * volMult)
// Macro trend check
trendLongAllowed = not useDailyTrend or dailyBullish
trendShortAllowed = not useDailyTrend or not dailyBullish
// Standard breakouts
breakoutLong = rangeDefined and isRangeValid and not tradedToday and inTradeWindow and ta.crossover(close, rangeHigh) and volExpansion and trendLongAllowed
breakoutShort = rangeDefined and isRangeValid and not tradedToday and inTradeWindow and ta.crossunder(close, rangeLow) and volExpansion and trendShortAllowed
// ==========================================
// 6. QUANT RISK PARITY POSITION SIZING
// ==========================================
// Calculate dynamic contract quantity based on custom risk budget
f_calculate_qty(float riskPrice) =>
if useDynamicSizing
riskCash = strategy.equity * (riskPerTrade / 100.0)
pointVal = syminfo.pointvalue
calculatedQty = riskCash / (riskPrice * pointVal)
math.min(maxQtyCap, math.max(1, math.floor(calculatedQty)))
else
1
// ==========================================
// 7. STRATEGY EXECUTION (Trade Management)
// ==========================================
var float activeSL = na
var float activeTP = na
var bool reachedBE = false
if strategy.position_size == 0
reachedBE := false
// If the trade window closes or cutoff is reached, cancel any unfilled pullback limit orders
if not inTradeWindow or isPastCutoff
strategy.cancel("ORB Long Pullback")
strategy.cancel("ORB Short Pullback")
// Long Entry execution
if breakoutLong and strategy.position_size == 0
rangeSize = rangeHigh - rangeLow
stopPrice = useHalfwayStop ? (rangeHigh - (rangeSize * 0.5)) : rangeLow
riskAmt = close - stopPrice
qtySize = f_calculate_qty(riskAmt)
activeSL := stopPrice
activeTP := close + (riskAmt * riskReward)
reachedBE := false
if usePullbackLimit
// Rests a limit buy order exactly at the Range High boundary
strategy.entry("ORB Long Pullback", strategy.long, qty=qtySize, limit=rangeHigh)
else
strategy.entry("ORB Long", strategy.long, qty=qtySize)
tradedToday := true
// Short Entry execution
if breakoutShort and strategy.position_size == 0
rangeSize = rangeHigh - rangeLow
stopPrice = useHalfwayStop ? (rangeLow + (rangeSize * 0.5)) : rangeHigh
riskAmt = stopPrice - close
qtySize = f_calculate_qty(riskAmt)
activeSL := stopPrice
activeTP := close - (riskAmt * riskReward)
reachedBE := false
if usePullbackLimit
// Rests a limit sell order exactly at the Range Low boundary
strategy.entry("ORB Short Pullback", strategy.short, qty=qtySize, limit=rangeLow)
else
strategy.entry("ORB Short", strategy.short, qty=qtySize)
tradedToday := true
// Trade execution exit brackets
if strategy.position_size > 0
initialRisk = strategy.position_avg_price - activeSL
if useAutoBE and not reachedBE and (close >= strategy.position_avg_price + initialRisk)
activeSL := strategy.position_avg_price
reachedBE := true
strategy.exit("Exit Long", stop=activeSL, limit=activeTP)
if strategy.position_size < 0
initialRisk = activeSL - strategy.position_avg_price
if useAutoBE and not reachedBE and (close <= strategy.position_avg_price - initialRisk)
activeSL := strategy.position_avg_price
reachedBE := true
strategy.exit("Exit Short", stop=activeSL, limit=activeTP)
// Force Time Cutoff
if strategy.position_size != 0 and isPastCutoff
strategy.close_all(comment="Time Cutoff (11:30 EST)")
// ==========================================
// 8. CHART VISUALIZATION
// ==========================================
plot(rangeDefined ? rangeHigh : na, title="Opening Range High", color=isRangeValid ? color.green : color.gray, style=plot.style_stepline, linewidth=2)
plot(rangeDefined ? rangeLow : na, title="Opening Range Low", color=isRangeValid ? color.red : color.gray, style=plot.style_stepline, linewidth=2)
plot(rangeDefined and useHalfwayStop ? (rangeHigh + rangeLow) / 2 : na, title="Range Midpoint", color=color.orange)
isTradingHour = inTradeWindow and not isPastCutoff
bgcolor(isTradingHour ? color.new(color.blue, 96) : na, title="Trading Session Background")