AL Brooks Open Bars Theory ( 12 Bars 5MIN ) NY Session Breakout

everevolving365 · study · 145 行 · 点赞 72 · TradingView 原页

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

Pine Script

//@version=6
indicator('AL Brooks Open Bars Theory ( 12 Bars 5MIN ) NY Session Breakout ', overlay = true, max_boxes_count = 500, max_lines_count = 500)
 
// ==========================================
// USER INPUTS & TIMEZONE CONFIGURATION
// ==========================================
i_timeZone = input.string('America/Chicago', title = 'Session Time Zone', tooltip = 'Set to America/Chicago for Central Time (08:30 AM Open)')
i_sessTime = input.session('0830-1500:23456', title = 'RTH Session Window', tooltip = '08:30 AM to 03:00 PM Central Time, Monday-Friday')
i_barsToCount = input.int(12, title = 'Opening Range Bar Count', minval = 1, tooltip = 'Number of 5-MINUTE bars to evaluate from the session open. Always counted on the 5-minute timeframe, whatever chart timeframe is open (Al Brooks Concept = 18)')
 
// Color & Style Inputs
i_highColor = input.color(color.green, title = 'First 12-Bar High Level Color')
i_lowColor = input.color(color.red, title = 'First 12-Bar Low Level Color')
i_fillColor = input.color(color.new(color.blue, 92), title = 'Opening Range Background Fill')
 
// ==========================================
// 5-MINUTE ENGINE (HARD-CODED TIMEFRAME)
// ==========================================
// Bar counting, the range, the lock and the first breakout all run on
// 5-minute data, whatever chart timeframe is open.
ENGINE_TF = '5'
 
f_engine5m() =>
    // Session detection on 5-minute bars
    bool inSession5m = not na(time(ENGINE_TF, i_sessTime, i_timeZone))
    bool isNewSession5m = inSession5m and not inSession5m[1]
 
    // Session variables
    var int sessionBarCount5m = 0
    var float rangeHigh5m = na
    var float rangeLow5m = na
    var bool rangeLocked5m = false
    var int lockTime5m = na
    var int buyTime5m = na
    var int sellTime5m = na
 
    // Reset tracking on a new daily session
    if isNewSession5m
        sessionBarCount5m := 1
        rangeHigh5m := high
        rangeLow5m := low
        rangeLocked5m := false
        lockTime5m := na
        buyTime5m := na
        sellTime5m := na
        sellTime5m
    else if inSession5m and not rangeLocked5m
        sessionBarCount5m := sessionBarCount5m + 1
        rangeHigh5m := math.max(rangeHigh5m, high)
        rangeLow5m := math.min(rangeLow5m, low)
        rangeLow5m
 
    // Lock the range on the completion of the Nth 5-minute bar
    if inSession5m and sessionBarCount5m == i_barsToCount and not rangeLocked5m
        rangeLocked5m := true
        lockTime5m := time_close
        lockTime5m
 
    // First break of the locked range, stamped with its 5-minute bar time
    if inSession5m and rangeLocked5m
        if high > rangeHigh5m and na(buyTime5m)
            buyTime5m := time
            buyTime5m
        if low < rangeLow5m and na(sellTime5m)
            sellTime5m := time
            sellTime5m
 
    [rangeHigh5m, rangeLow5m, rangeLocked5m, inSession5m, lockTime5m, buyTime5m, sellTime5m]
 
[rangeHigh, rangeLow, engineLocked, engineInSession, lockTime, buyTime, sellTime] = request.security(syminfo.tickerid, ENGINE_TF, f_engine5m(), lookahead = barmerge.lookahead_off)
 
// ==========================================
// CHART SESSION TRACKING
// ==========================================
// Detect session status
inSession = false
if not na(time(timeframe.period, i_sessTime, i_timeZone))
    inSession := true
    inSession
 
isNewSession = inSession and not inSession[1]
 
var bool buyTriggered = false
var bool sellTriggered = false
 
// Reset trigger flags on a new daily session
if isNewSession
    buyTriggered := false
    sellTriggered := false
    sellTriggered
 
// Below 5 minutes, the open prints before the session's first 5-minute bar closes,
// so the engine only counts once that bar belongs to today's session
bool sub5m = timeframe.in_seconds() < 300
bool rangeLocked = sub5m ? engineLocked and engineInSession : engineLocked
 
// Levels go live on the chart bar that closes at (or after) the 5-minute lock
bool rangeReady = rangeLocked and time_close >= lockTime
 
// ==========================================
// BREAKOUT DETECTION (INTRABAR / REAL-TIME)
// ==========================================
bool buySignal = false
bool sellSignal = false
 
if sub5m
    // Below 5 minutes: the exact chart bar that first breaks the locked 5-minute range
    if inSession and rangeLocked and time_close > lockTime
        if high > rangeHigh and not buyTriggered
            buySignal := true
            buyTriggered := true
            buyTriggered
        if low < rangeLow and not sellTriggered
            sellSignal := true
            sellTriggered := true
            sellTriggered
else if timeframe.isintraday
    // 5 minutes and above: the chart bar that contains the 5-minute breakout
    buySignal := not na(buyTime) and (na(buyTime[1]) or buyTime != buyTime[1])
    sellSignal := not na(sellTime) and (na(sellTime[1]) or sellTime != sellTime[1])
    sellSignal
 
// ==========================================
// PLOTTING & VISUALS
// ==========================================
// Plot horizontal levels only after the 18 five-minute bars are completed
plotHigh = rangeReady and inSession ? rangeHigh : na
plotLow = rangeReady and inSession ? rangeLow : na
 
pHigh = plot(plotHigh, title = '18-Bar High', color = i_highColor, linewidth = 2, style = plot.style_line)
pLow = plot(plotLow, title = '18-Bar Low', color = i_lowColor, linewidth = 2, style = plot.style_line)
fill(pHigh, pLow, color = rangeReady and inSession ? i_fillColor : na, title = '18-Bar Range Zone')
 
// Plot Buy / Sell Signal Shapes
plotshape(buySignal, title = 'Bullish 18-Bar Breakout', style = shape.labelup, location = location.belowbar, color = color.green, text = 'BUY', textcolor = color.white, size = size.small)
plotshape(sellSignal, title = 'Bearish 18-Bar Breakout', style = shape.labeldown, location = location.abovebar, color = color.red, text = 'SELL', textcolor = color.white, size = size.small)
 
// Background highlight during the initial 18-bar evaluation period
bgcolor(inSession and not rangeReady ? color.new(color.orange, 90) : na, title = 'Evaluating First 18 Bars')
 
// ==========================================
// ALERT CONDITIONS FOR WEBHOOKS / TRADERS
// ==========================================
alertcondition(buySignal, title = '18-Bar High Breakout (BUY)', message = 'NQ 18-Bar High Breakout BUY Signal Triggered!')
alertcondition(sellSignal, title = '18-Bar Low Breakout (SELL)', message = 'NQ 18-Bar Low Breakout SELL Signal Triggered!')

← 返回列表