本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 (MPL 2.0).
// (c) Unbroke - free & open-source. Not financial advice. For educational purposes only.
// orb stats 1.1
//@version=6
indicator("Unbroke ORB Stats", shorttitle = "UB ORB Stats", overlay = true, behind_chart = false, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
// Opening range, close-based breakouts and an honest Proof panel: what happens after the opening range,
// compared with ranges of the same length that start at a random time later in the same session.
// Registered rules: specs/orb_stats_prereg_rev2.md (supersedes rev1). Python mirror: tools/orb_stats_mirror.py.
//==============================================================================
// INPUTS
//==============================================================================
G_OR = "1. Opening range"
sessIn = input.session("0930-1600", "Session (HHMM-HHMM)", group = G_OR, tooltip = "Regular session in the time zone below. The opening range starts at the session open. Stats stop at the session end, or earlier on early-close days (session.islastbar).", display = display.none)
tzIn = input.string("America/New_York", "Time zone", group = G_OR, tooltip = "IANA time zone of the session. Default = US equities / CME equity index RTH.", display = display.none)
orSel = input.string("30", "Opening range length (min)", options = ["5", "15", "30", "60", "Custom"], group = G_OR, tooltip = "The chart timeframe must divide this length (5m chart for a 15m or 30m range).", display = display.none)
orCust = input.int(45, "Custom length (min)", minval = 1, maxval = 240, group = G_OR, tooltip = "Used when the length is set to Custom.", display = display.none)
hzMin = input.int(120, "Horizon after the range (min)", minval = 15, maxval = 480, group = G_OR, tooltip = "Breaks and outcomes are counted for this many minutes after a range ends. The same horizon is used for the random ranges, so both sides get the same time to work.", display = display.none)
G_DSP = "2. Display"
showDays = input.int(3, "Days drawn", minval = 1, maxval = 20, group = G_DSP, display = display.none)
glowOn = input.bool(true, "Glow on OR high / low", group = G_DSP, display = display.none)
showMid = input.bool(true, "Midline", group = G_DSP, display = display.none)
showExt = input.bool(true, "Extension targets", inline = "ex", group = G_DSP, tooltip = "Multiples of the opening range width, above the high and below the low. Only 1x is tested in the Proof panel.", display = display.none)
ext1 = input.float(0.5, "", minval = 0.1, step = 0.25, inline = "ex", group = G_DSP, display = display.none)
ext2 = input.float(1.0, "", minval = 0.1, step = 0.25, inline = "ex", group = G_DSP, display = display.none)
ext3 = input.float(2.0, "", minval = 0.1, step = 0.25, inline = "ex", group = G_DSP, display = display.none)
showMk = input.bool(true, "Breakout markers (first close beyond each side)", group = G_DSP, tooltip = "Printed only after the bar CLOSES beyond the range, once per side per day. Inside the horizon only.", display = display.none)
showLbl = input.bool(true, "Labels", group = G_DSP, display = display.none)
lblSize = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal"], group = G_DSP, display = display.none)
G_PRF = "3. Proof panel"
showPanel = input.bool(true, "Show panel", inline = "pp", group = G_PRF, tooltip = "Default Top Left, clear of the newest candles and price scale tags, Key Levels (top right), Risk Guard (top center), Backtester (bottom left) and SR Zones (bottom center).", display = display.none)
panelPos = input.string("Top Left", "", options = ["Top Left", "Middle Right", "Middle Left", "Bottom Right"], inline = "pp", group = G_PRF, display = display.none)
detailed = input.bool(false, "Detailed panel", group = G_PRF, tooltip = "Adds the break up / down, both / neither and median width rows, Wilson 95% intervals and the definitions row.", display = display.none)
panelTop = input.int(14, "Panel top offset (% of chart height)", minval = 0, maxval = 40, group = G_PRF, tooltip = "See-through spacer above the panel so it starts below the TradingView legend (symbol, OHLC, buy / sell, about 6 indicator names). 0 = flush to the top.", display = display.none)
int panelGap = panelTop > 0 ? 1 : 0
dashSize = input.string("Small", "Panel text size", options = ["Tiny", "Small", "Normal"], group = G_PRF, display = display.none)
minN = input.int(20, "Minimum n for a number", minval = 5, maxval = 500, group = G_PRF, tooltip = "Below this the panel shows 'insufficient data'.", display = display.none)
oosT = input.time(timestamp("26 Oct 2026 00:00 -0400"), "Forward test starts (fixed date)", group = G_PRF, tooltip = "Pre-registered (prereg rev2). Ranges resolved from this date on are the forward test.", display = display.none)
readT = input.time(timestamp("29 Oct 2027 16:00 -0400"), "Registered read (fixed date)", group = G_PRF, tooltip = "The forward result stays hidden until this date (prereg rev2: date only, no n trigger).", display = display.none)
kRand = input.int(10, "Random ranges per day", minval = 1, maxval = 20, group = G_PRF, tooltip = "Same-length ranges starting at distinct seeded random bars (no repeats in a day) between the end of the opening range and session end - horizon - length, so every random horizon ends inside the session. Scored with the same rules.", display = display.none)
seed = input.int(7, "Random seed", minval = 1, group = G_PRF, display = display.none)
G_XP = "4. Export"
xpOn = input.bool(false, "Export mode (data window plots)", group = G_XP, tooltip = "Adds 'ORB XP' plots to the data window and chart export so every bar can be matched with tools/orb_stats_mirror.py --tv-compare.", display = display.none)
ubThemeMode = input.string("Auto", "Theme", options = ["Auto", "Dark", "Light"], group = G_XP, tooltip = "Auto follows your chart background.", display = display.none)
//==============================================================================
// UNBROKE STYLE KIT for Pine v6 - paste this block into every Unbroke script.
// (c) Unbroke - MPL 2.0. Original code. ASCII only.
// Paste it AFTER the inputs. The script must declare, before this block:
// string ubThemeMode = input.string("Auto", "Theme", options = ["Auto", "Dark", "Light"], ...)
// Libraries need publishing, so for now this is pasted, not imported.
// Version: style kit 1.3 (2026-10-24: all table colors opaque; T_BG follows chart.bg_color in Auto). Keep every copy identical.
//==============================================================================
// ---- 1. Palette (fixed brand constants) -------------------------------------
UB_GREEN_DEEP = #1B7A5A // brand green (light-theme frame, deep fills)
UB_GREEN = #3FBF8F // brand green bright (dark-theme frame, bull)
UB_INK = #141210 // dark background / text on light
UB_PAPER = #F6F3EC // light background / text on dark
UB_BULL = #3FBF8F // up / safe / reacted
UB_BEAR = #E5534B // down / danger / broke
UB_NEUTRAL = #8C8A85 // mid lines, neutral state, muted marks
UB_AMBER = #F2A93B // caution, opening range
UB_SKY = #4C9AFF // info levels (previous day), entries
UB_VIOLET = #8B5CF6 // secondary info (London, previous week)
UB_PINK = #E0559A // Asia session
UB_CYAN = #14B8C4 // overnight session
// ---- 2. Theme detection -----------------------------------------------------
ub_lum(color c) =>
0.299 * color.r(c) + 0.587 * color.g(c) + 0.114 * color.b(c)
ub_isLight() =>
ub_lum(chart.bg_color) > 128
bool UB_LIGHT = ubThemeMode == "Light" or (ubThemeMode == "Auto" and ub_isLight())
color T_BG = color.new(ubThemeMode == "Auto" ? chart.bg_color : UB_LIGHT ? UB_PAPER : UB_INK, 0)
color T_ROW = color.new(UB_LIGHT ? #FFFFFF : #1E1C19, 0)
color T_HEAD = color.new(UB_LIGHT ? #E3F1EA : #0F2A20, 0)
color T_TEXT = UB_LIGHT ? UB_INK : UB_PAPER
color T_MUTED = UB_LIGHT ? #5C574F : #A8A39A
color T_FRAME = color.new(UB_LIGHT ? UB_GREEN_DEEP : UB_GREEN, 0)
color T_BORDER = color.new(UB_LIGHT ? #E2DCCF : #2A2723, 0)
color T_TRACK = color.new(UB_LIGHT ? #E2DCCF : #2E2B27, 0)
// Readable text on any fill color.
ub_txtOn(color c) =>
ub_lum(c) > 150 ? UB_INK : #FFFFFF
// ---- 3. Sizes and positions -------------------------------------------------
ub_size(string s) =>
s == "Tiny" ? size.tiny : s == "Small" ? size.small : s == "Large" ? size.large : size.normal
ub_sizeUp(string s) =>
s == "Tiny" ? size.small : s == "Small" ? size.normal : s == "Large" ? size.huge : size.large
ub_pos(string s) =>
s == "Top Right" ? position.top_right : s == "Bottom Right" ? position.bottom_right : s == "Top Left" ? position.top_left : s == "Middle Left" ? position.middle_left : s == "Middle Right" ? position.middle_right : s == "Bottom Center" ? position.bottom_center : s == "Top Center" ? position.top_center : position.bottom_left
// ---- 4. Gradient colors -----------------------------------------------------
// Strength color: bear at lo, neutral at the midpoint, bull at hi.
ub_strength(float v, float lo, float hi) =>
float mid = (lo + hi) / 2.0
v >= mid ? color.from_gradient(v, mid, hi, UB_NEUTRAL, UB_BULL) : color.from_gradient(v, lo, mid, UB_BEAR, UB_NEUTRAL)
// Room color: 0 = red, 50 = amber, 100 = green (risk meters).
ub_room(float pct) =>
pct >= 50 ? color.from_gradient(pct, 50, 100, UB_AMBER, UB_BULL) : color.from_gradient(pct, 0, 50, UB_BEAR, UB_AMBER)
// ---- 5. Glow for drawn lines (plots: see DESIGN_SYSTEM.md, plot() is global-only) --
// g = a var array<line> owned by the caller (created empty). Two halo lines are
// created once, then moved. Call on barstate.islast. x1 = start bar, y = price.
ub_glow(array<line> g, int x1, float y, color c, int baseW, bool on) =>
if array.size(g) == 0
array.push(g, line.new(na, na, na, na, xloc = xloc.bar_index, extend = extend.right, color = color.new(c, 88), width = baseW + 6))
array.push(g, line.new(na, na, na, na, xloc = xloc.bar_index, extend = extend.right, color = color.new(c, 72), width = baseW + 3))
bool ok = on and not na(y) and not na(x1)
for i = 0 to 1
line gl = array.get(g, i)
line.set_x1(gl, ok ? x1 : na)
line.set_x2(gl, ok ? x1 + 1 : na)
line.set_y1(gl, ok ? y : na)
line.set_y2(gl, ok ? y : na)
line.set_color(gl, color.new(c, i == 0 ? 88 : 72))
// ---- 6. Dashboard builder ---------------------------------------------------
// Header row: "UNBROKE" | title (merged) | subtitle. ncols >= 2.
ub_header(table t, int ncols, string title, string sub, hsz, int hr = 0) =>
table.cell(t, 0, hr, "UNBROKE", text_color = T_FRAME, bgcolor = T_HEAD, text_size = hsz, text_halign = text.align_left, text_formatting = text.format_bold)
if ncols == 2
table.cell(t, 1, hr, title, text_color = T_TEXT, bgcolor = T_HEAD, text_size = hsz, text_halign = text.align_right, text_formatting = text.format_bold, tooltip = sub)
else
table.cell(t, 1, hr, title, text_color = T_TEXT, bgcolor = T_HEAD, text_size = hsz, text_halign = text.align_left, text_formatting = text.format_bold)
table.cell(t, ncols - 1, hr, sub, text_color = T_MUTED, bgcolor = T_HEAD, text_size = hsz, text_halign = text.align_right)
if ncols > 3
table.merge_cells(t, 1, hr, ncols - 2, hr)
// Footer note across all columns (tiny, muted).
ub_footer(table t, int ncols, int row, string txt) =>
table.cell(t, 0, row, txt, text_color = T_MUTED, bgcolor = T_BG, text_size = size.tiny, text_halign = text.align_left)
if ncols > 1
table.merge_cells(t, 0, row, ncols - 1, row)
// Segmented progress bar in table cells c0 .. c0+segs-1 of one row.
// Each lit segment takes the room color of its own position (red -> amber -> green).
ub_bar(table t, int c0, int row, int segs, float pct, string tip) =>
for i = 0 to segs - 1
float mark = (i + 0.5) * 100.0 / segs
bool lit = not na(pct) and pct >= mark
table.cell(t, c0 + i, row, "", bgcolor = lit ? ub_room(mark) : T_TRACK, width = 1.4, height = 1.6, tooltip = tip)
// ASCII meter, e.g. "######...." for 60% with n = 10.
ub_meter(float pct, int n) =>
int k = na(pct) ? 0 : int(math.round(math.max(0.0, math.min(100.0, pct)) / 100.0 * n))
str.repeat("#", k) + str.repeat(".", n - k)
// ---- 7. PROOF panel (signature element of every Unbroke indicator) ----------
// Percent helper: a of b, as 0-100 float, na when b = 0.
ub_pct(int a, int b) =>
b > 0 ? a * 100.0 / b : na
// Proof column headers (5 columns): Signal | n | Hit % | vs random | Verdict.
ub_proofHead(table t, int row, string c0, tsz) =>
table.cell(t, 0, row, c0, text_color = T_MUTED, bgcolor = T_BG, text_size = tsz, text_halign = text.align_left)
table.cell(t, 1, row, "n", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz)
table.cell(t, 2, row, "Hit %", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz)
table.cell(t, 3, row, "vs random", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz)
table.cell(t, 4, row, "Verdict", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz)
//==================================== END UNBROKE STYLE KIT ===================
//==============================================================================
// SETUP
//==============================================================================
int s0 = int(str.tonumber(str.substring(sessIn, 0, 2))) * 60 + int(str.tonumber(str.substring(sessIn, 2, 4)))
int s1 = int(str.tonumber(str.substring(sessIn, 5, 7))) * 60 + int(str.tonumber(str.substring(sessIn, 7, 9)))
int Ln = orSel == "Custom" ? orCust : int(str.tonumber(orSel))
int tf = int(timeframe.in_seconds() / 60)
bool tfOk = timeframe.isintraday and tf > 0 and tf <= Ln and Ln % tf == 0 and s1 > s0 + Ln
int S = tfOk ? math.max(0, int(math.floor(float((s1 - hzMin - Ln) - (s0 + Ln)) / tf)) + 1) : 0
int dk = year(time, tzIn) * 10000 + month(time, tzIn) * 100 + dayofmonth(time, tzIn)
int m = hour(time, tzIn) * 60 + minute(time, tzIn)
bool inwin = m >= s0 and m + tf <= s1
float atrV = ta.atr(14)
int NR = 21
f_rand(int d, int k) =>
float x = math.sin(d * 12.9898 + k * 78.233 + 5 * 37.719 + seed * 4.1414) * 43758.5453
x - math.floor(x)
// width-in-ATR bins fixed in prereg rev2: <2, 2-3, 3-4, 4-5, >=5 (left-closed)
NB = 5
f_bin(float wa) => na(wa) ? -1 : wa >= 5.0 ? 4 : wa >= 4.0 ? 3 : wa >= 3.0 ? 2 : wa >= 2.0 ? 1 : 0
// range state, index 0 = opening range, 1..kRand = random lanes
// status: 0 wait, 1 build, 2 active, 3 done, 4 skipped, 5 unused
var array<int> rSt = array.new<int>(NR, 0)
var array<int> rStat = array.new<int>(NR, 5)
var array<int> rCnt = array.new<int>(NR, 0)
var array<float> rHi = array.new<float>(NR, na)
var array<float> rLo = array.new<float>(NR, na)
var array<float> rW = array.new<float>(NR, na)
var array<float> rWa = array.new<float>(NR, na)
var array<int> rEnd = array.new<int>(NR, 0)
var array<int> rUp = array.new<int>(NR, 0)
var array<int> rDn = array.new<int>(NR, 0)
var array<int> rFb = array.new<int>(NR, 0)
var array<int> rFbi = array.new<int>(NR, -1)
var array<int> rDec = array.new<int>(NR, 0) // 1 hit (1x first), 2 opposite side first
var array<int> rFail = array.new<int>(NR, 0)
// cumulative: idx = split*14 + side*7 + q ; split 0 all, 1 forward ; side 0 OR, 1 random
// q: 0 n, 1 up, 2 down, 3 both, 4 first breaks, 5 hits, 6 failed
var array<int> cum = array.new<int>(28, 0)
var array<int> lFb = array.new<int>(40, 0)
var array<int> lHit = array.new<int>(40, 0)
var array<float> wO0 = array.new<float>()
var array<float> wR0 = array.new<float>()
var array<float> wO1 = array.new<float>()
var array<float> wR1 = array.new<float>()
var array<int> skipA = array.new<int>(1, 0)
var array<int> noSes = array.new<int>(1, 0)
var bool sawWin = false
// width bins (first breaks only): idx = split*NB + bin ; lane bins: idx = split*100 + lane*NB + bin
var array<int> bOn = array.new<int>(10, 0)
var array<int> bOh = array.new<int>(10, 0)
var array<int> bRn = array.new<int>(10, 0)
var array<int> bRh = array.new<int>(10, 0)
var array<int> bLn = array.new<int>(200, 0)
var array<int> bLh = array.new<int>(200, 0)
// per-bar export counters: 0..13 = mirror KEYS order, 14 r_bsum, 15 r_hbsum
array<float> pb = array.new<float>(16, 0.0)
float oBinNow = na
f_resolve(int j, array<float> pbx) =>
rStat.set(j, 3)
int sd = j == 0 ? 0 : 1
int up = rUp.get(j)
int dn = rDn.get(j)
int fb = rFb.get(j) != 0 ? 1 : 0
int hit = rDec.get(j) == 1 ? 1 : 0
int fl = rFail.get(j)
array<int> v = array.from(1, up, dn, up * dn, fb, hit, fl)
bool fw = time >= oosT
for q = 0 to 6
pbx.set(sd * 7 + q, pbx.get(sd * 7 + q) + v.get(q))
cum.set(sd * 7 + q, cum.get(sd * 7 + q) + v.get(q))
if fw
cum.set(14 + sd * 7 + q, cum.get(14 + sd * 7 + q) + v.get(q))
int bb = f_bin(rWa.get(j))
if fb == 1 and bb >= 0
for sp = 0 to 1
if sp == 0 or fw
if j == 0
bOn.set(sp * NB + bb, bOn.get(sp * NB + bb) + 1)
bOh.set(sp * NB + bb, bOh.get(sp * NB + bb) + hit)
else
bRn.set(sp * NB + bb, bRn.get(sp * NB + bb) + 1)
bRh.set(sp * NB + bb, bRh.get(sp * NB + bb) + hit)
bLn.set(sp * 100 + (j - 1) * NB + bb, bLn.get(sp * 100 + (j - 1) * NB + bb) + 1)
bLh.set(sp * 100 + (j - 1) * NB + bb, bLh.get(sp * 100 + (j - 1) * NB + bb) + hit)
if j > 0
pbx.set(14, pbx.get(14) + bb + 1)
pbx.set(15, pbx.get(15) + (bb + 1) * hit)
if j > 0 and fb == 1
lFb.set(j - 1, lFb.get(j - 1) + 1)
lHit.set(j - 1, lHit.get(j - 1) + hit)
if fw
lFb.set(20 + j - 1, lFb.get(20 + j - 1) + 1)
lHit.set(20 + j - 1, lHit.get(20 + j - 1) + hit)
float wa = rWa.get(j)
if not na(wa)
if j == 0
wO0.push(wa)
if fw
wO1.push(wa)
else
wR0.push(wa)
if fw
wR1.push(wa)
f_finalize(array<float> pbx) =>
// F8: a date with no in-window bar is counted once as a no-session date, not as skipped ranges
if not sawWin
noSes.set(0, noSes.get(0) + 1)
for j = 0 to NR - 1
rStat.set(j, 5)
for j = 0 to kRand
int st = rStat.get(j)
if st == 2
f_resolve(j, pbx)
else if st <= 1
rStat.set(j, 4)
skipA.set(0, skipA.get(0) + 1)
//==============================================================================
// DRAWING HELPERS
//==============================================================================
type DayDraw
array<box> bx
array<line> ln
array<label> lb
var array<DayDraw> days = array.new<DayDraw>()
f_shade(color c) => UB_LIGHT ? color.rgb(color.r(c) * 0.55, color.g(c) * 0.55, color.b(c) * 0.55) : color.new(c, 0)
f_kill(DayDraw d) =>
for b in d.bx
b.delete()
for l in d.ln
l.delete()
for x in d.lb
x.delete()
// gradient box: 4 bands per half, outer bands strong (55) to middle faint (92)
f_boxes(DayDraw d, float hi, float lo, int tL, int tR) =>
float w = hi - lo
array<int> tr = array.from(55, 67, 80, 92)
if d.bx.size() == 0
for i = 0 to 7
d.bx.push(box.new(tL, hi, tR, lo, xloc = xloc.bar_time, border_color = color(na), border_width = 0, bgcolor = color.new(UB_AMBER, math.min(96, tr.get(i % 4) + (UB_LIGHT ? 4 : 0)))))
for i = 0 to 3
box bt = d.bx.get(i)
bt.set_top(hi - i * w / 8)
bt.set_bottom(hi - (i + 1) * w / 8)
bt.set_right(tR)
box bb = d.bx.get(4 + i)
bb.set_bottom(lo + i * w / 8)
bb.set_top(lo + (i + 1) * w / 8)
bb.set_right(tR)
f_hline(DayDraw d, int x1, int x2, float y, color c, int w, string st) =>
d.ln.push(line.new(x1, y, x2, y, xloc = xloc.bar_time, color = c, width = w, style = st))
// label ink: darker shade on light charts (UB_LIGHT = chart.bg_color luminance, style kit), full color on dark
f_ink(color c) => UB_LIGHT ? color.rgb(color.r(c) * 0.42, color.g(c) * 0.42, color.b(c) * 0.42) : color.new(c, 0)
var array<label> extLb = array.new<label>()
f_tag(DayDraw d, int x, float y, string t, color c, string sty) =>
if showLbl
d.lb.push(label.new(x, y, t, xloc = xloc.bar_time, style = sty, color = color.new(UB_LIGHT ? #FFFFFF : UB_INK, 25), textcolor = f_ink(c), size = ub_size(lblSize)))
// extension tag: same pill, also tracked in extLb so only the current day's tags survive
f_tagE(DayDraw d, int x, float y, string t) =>
if showLbl
label lb = label.new(x, y, t, xloc = xloc.bar_time, style = label.style_label_left, color = color.new(UB_LIGHT ? #FFFFFF : UB_INK, 25), textcolor = f_ink(UB_AMBER), size = ub_size(lblSize))
d.lb.push(lb)
extLb.push(lb)
//==============================================================================
// ENGINE (closed bars only; mirrors tools/orb_stats_mirror.py)
//==============================================================================
var int curDay = na
var bool dayDone = true
var float orH = na
var float orL = na
var int fbTime = na
var int fbSide = 0
float wNow = na
bool mkUp = false
bool mkDn = false
if barstate.isconfirmed and tfOk
if na(curDay) or dk != curDay
if not dayDone
f_finalize(pb)
curDay := dk
dayDone := false
orH := na
orL := na
fbTime := na
fbSide := 0
sawWin := false
// S1: distinct lane slots per day (partial Fisher-Yates driven by the seeded hash; same as the mirror)
int kUse = math.min(kRand, S)
array<int> slots = array.new<int>()
if S > 0
for q = 0 to S - 1
slots.push(q)
if kUse > 0
for i = 0 to kUse - 1
int jj = math.min(S - 1, i + int(math.floor(f_rand(dk, i) * (S - i))))
int tmp = slots.get(i)
slots.set(i, slots.get(jj))
slots.set(jj, tmp)
for j = 0 to NR - 1
int st0 = j == 0 ? s0 : j <= kUse ? s0 + Ln + slots.get(j - 1) * tf : 0
rSt.set(j, st0)
rStat.set(j, j > kUse ? 5 : 0)
rCnt.set(j, 0)
rUp.set(j, 0)
rDn.set(j, 0)
rFb.set(j, 0)
rFbi.set(j, -1)
rDec.set(j, 0)
rFail.set(j, 0)
rWa.set(j, na)
if not dayDone and not inwin and m >= s1
f_finalize(pb)
dayDone := true
if not dayDone and inwin
sawWin := true
for j = 0 to kRand
int st = rStat.get(j)
int a = rSt.get(j)
if st <= 1 and m >= a and m < a + Ln
bool ok = true
if m == a
rStat.set(j, 1)
rHi.set(j, high)
rLo.set(j, low)
rCnt.set(j, 1)
if j == 0
DayDraw d = DayDraw.new(array.new<box>(), array.new<line>(), array.new<label>())
days.push(d)
while days.size() > showDays
f_kill(days.shift())
else if st == 1
rHi.set(j, math.max(rHi.get(j), high))
rLo.set(j, math.min(rLo.get(j), low))
rCnt.set(j, rCnt.get(j) + 1)
else
rStat.set(j, 4)
skipA.set(0, skipA.get(0) + 1)
ok := false
if ok and j == 0 and days.size() > 0
f_boxes(days.last(), rHi.get(0), rLo.get(0), timestamp(tzIn, year(time, tzIn), month(time, tzIn), dayofmonth(time, tzIn), int(s0 / 60), s0 % 60), time_close)
if ok and m + tf == a + Ln
if rCnt.get(j) * tf == Ln
rStat.set(j, 2)
float W = rHi.get(j) - rLo.get(j)
rW.set(j, W)
rEnd.set(j, a + Ln + hzMin)
rWa.set(j, atrV > 0 ? W / atrV : na)
if j == 0
orH := rHi.get(0)
orL := rLo.get(0)
wNow := rWa.get(0)
oBinNow := f_bin(wNow) >= 0 ? f_bin(wNow) : na
// lines, extensions and labels for today
if days.size() > 0
DayDraw d = days.last()
int tL = timestamp(tzIn, year(time, tzIn), month(time, tzIn), dayofmonth(time, tzIn), int(s0 / 60), s0 % 60)
int tE = timestamp(tzIn, year(time, tzIn), month(time, tzIn), dayofmonth(time, tzIn), int(s1 / 60), s1 % 60)
for y in array.from(orH, orL)
if glowOn
// design-system glow: wide faint halo, narrower inner halo, thin bright core on top
// Pine transparency: 0 = opaque, 100 = invisible
f_hline(d, tL, tE, y, color.new(UB_AMBER, 85), 10, line.style_solid) // outer halo: soft band
f_hline(d, tL, tE, y, color.new(UB_AMBER, 65), 4, line.style_solid) // inner halo
f_hline(d, tL, tE, y, f_ink(UB_AMBER), 1, line.style_solid)
if showMid
f_hline(d, tL, tE, (orH + orL) / 2, color.new(UB_NEUTRAL, 20), 1, line.style_dotted)
// extension tags sit at the LEFT end of the levels (OR end), text running right, so they never
// reach the right side (Key Levels / SR Zones tags). Only today's are kept: older days' tags
// scrolled to the left side showed as clipped stubs.
int tX = time_close
for lb in extLb
lb.delete()
extLb.clear()
if showExt
for e in array.from(ext1, ext2, ext3)
f_hline(d, time_close, tE, orH + e * W, color.new(UB_AMBER, 45), 1, line.style_dashed)
f_hline(d, time_close, tE, orL - e * W, color.new(UB_AMBER, 45), 1, line.style_dashed)
f_tagE(d, tX, orH + e * W, "+" + str.tostring(e) + "x " + str.tostring(orH + e * W, format.mintick))
f_tagE(d, tX, orL - e * W, "-" + str.tostring(e) + "x " + str.tostring(orL - e * W, format.mintick))
f_tag(d, tL, orH, "OR " + str.tostring(Ln) + "m | " + str.tostring(W, format.mintick) + (na(wNow) ? "" : " = " + str.tostring(wNow, "0.0") + " ATR"), UB_AMBER, label.style_label_lower_left)
else
rStat.set(j, 4)
skipA.set(0, skipA.get(0) + 1)
if j == 0 and days.size() > 0
f_kill(days.pop())
else if st == 0 and m >= a
rStat.set(j, 4)
skipA.set(0, skipA.get(0) + 1)
else if st == 1 and m >= a + Ln
rStat.set(j, 4)
skipA.set(0, skipA.get(0) + 1)
if j == 0 and days.size() > 0
f_kill(days.pop())
else if st == 2 and m >= a + Ln
float hi = rHi.get(j)
float lo = rLo.get(j)
float W = rW.get(j)
int fb = rFb.get(j)
if fb != 0 and bar_index > rFbi.get(j)
if rDec.get(j) == 0
bool opp = fb > 0 ? low <= lo : high >= hi
bool tg = fb > 0 ? high >= hi + W : low <= lo - W
rDec.set(j, opp ? 2 : tg ? 1 : 0)
if rFail.get(j) == 0 and rDec.get(j) != 1 and (fb > 0 ? close <= hi : close >= lo)
rFail.set(j, 1)
if close > hi
if j == 0 and rUp.get(0) == 0
mkUp := true
rUp.set(j, 1)
if close < lo
if j == 0 and rDn.get(0) == 0
mkDn := true
rDn.set(j, 1)
if fb == 0 and (close > hi or close < lo)
rFb.set(j, close > hi ? 1 : -1)
rFbi.set(j, bar_index)
if j == 0
fbTime := time
fbSide := close > hi ? 1 : -1
if m + tf >= rEnd.get(j)
f_resolve(j, pb)
if m + tf >= s1 or session.islastbar
f_finalize(pb)
dayDone := true
plotshape(showMk and mkUp, "ORB break up", shape.triangleup, location.belowbar, f_shade(UB_BULL), size = size.small, display = display.pane)
plotshape(showMk and mkDn, "ORB break down", shape.triangledown, location.abovebar, f_shade(UB_BEAR), size = size.small, display = display.pane)
alertcondition(mkUp, "ORB first close above the opening range", "ORB: first close above the opening range")
alertcondition(mkDn, "ORB first close below the opening range", "ORB: first close below the opening range")
//==============================================================================
// EXPORT (data window). Names unique to this script: 'ORB XP ...'
//==============================================================================
plot(xpOn ? 110 : na, "ORB XP ver", display = display.data_window)
plot(xpOn ? pb.get(0) : na, "ORB XP o_n", display = display.data_window)
plot(xpOn ? pb.get(1) : na, "ORB XP o_up", display = display.data_window)
plot(xpOn ? pb.get(2) : na, "ORB XP o_dn", display = display.data_window)
plot(xpOn ? pb.get(3) : na, "ORB XP o_both", display = display.data_window)
plot(xpOn ? pb.get(4) : na, "ORB XP o_fb", display = display.data_window)
plot(xpOn ? pb.get(5) : na, "ORB XP o_hit", display = display.data_window)
plot(xpOn ? pb.get(6) : na, "ORB XP o_fail", display = display.data_window)
plot(xpOn ? pb.get(7) : na, "ORB XP r_n", display = display.data_window)
plot(xpOn ? pb.get(8) : na, "ORB XP r_up", display = display.data_window)
plot(xpOn ? pb.get(9) : na, "ORB XP r_dn", display = display.data_window)
plot(xpOn ? pb.get(10) : na, "ORB XP r_both", display = display.data_window)
plot(xpOn ? pb.get(11) : na, "ORB XP r_fb", display = display.data_window)
plot(xpOn ? pb.get(12) : na, "ORB XP r_hit", display = display.data_window)
plot(xpOn ? pb.get(13) : na, "ORB XP r_fail", display = display.data_window)
plot(xpOn ? orH : na, "ORB XP orH", display = display.data_window)
plot(xpOn ? orL : na, "ORB XP orL", display = display.data_window)
plot(xpOn ? wNow : na, "ORB XP o_w", display = display.data_window)
plot(xpOn ? skipA.get(0) : na, "ORB XP skip", display = display.data_window)
plot(xpOn ? noSes.get(0) : na, "ORB XP nosess", display = display.data_window)
plot(xpOn ? oBinNow : na, "ORB XP o_bin", display = display.data_window)
plot(xpOn ? pb.get(14) : na, "ORB XP r_bsum", display = display.data_window)
plot(xpOn ? pb.get(15) : na, "ORB XP r_hbsum", display = display.data_window)
//==============================================================================
// PROOF PANEL
//==============================================================================
var table panel = na
f_cell(int c, int r, string t, color tc, string tip = "") =>
panel.cell(c, r, t, text_color = tc, bgcolor = T_ROW, text_size = ub_size(dashSize), text_halign = c == 0 ? text.align_left : text.align_center, tooltip = tip)
f_c(int s, int sd, int q) => cum.get(s * 14 + sd * 7 + q)
f_p(int a, int b) => b > 0 ? str.tostring(100.0 * a / b, "0") + "%" : "-"
f_rate(int s, int sd) => f_c(s, sd, 4) > 0 ? float(f_c(s, sd, 5)) / f_c(s, sd, 4) : na
f_wilson(int h, int n) =>
float lo = na
float hi = na
if n > 0
float z = 1.96
float p = float(h) / n
float d = 1 + z * z / n
float c = (p + z * z / (2.0 * n)) / d
float w = z * math.sqrt(p * (1 - p) / n + z * z / (4.0 * n * n)) / d
lo := c - w
hi := c + w
[lo, hi]
// percentile of the OR hit rate among the single-lane rates (ties count half)
f_pctile(int s, float zr) =>
int mm = 0
float below = 0.0
for k = 0 to kRand - 1
int n = lFb.get(s * 20 + k)
if n > 0
float r = float(lHit.get(s * 20 + k)) / n
mm += 1
below += r < zr ? 1.0 : r == zr ? 0.5 : 0.0
mm > 0 and not na(zr) ? 100.0 * below / mm : na
// width-reweighted random hit rate (prereg rev2 primary). on/rn/rh: 5 bins. Empty random bin -> nearest
// non-empty bin by index, tie -> lower width bin. Returns [rate, n_eff of the random side].
f_wrate(array<int> on, array<int> rn, array<int> rh) =>
int tot = 0
int anyR = 0
for b = 0 to NB - 1
tot += on.get(b)
anyR += rn.get(b)
float acc = na
float ne = 0.0
if tot > 0 and anyR > 0
acc := 0.0
array<float> wt = array.new<float>(NB, 0.0)
for b = 0 to NB - 1
if on.get(b) > 0
int best = -1
for t = 0 to NB - 1
if rn.get(t) > 0 and (best < 0 or math.abs(t - b) < math.abs(best - b))
best := t
float sh = float(on.get(b)) / tot
acc += sh * rh.get(best) / rn.get(best)
wt.set(best, wt.get(best) + sh)
float s2 = 0.0
for t = 0 to NB - 1
if rn.get(t) > 0
s2 += wt.get(t) * wt.get(t) / rn.get(t)
ne := s2 > 0 ? 1.0 / s2 : 0.0
[acc, ne]
f_sl(array<int> a, int off) => a.slice(off, off + NB)
// [OR hit rate over binned OR breaks, n binned, reweighted random rate, n_eff, lane percentile of the OR rate]
f_wstats(int s) =>
array<int> on = f_sl(bOn, s * NB)
int n1 = 0
int h1 = 0
for b = 0 to NB - 1
n1 += on.get(b)
h1 += bOh.get(s * NB + b)
float zr = n1 > 0 ? float(h1) / n1 : na
[rw, ne] = f_wrate(on, f_sl(bRn, s * NB), f_sl(bRh, s * NB))
int mm = 0
float below = 0.0
for k = 0 to kRand - 1
[lr, lne] = f_wrate(on, f_sl(bLn, s * 100 + k * NB), f_sl(bLh, s * 100 + k * NB))
if not na(lr)
mm += 1
below += lr < zr ? 1.0 : lr == zr ? 0.5 : 0.0
float pc = mm > 0 and not na(zr) ? 100.0 * below / mm : na
[zr, n1, rw, ne, pc]
f_ci(int s, int sd) =>
[lo, hi] = f_wilson(f_c(s, sd, 5), f_c(s, sd, 4))
na(lo) ? "-" : str.tostring(lo * 100, "0") + "-" + str.tostring(hi * 100, "0") + "%"
// one history row: random first, then OR (design rule: random baseline first)
f_hist(int r, string name, string rv, string ov, int n, string tip, string note = "descriptive") =>
bool enough = n >= minN and f_c(0, 1, 0) >= minN
f_cell(0, r, name, T_MUTED, tip)
f_cell(1, r, str.tostring(n), T_MUTED, tip)
f_cell(2, r, enough ? rv : "-", T_MUTED, tip)
f_cell(3, r, enough ? ov : "-", T_MUTED, tip)
f_cell(4, r, enough ? note : "insufficient data", T_MUTED, tip)
if barstate.islast and showPanel
if na(panel)
// with a spacer the table itself is see-through; every real cell paints its own background
panel := table.new(ub_pos(panelPos), 5, 14 + panelGap, bgcolor = panelGap > 0 ? color.new(T_BG, 100) : T_BG, frame_color = T_FRAME, frame_width = panelGap > 0 ? 0 : 1, border_color = panelGap > 0 ? color.new(T_BORDER, 100) : T_BORDER, border_width = 1)
panel.clear(0, 0, 4, 13 + panelGap)
if panelGap > 0
// one transparent cell merged across all columns; its height (percent of pane height) pushes the panel down.
// TradingView collapses empty rows without an explicit height, so the offset must come from height.
panel.cell(0, 0, "", text_size = size.tiny, bgcolor = color.new(T_BG, 100), width = 0, height = panelTop)
panel.merge_cells(0, 0, 4, 0)
ub_header(panel, 5, "ORB Stats", "OR " + str.tostring(Ln) + "m | " + timeframe.period, ub_sizeUp(dashSize), panelGap)
int r = 1 + panelGap
if not tfOk
f_cell(0, r, "Use an intraday chart whose timeframe divides the range length (e.g. 5m for a 30m range).", UB_AMBER)
panel.merge_cells(0, r, 4, r)
r += 1
else
// today
string tTxt = na(orH) ? "waiting for the opening range" : "H " + str.tostring(orH, format.mintick) + " L " + str.tostring(orL, format.mintick)
string bTxt = na(orH) ? "-" : fbSide == 0 ? "no close break yet" : (fbSide > 0 ? "broke up " : "broke down ") + str.format_time(fbTime, "HH:mm", tzIn)
f_cell(0, r, "Today", T_MUTED)
f_cell(1, r, tTxt, f_shade(UB_AMBER))
panel.merge_cells(1, r, 2, r)
f_cell(3, r, bTxt, fbSide > 0 ? f_shade(UB_BULL) : fbSide < 0 ? f_shade(UB_BEAR) : T_MUTED)
panel.merge_cells(3, r, 4, r)
r += 1
// column heads
color hc = T_MUTED
panel.cell(0, r, "History (descriptive)", text_color = hc, bgcolor = T_BG, text_size = ub_size(dashSize), text_halign = text.align_left)
panel.cell(1, r, "n", text_color = hc, bgcolor = T_BG, text_size = ub_size(dashSize))
panel.cell(2, r, "Random", text_color = hc, bgcolor = T_BG, text_size = ub_size(dashSize), tooltip = str.tostring(kRand) + " same-length ranges per day starting at a seeded random time after the opening range. Same horizon and rules.")
panel.cell(3, r, "OR", text_color = hc, bgcolor = T_BG, text_size = ub_size(dashSize))
panel.cell(4, r, "Note", text_color = hc, bgcolor = T_BG, text_size = ub_size(dashSize))
r += 1
int nO = f_c(0, 0, 0)
int nR = f_c(0, 1, 0)
string dTip = "Share of days (random: ranges) with a CLOSE beyond only the high / only the low / both / neither within " + str.tostring(hzMin) + " min after the range. Random n = " + str.tostring(nR) + "."
if detailed
f_hist(r, "Break up / down only", f_p(f_c(0, 1, 1) - f_c(0, 1, 3), nR) + " / " + f_p(f_c(0, 1, 2) - f_c(0, 1, 3), nR), f_p(f_c(0, 0, 1) - f_c(0, 0, 3), nO) + " / " + f_p(f_c(0, 0, 2) - f_c(0, 0, 3), nO), nO, dTip)
r += 1
f_hist(r, "Break both / neither", f_p(f_c(0, 1, 3), nR) + " / " + f_p(nR - f_c(0, 1, 1) - f_c(0, 1, 2) + f_c(0, 1, 3), nR), f_p(f_c(0, 0, 3), nO) + " / " + f_p(nO - f_c(0, 0, 1) - f_c(0, 0, 2) + f_c(0, 0, 3), nO), nO, dTip)
r += 1
int fO = f_c(0, 0, 4)
int fR = f_c(0, 1, 4)
float hO = f_rate(0, 0)
float hR = f_rate(0, 1)
float pc0 = f_pctile(0, hO)
string hTip = "First close beyond the range, then from the next bar: does price touch 1x the range width beyond the broken side before it touches the other side? Both on one bar = other side first. Random breakouts n = " + str.tostring(fR) + ". Loaded history includes the data used while building the tool, so it is descriptive only."
[wz, wn, wr, wne, wpc] = f_wstats(0)
string wTip = "Primary statistic (prereg rev2): random breakouts reweighted to the OR's width-in-ATR mix (bins <2, 2-3, 3-4, 4-5, >=5 ATR), because a wider range needs a longer move to reach 1x. Random effective n = " + str.tostring(wne, "0") + ". The registered number comes from the mirror's day-clustered bootstrap; this panel line is an approximation."
f_hist(r, "Reach 1x, width-matched", na(wr) ? "-" : str.tostring(100 * wr, "0") + "%", na(wz) ? "-" : str.tostring(100 * wz, "0") + "%", wn, wTip, "approx." + (na(wpc) ? "" : " | pct " + str.tostring(wpc, "0")))
r += 1
f_hist(r, "Reach 1x, raw (not width-matched)", f_p(f_c(0, 1, 5), fR), f_p(f_c(0, 0, 5), fO), fO, hTip, "descriptive, raw" + (na(pc0) ? "" : " | pct " + str.tostring(pc0, "0")))
r += 1
f_hist(r, "Failed (closed back inside)", f_p(f_c(0, 1, 6), fR), f_p(f_c(0, 0, 6), fO), fO, "Share of first breakouts where a bar closes back inside the range (or beyond the other side) before 1x is touched.")
r += 1
float mO = wO0.size() > 0 ? wO0.median() : na
float mR = wR0.size() > 0 ? wR0.median() : na
if detailed
f_hist(r, "Median width (ATR)", na(mR) ? "-" : str.tostring(mR, "0.00"), na(mO) ? "-" : str.tostring(mO, "0.00"), nO, "Range high - low divided by ATR(14) of the chart at the range's last bar.")
r += 1
// forward: hidden until the registered read
int fF = f_c(1, 0, 4)
int fRF = f_c(1, 1, 4)
bool ready = time >= readT
string fTip = "Pre-registered forward test (prereg rev2), NY session, NQ futures, 5-minute: reach-1x rate, OR vs width-matched random, from the fixed start date. Hidden until the read date, so it cannot be peeked at. The panel shows an approximate difference and CI only; the registered result (mirror day-clustered bootstrap) is reported in the published read, not on the chart."
f_cell(0, r, "Reach 1x, forward (width-matched)", T_TEXT, fTip)
f_cell(1, r, str.tostring(fF), T_TEXT, fTip)
if ready
[fz, fn, frw, fne, fpc] = f_wstats(1)
int fh = na(fz) ? 0 : int(math.round(fz * fn))
[zl, zh] = f_wilson(fh, fn)
float se = fne > 0 and not na(frw) ? math.sqrt(frw * (1 - frw) / fne) : na
string vt = "insufficient data"
color vc = UB_NEUTRAL
if fn >= minN and fne >= minN and not na(se)
bool beat = zl > frw + 1.96 * se and fpc > 97.5
bool worse = zh < frw - 1.96 * se
// display-only CI from the existing Wilson bounds and se; feeds no signal, alert, plot or export
float hw = math.sqrt(math.pow((zh - zl) / 2, 2) + math.pow(1.96 * se, 2))
float dl = (fz - frw) - hw
float dh = (fz - frw) + hw
vt := "vs random: " + (fz >= frw ? "+" : "") + str.tostring((fz - frw) * 100, "0.0") + " pts (approx. 95% CI " + str.tostring(dl * 100, "0.0") + " to " + str.tostring(dh * 100, "0.0") + "), this chart's history only, not a forecast"
f_cell(2, r, na(frw) ? "-" : str.tostring(100 * frw, "0") + "%", T_TEXT, fTip)
f_cell(3, r, na(fz) ? "-" : str.tostring(100 * fz, "0") + "%", T_TEXT, fTip)
f_cell(4, r, vt, f_shade(vc), fTip)
else
f_cell(2, r, "hidden", T_MUTED, fTip)
f_cell(3, r, "hidden", T_MUTED, fTip)
f_cell(4, r, "recording n " + str.tostring(fF), f_shade(UB_AMBER), fTip)
r += 1
if detailed
f_cell(0, r, "95% Wilson, history: OR " + f_ci(0, 0) + " | random " + f_ci(0, 1) + (ready ? " | forward: OR " + f_ci(1, 0) + " | random " + f_ci(1, 1) : " | forward hidden"), T_MUTED)
panel.merge_cells(0, r, 4, r)
r += 1
f_cell(0, r, "close-based breaks | horizon " + str.tostring(hzMin) + "m | random " + str.tostring(kRand) + "/day, seed " + str.tostring(seed) + " | ranges skipped " + str.tostring(skipA.get(0)) + " | no-session dates " + str.tostring(noSes.get(0)), T_MUTED)
panel.merge_cells(0, r, 4, r)
r += 1
// one readable footer line (kit footer is size.tiny)
panel.cell(0, r, "orb stats 1.1 | Educational, not financial advice. Past results don't predict future results.", text_color = T_MUTED, bgcolor = T_BG, text_size = size.small, text_halign = text.align_left)
panel.merge_cells(0, r, 4, r)