Breakfast Breakout - Praj

prajwal51 · strategy · 154 行 · 点赞 8 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
strategy('ORB Trend Strategy', shorttitle = 'ORB Trend', overlay = true, initial_capital = 100000,
     default_qty_type = strategy.percent_of_equity, default_qty_value = 25, pyramiding = 0,
     commission_type = strategy.commission.percent, commission_value = 0.02, slippage = 2,
     process_orders_on_close = true, calc_on_every_tick = false,
     max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
 
if not timeframe.isintraday or timeframe.in_seconds() > 30 * 60
    runtime.error('Use an intraday timeframe of 30 min or lower (1m / 5m / 15m recommended).')
 
// ───────────── Inputs ─────────────
G1 = 'Opening Range'
tz         = input.string('America/New_York', 'Timezone', group = G1)
rangeSess  = input.session('0930-1030', 'Range Session (first hour)', group = G1)
boxColor   = input.color(color.new(color.blue, 85), 'Box Fill', group = G1)
highColor  = input.color(color.green, 'High Line', group = G1)
lowColor   = input.color(color.red, 'Low Line', group = G1)
vertColor  = input.color(color.gray, 'Vertical Lines', group = G1)
 
G2 = 'Trend Filter (no signals in sideways market)'
adxLen      = input.int(14, 'ADX Length', minval = 2, group = G2)
adxMin      = input.float(25, 'Min ADX', minval = 5, group = G2)
adxRising   = input.bool(true, 'ADX Must Be Rising (vs 3 bars ago)', group = G2)
emaSepATR   = input.float(0.15, 'Min |EMA9 - EMA21| (x ATR)', minval = 0, step = 0.05, group = G2)
 
G3 = 'Signal'
requireBreak = input.bool(true, 'Require Close Outside Opening Range', group = G3)
rsiLen       = input.int(14, 'RSI Length', minval = 2, group = G3)
rsiLongMin   = input.float(55, 'RSI Long Min', group = G3)
rsiLongMax   = input.float(75, 'RSI Long Max (avoid overbought)', group = G3)
rsiShortMax  = input.float(45, 'RSI Short Max', group = G3)
rsiShortMin  = input.float(25, 'RSI Short Min (avoid oversold)', group = G3)
entryWindow  = input.int(330, 'No New Trades After (min from 9:30)', minval = 60, group = G3)
maxTrades    = input.int(2, 'Max Trades / Day', minval = 1, group = G3)
 
G4 = 'Exits'
slATR     = input.float(1.5, 'Stop (x ATR)', minval = 0.2, step = 0.1, group = G4)
rewardR   = input.float(2.0, 'Target (R multiple)', minval = 0.5, step = 0.1, group = G4)
exitOnEma = input.bool(true, 'Exit When EMA 9/21 Flip Against Trade', group = G4)
 
showChop = input.bool(true, 'Shade Sideways / No-Trade Periods', group = 'Visuals')
 
// ───────────── Indicators ─────────────
ema9  = ta.ema(close, 9)
ema21 = ta.ema(close, 21)
vwap  = ta.vwap(hlc3)
rsi   = ta.rsi(close, rsiLen)
[macdLine, macdSignal, macdHist] = ta.macd(close, 12, 26, 9)
[diPlus, diMinus, adx] = ta.dmi(adxLen, adxLen)
atr   = ta.atr(14)
 
// ───────────── Opening range (9:30 - 10:30) drawing ─────────────
inRange   = not na(time(timeframe.period, rangeSess, tz))
newRange  = inRange and not inRange[1]
rangeDone = not inRange and inRange[1]
 
var float rH          = na
var float rL          = na
var int   rStart      = na
var bool  rangeReady  = false
var bool  dayActive   = false
var int   tradesToday = 0
var box   rBox        = na
var line  hLine       = na
var line  lLine       = na
var line  vStart      = na
var line  vEnd        = na
 
if newRange
    rH          := high
    rL          := low
    rStart      := time
    rangeReady  := false
    dayActive   := true
    tradesToday := 0
    rBox   := box.new(time, high, time_close, low, xloc = xloc.bar_time, border_color = vertColor, bgcolor = boxColor)
    hLine  := line.new(time, high, time_close, high, xloc = xloc.bar_time, color = highColor, width = 2)
    lLine  := line.new(time, low, time_close, low, xloc = xloc.bar_time, color = lowColor, width = 2)
    vStart := line.new(time, high, time, low, xloc = xloc.bar_time, color = vertColor, width = 2)
else if dayActive and inRange
    rH := math.max(rH, high)
    rL := math.min(rL, low)
    box.set_top(rBox, rH)
    box.set_bottom(rBox, rL)
    box.set_right(rBox, time_close)
    line.set_xy1(hLine, rStart, rH)
    line.set_xy2(hLine, time_close, rH)
    line.set_xy1(lLine, rStart, rL)
    line.set_xy2(lLine, time_close, rL)
    line.set_xy1(vStart, rStart, rH)
    line.set_xy2(vStart, rStart, rL)
else if dayActive and rangeDone
    rangeReady := true
    box.set_right(rBox, time)
    vEnd := line.new(time, rH, time, rL, xloc = xloc.bar_time, color = vertColor, width = 2)
    line.set_x2(hLine, time_close)
    line.set_x2(lLine, time_close)
else if dayActive and rangeReady
    // extend the range lines through the rest of the session
    line.set_x2(hLine, time_close)
    line.set_x2(lLine, time_close)
 
// ───────────── Trend / sideways filter ─────────────
adxOK    = adx >= adxMin and (not adxRising or adx > adx[3])
emaSepOK = math.abs(ema9 - ema21) >= emaSepATR * atr
trending = adxOK and emaSepOK
 
inWindow = rangeReady and not na(rStart) and time <= rStart + entryWindow * 60000
 
longBase  = ema9 > ema21 and close > vwap and diPlus > diMinus and rsi >= rsiLongMin and rsi <= rsiLongMax and macdLine > macdSignal and macdHist > 0
shortBase = ema9 < ema21 and close < vwap and diMinus > diPlus and rsi <= rsiShortMax and rsi >= rsiShortMin and macdLine < macdSignal and macdHist < 0
 
longCond  = trending and inWindow and longBase and (not requireBreak or close > rH)
shortCond = trending and inWindow and shortBase and (not requireBreak or close < rL)
 
buySignal  = longCond and not longCond[1]
sellSignal = shortCond and not shortCond[1]
 
// ───────────── Orders ─────────────
if buySignal and strategy.position_size == 0 and tradesToday < maxTrades
    risk = slATR * atr
    strategy.entry('Long', strategy.long)
    strategy.exit('Long Exit', 'Long', stop = close - risk, limit = close + rewardR * risk)
    tradesToday += 1
 
if sellSignal and strategy.position_size == 0 and tradesToday < maxTrades
    risk = slATR * atr
    strategy.entry('Short', strategy.short)
    strategy.exit('Short Exit', 'Short', stop = close + risk, limit = close - rewardR * risk)
    tradesToday += 1
 
if exitOnEma
    if strategy.position_size > 0 and ema9 < ema21
        strategy.close('Long', comment = 'EMA flip')
    if strategy.position_size < 0 and ema9 > ema21
        strategy.close('Short', comment = 'EMA flip')
 
if session.islastbar_regular
    if strategy.position_size != 0
        strategy.close_all('EOD')
    dayActive := false
 
// ───────────── Plots ─────────────
plot(ema9, '9 EMA', color.new(color.aqua, 0), 1)
plot(ema21, '21 EMA', color.new(color.fuchsia, 0), 1)
plot(vwap, 'VWAP', color.new(color.orange, 0), 2)
 
plotshape(buySignal, 'BUY', shape.labelup, location.belowbar, color.green, text = 'BUY', textcolor = color.white, size = size.small)
plotshape(sellSignal, 'SELL', shape.labeldown, location.abovebar, color.red, text = 'SELL', textcolor = color.white, size = size.small)
 
bgcolor(showChop and dayActive and rangeReady and not trending ? color.new(color.gray, 90) : na, title = 'Sideways')
 
alertcondition(buySignal, 'BUY Signal', 'ORB Trend: BUY')
alertcondition(sellSignal, 'SELL Signal', 'ORB Trend: SELL')

← 返回列表