本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// SOKOLOV ORB Lite - opening range box and breakout marker for TradingView (Pine Script v6)
// (c) 2026 Sokolov Holdings. Free to use. Licensed under the Mozilla Public License 2.0.
// Draws the New York opening range and marks the first candle that closes outside it.
// It is an indicator only: no entries, stops, targets, position sizing, alerts or orders.
// Not investment advice. Trading involves a substantial risk of loss.
//@version=6
indicator("SOKOLOV ORB Lite", shorttitle="ORB Lite", overlay=true, max_boxes_count=100, max_labels_count=100)
string MAIN = "01 | Opening range"
string VIEW = "02 | Display"
string anchor = input.string("US cash · 09:30", "New York window", options=["New York FX · 08:00", "US data · 08:30", "US cash · 09:30"], group=MAIN, tooltip="All windows use America/New_York time with automatic daylight saving. If the market is closed or candles are missing inside the window, no range is drawn for that day.", display=display.none)
int orMinutes = input.int(15, "Opening range — minutes", options=[15,30], group=MAIN, display=display.none)
int confirmMinutes = input.int(15, "Breakout candle — minutes", options=[1,5,15], group=MAIN, tooltip="The breakout is marked when a complete candle of this length closes outside the range. These candles start at the end of the range. Markers appear until 12:00 New York, at most one per day.", display=display.none)
int keepDays = input.int(20, "Days kept on the chart", minval=1, maxval=40, group=VIEW, display=display.none)
string NY = "America/New_York"
int MIN = 60000
color GOLD = #C4A159
color PURPLE = #733B78
color GREEN = #168779
color RED = #D94362
color GREY = #8A8494
int anchorHour = anchor == "US cash · 09:30" ? 9 : 8
int anchorMinute = anchor == "New York FX · 08:00" ? 0 : 30
int barMs = timeframe.in_seconds() * 1000
if barstate.isfirst and (not chart.is_standard or not timeframe.isintraday or (orMinutes * MIN) % barMs != 0 or (confirmMinutes * MIN) % barMs != 0)
runtime.error("ORB Lite: use a standard intraday candle chart whose candle length fits evenly into the range and the breakout candle (for the 15 / 15 default: 1, 3, 5 or 15 minutes).")
f_day(int t) =>
year(t, NY) * 10000 + month(t, NY) * 100 + dayofmonth(t, NY)
var int day = na
var int rangeStart = na
var int rangeEnd = na
var int markEnd = na
var float orHigh = na
var float orLow = na
var int rangeBars = 0
var bool firstBarOk = false
var bool rangeReady = false
var bool brokeToday = false
var int bucket = na
var int bucketBars = 0
var bool bucketOk = false
var box orb = na
var array<box> boxes = array.new<box>()
var array<label> marks = array.new<label>()
// New York calendar day: reset the range.
int today = f_day(time)
if na(day) or today != day
day := today
rangeStart := timestamp(NY, year(time, NY), month(time, NY), dayofmonth(time, NY), anchorHour, anchorMinute)
rangeEnd := rangeStart + orMinutes * MIN
markEnd := timestamp(NY, year(time, NY), month(time, NY), dayofmonth(time, NY), 12, 0)
orHigh := na
orLow := na
rangeBars := 0
firstBarOk := false
rangeReady := false
brokeToday := false
bucket := na
bucketBars := 0
bucketOk := false
orb := na
// Build the opening range from the candles inside the window.
if time >= rangeStart and time < rangeEnd
if na(orb)
firstBarOk := time == rangeStart
orb := box.new(rangeStart, high, rangeEnd, low, xloc=xloc.bar_time, bgcolor=color.new(PURPLE, 87), border_color=GOLD, border_width=2, text="ORB · " + str.tostring(orMinutes) + "m", text_color=PURPLE, text_size=size.small, text_valign=text.align_bottom)
array.push(boxes, orb)
if array.size(boxes) > keepDays
box.delete(array.shift(boxes))
rangeBars += 1
orHigh := na(orHigh) ? high : math.max(orHigh, high)
orLow := na(orLow) ? low : math.min(orLow, low)
box.set_top(orb, orHigh)
box.set_bottom(orb, orLow)
if barstate.isconfirmed and time_close == rangeEnd
rangeReady := firstBarOk and rangeBars * barMs == orMinutes * MIN and orHigh - orLow >= 2 * syminfo.mintick
if not rangeReady
box.set_border_color(orb, GREY)
box.set_bgcolor(orb, color.new(GREY, 90))
box.set_text(orb, "ORB · incomplete data")
box.set_text_color(orb, GREY)
// Keep the range visible until 12:00 New York.
if rangeReady and not na(orb) and time >= rangeEnd and time < markEnd
box.set_right(orb, math.min(time_close, markEnd))
// Mark the first complete breakout candle that closes outside the range.
if rangeReady and not brokeToday and time >= rangeEnd and time < markEnd
int start = rangeEnd + int(math.floor((time - rangeEnd) / (confirmMinutes * MIN))) * confirmMinutes * MIN
if na(bucket) or start != bucket
bucket := start
bucketBars := 0
bucketOk := time == start
bucketBars += 1
if barstate.isconfirmed and bucketOk and time_close == start + confirmMinutes * MIN and bucketBars * barMs == confirmMinutes * MIN
bool up = close >= orHigh + syminfo.mintick * .999
bool down = close <= orLow - syminfo.mintick * .999
if up or down
brokeToday := true
label mark = na
if up
mark := label.new(time, low, "BREAKOUT ▲\n" + str.tostring(close, format.mintick), xloc=xloc.bar_time, style=label.style_label_up, color=GREEN, textcolor=color.white, size=size.small)
else
mark := label.new(time, high, "BREAKOUT ▼\n" + str.tostring(close, format.mintick), xloc=xloc.bar_time, style=label.style_label_down, color=RED, textcolor=color.white, size=size.small)
array.push(marks, mark)
if array.size(marks) > keepDays
label.delete(array.shift(marks))