ORB — Opening Range Breakout [LliterH]

LliterH · study · 139 行 · 点赞 148 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LliterH
 
// ╔══════════════════════════════════════════════════════════════╗
// ║   ORB — Opening Range Breakout                               ║
// ║   by @LliterH                                                ║
// ╚══════════════════════════════════════════════════════════════╝
//@version=6
indicator("ORB — Opening Range Breakout [LliterH]", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// ============================================================
// █ LliterH — Inputs █
// ============================================================
GRP_CORE    = "Core Settings"
GRP_FILTER  = "Breakout Filters"
GRP_DISPLAY = "UX / Interface"
GRP_CLR     = "Color Palette"
 
string sessionInput    = input.session("0930-0945",        title = "Opening Range Session",                       tooltip = "Time window used to build the ORB high and low.",                                                                                    group = GRP_CORE)
string timezoneInput   = input.string("America/New_York",  title = "Timezone",                                    options = ["America/New_York", "America/Chicago", "America/Los_Angeles", "America/Sao_Paulo", "Europe/London", "Europe/Frankfurt", "Europe/Madrid", "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Dubai", "Australia/Sydney"], tooltip = "Select the exchange or city closest to your market.", group = GRP_CORE)
int    signalWindowMin = input.int(60,                     title = "Signal Window (minutes after ORB close)",     minval = 0, maxval = 480, step = 15,  tooltip = "How many minutes after the ORB session ends the indicator is allowed to fire. Set to 0 to disable.", group = GRP_CORE)
float  minRangePips    = input.float(5.0,                  title = "Minimum ORB Range (points)",                  minval = 0.0, maxval = 10000.0, step = 0.5, tooltip = "Sessions where ORB high minus ORB low is smaller than this value are skipped.",           group = GRP_FILTER)
bool   requireClose    = input.bool(true,                  title = "Require candle close beyond ORB level",       tooltip = "When enabled, a breakout is confirmed only when a candle fully closes above ORB High or below ORB Low.",                               group = GRP_FILTER)
bool   showDashboard   = input.bool(true,                  title = "Show Dashboard",                              tooltip = "Toggle the information panel in the top-right corner.",                                                                               group = GRP_DISPLAY)
color  colorBox        = input.color(color.new(#42A5F5, 90), title = "ORB Box Color",                             tooltip = "Fill color for the Opening Range zone during the session.",                                                                           group = GRP_CLR)
color  colorUp         = input.color(color.new(#26c6da, 0),  title = "Bullish Color (Teal)",                      tooltip = "Color used for buy entries and bullish signals.",                                                                                     group = GRP_CLR)
color  colorDown       = input.color(color.new(#ef5350, 0),  title = "Bearish Color (Red)",                       tooltip = "Color used for sell entries and bearish signals.",                                                                                    group = GRP_CLR)
 
// ============================================================
// █ LliterH — Core Logic █
// ============================================================
 
// --- Session & Calendar Detection ---
bool inSession  = not na(time(timeframe.period, sessionInput, timezoneInput))
bool isFirstBar = inSession and not inSession[1]
bool isLastBar  = not inSession and inSession[1]
bool isNewDay   = dayofmonth != dayofmonth[1]
 
// --- Opening Range Construction ---
var float orHigh       = na
var float orLow        = na
var int   orbCloseTime = na
 
if isFirstBar
    orHigh       := high
    orLow        := low
    orbCloseTime := na
else if inSession
    orHigh := math.max(orHigh, high)
    orLow  := math.min(orLow, low)
 
if isLastBar
    orbCloseTime := time
 
// --- Signal Window ---
float minutesSinceClose = na(orbCloseTime) ? na : (time - orbCloseTime) / 60000.0
bool  withinWindow      = signalWindowMin == 0 ? true : (not na(minutesSinceClose) and minutesSinceClose <= signalWindowMin)
 
// --- Range Quality ---
float orbRange    = orHigh - orLow
bool  rangeIsWide = orbRange >= minRangePips
 
// --- State Machine ---
var int signal_state = 0
 
if isNewDay
    signal_state := 0
 
bool rawBreakoutUp   = requireClose ? (close[1] > orHigh and not inSession) : (ta.crossover(close, orHigh)  and not inSession)
bool rawBreakoutDown = requireClose ? (close[1] < orLow  and not inSession) : (ta.crossunder(close, orLow)  and not inSession)
 
bool breakoutUp   = rawBreakoutUp   and signal_state == 0  and rangeIsWide and withinWindow
bool breakoutDown = rawBreakoutDown and signal_state == 0  and rangeIsWide and withinWindow
bool exitLong     = rawBreakoutDown and signal_state == 1  and rangeIsWide and withinWindow
bool exitShort    = rawBreakoutUp   and signal_state == -1 and rangeIsWide and withinWindow
 
if breakoutUp
    signal_state := 1
if breakoutDown
    signal_state := -1
if exitLong
    signal_state := -1
if exitShort
    signal_state := 1
 
// ============================================================
// █ LliterH — Visuals █
// ============================================================
p_high = plot(orHigh, color = inSession ? na : color.new(color.gray, 50), style = plot.style_circles, title = "ORB High")
p_low  = plot(orLow,  color = inSession ? na : color.new(color.gray, 50), style = plot.style_circles, title = "ORB Low")
 
p_boxHigh = plot(inSession ? orHigh : na, color = na, editable = false)
p_boxLow  = plot(inSession ? orLow  : na, color = na, editable = false)
fill(p_boxHigh, p_boxLow, color = colorBox, title = "ORB Zone")
 
plot(ta.ema(close, 200), color = color.new(color.gray, 40), linewidth = 1, title = "EMA 200")
 
// Entry labels
plotshape(breakoutUp,   style = shape.labelup,   location = location.belowbar, color = colorUp,                      size = size.small, title = "BUY Entry",  text = "BUY",  textcolor = color.white)
plotshape(breakoutDown, style = shape.labeldown,  location = location.abovebar, color = colorDown,                    size = size.small, title = "SELL Entry", text = "SELL", textcolor = color.white)
 
// Exit labels — semi-transparent to distinguish from entries
plotshape(exitLong,  style = shape.labeldown, location = location.abovebar, color = color.new(colorDown, 40), size = size.tiny, title = "Exit Long",  text = "EXIT", textcolor = color.white)
plotshape(exitShort, style = shape.labelup,   location = location.belowbar, color = color.new(colorUp,   40), size = size.tiny, title = "Exit Short", text = "EXIT", textcolor = color.white)
 
// ============================================================
// █ LliterH — Dashboard █
// ============================================================
var table dash = table.new(position.top_right, 2, 4, bgcolor = color.new(color.black, 62), border_width = 1, border_color = color.new(color.gray, 65))
 
if showDashboard and barstate.islast
    table.cell(dash, 0, 0, "ORB Status",  text_color = color.new(color.gray, 0), text_size = size.small)
    table.cell(dash, 0, 1, "Range",        text_color = color.new(color.gray, 0), text_size = size.small)
    table.cell(dash, 0, 2, "Window",       text_color = color.new(color.gray, 0), text_size = size.small)
    table.cell(dash, 0, 3, "Signal State", text_color = color.new(color.gray, 0), text_size = size.small)
 
    string orbStatus = inSession ? "FORMING" : close > orHigh ? "ABOVE" : close < orLow ? "BELOW" : "INSIDE"
    color  orbColor  = inSession ? color.yellow : close > orHigh ? colorUp : close < orLow ? colorDown : color.white
    table.cell(dash, 1, 0, orbStatus, text_color = orbColor, text_size = size.small)
 
    string rangeText  = str.tostring(orbRange, "#.##") + (rangeIsWide ? " OK" : " SKIP")
    color  rangeColor = rangeIsWide ? colorUp : color.gray
    table.cell(dash, 1, 1, rangeText, text_color = rangeColor, text_size = size.small)
 
    string windowText  = signalWindowMin == 0 ? "unlimited" : withinWindow ? str.tostring(math.round(minutesSinceClose)) + "/" + str.tostring(signalWindowMin) + " min" : "CLOSED"
    color  windowColor = signalWindowMin == 0 ? color.gray : withinWindow ? colorUp : colorDown
    table.cell(dash, 1, 2, windowText, text_color = windowColor, text_size = size.small)
 
    string stateText  = signal_state ==  1 ? "LONG  active" : signal_state == -1 ? "SHORT active" : "waiting"
    color  stateColor = signal_state ==  1 ? colorUp : signal_state == -1 ? colorDown : color.gray
    table.cell(dash, 1, 3, stateText, text_color = stateColor, text_size = size.small)
 
// ============================================================
// █ LliterH — Alerts █
// ============================================================
alertcondition(breakoutUp,   title = "LliterH — ORB — BUY Entry",   message = "{{ticker}} | {{interval}} | ORB — BUY entry confirmed. First bullish breakout of the day within signal window. | Price: {{close}}")
alertcondition(breakoutDown, title = "LliterH — ORB — SELL Entry",  message = "{{ticker}} | {{interval}} | ORB — SELL entry confirmed. First bearish breakout of the day within signal window. | Price: {{close}}")
alertcondition(exitLong,     title = "LliterH — ORB — Exit Long",   message = "{{ticker}} | {{interval}} | ORB — Exit LONG. Opposite breakout confirmed. | Price: {{close}}")
alertcondition(exitShort,    title = "LliterH — ORB — Exit Short",  message = "{{ticker}} | {{interval}} | ORB — Exit SHORT. Opposite breakout confirmed. | Price: {{close}}")

← 返回列表