本页源码来自 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.
// auto trendlines 1.0.4
//@version=6
indicator("Unbroke Auto Trendlines", shorttitle = "UB Trendlines", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back = 5000)
// Trendlines through CONFIRMED swing points and the bar a line breaks (bar close only), with an honest Proof panel:
// how often a break ran 1 ATR before reversing, next to entries at seeded random bars scored the same way.
// Non-repainting: a pivot exists only from its confirming bar; nothing is drawn on a bar before it was known.
// Registered rules: specs/auto_trendlines_prereg_rev1.md. Python mirror: tools/auto_trendlines_mirror.py.
//==============================================================================
// INPUTS
//==============================================================================
G_LIN = "1. Trendlines"
pivLen = input.int(10, "Swing length (bars each side)", minval = 2, maxval = 50, group = G_LIN, tooltip = "A swing high needs this many lower bars on each side. It is only known this many bars later, so lines start late on purpose: that is what keeps them from repainting.", display = display.none)
slopeMax = input.float(0.2, "Slope filter (max ATR per bar)", minval = 0.01, maxval = 5, step = 0.05, group = G_LIN, tooltip = "Lines steeper than this many ATR(14) per bar are ignored. Steep lines are skipped by design.", display = display.none)
maxAge = input.int(300, "Retire unbroken lines after (bars)", minval = 20, maxval = 2000, group = G_LIN, tooltip = "Counted from the second swing point.", display = display.none)
G_PRF = "2. Proof panel"
showPanel = input.bool(true, "Show panel", inline = "pp", group = G_PRF, tooltip = "Default Bottom Right: sits over the volume area, clear of candles when the indicator is alone. With the full Unbroke set, Top Right is Key Levels and Bottom Left is the Backtester.", display = display.none)
panelPos = input.string("Bottom Right", "", options = ["Bottom Right", "Top Right", "Bottom Left", "Middle Left"], inline = "pp", group = G_PRF, display = display.none)
panelLift = input.int(8, "Panel offset (% of chart height)", minval = 0, maxval = 60, group = G_PRF, tooltip = "Empty space between the panel and its chart border (pushes a bottom panel up, a top panel down). Use it to clear the volume bars or another panel.", display = display.none)
detailed = input.bool(false, "Detailed panel", group = G_PRF, tooltip = "Compact panel = 7 rows (two footer rows). Detailed adds the difference row, false breaks with CI, up / down splits and the settings row.", display = display.none)
dashSize = input.string("Small", "Panel text size", options = ["Tiny", "Small", "Normal"], group = G_PRF, display = display.none)
hzBars = input.int(24, "Horizon (bars)", minval = 2, maxval = 500, group = G_PRF, tooltip = "Registered: 24. A break (or random entry) is a hit when price runs 1 ATR its way before 1 ATR against within this many bars. Both on one bar = against (conservative).", display = display.none)
pRand = input.float(0.05, "Random entry rate (per bar)", minval = 0.005, maxval = 0.5, step = 0.005, group = G_PRF, tooltip = "Share of bars used as random entries (seeded, 50/50 long / short), scored with the same rule. Registered: 0.05.", display = display.none)
seed = input.int(7, "Random seed", group = G_PRF, 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 rev1). Entries 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 (date only).", display = display.none)
G_DSP = "3. Style"
ubThemeMode = input.string("Auto", "Theme", options = ["Auto", "Dark", "Light"], group = G_DSP, tooltip = "Auto follows the chart background.", display = display.none)
glowOn = input.bool(true, "Glow on active lines", group = G_DSP, display = display.none)
showAnc = input.bool(false, "Dotted anchor (swing to confirmation)", group = G_DSP, tooltip = "Off by default. The part of a line BEFORE it was confirmed, drawn faint and dotted: the line did not exist yet on those bars. Breaks are only counted after confirmation.", display = display.none)
showLbl = input.bool(true, "Break labels", group = G_DSP, display = display.none)
keepOld = input.int(12, "Old lines kept on chart", minval = 0, maxval = 60, group = G_DSP, display = display.none)
lblSize = input.string("Normal", "Label size", options = ["Small", "Normal", "Large"], group = G_DSP, display = display.none)
futBars = input.int(5, "Project active lines (bars right)", minval = 0, maxval = 50, group = G_DSP, tooltip = "Active lines are drawn this many bars past the latest bar along their own slope: a projection of where the line will be, not a forecast.", display = display.none)
keepLast = input.bool(true, "Keep last line after it retires", group = G_DSP, tooltip = "When a line reaches the 300-bar retire age unbroken it stops counting for the stats. With this on it stays drawn (dashed, no glow) until price closes through it or a new line replaces it. No break label and no score for it.", display = display.none)
xpOn = input.bool(false, "Export mode (mirror check)", group = G_DSP, tooltip = "Adds 'TL XP ...' columns to the Data Window and to Export chart data, for tools/auto_trendlines_mirror.py --tv-compare.", 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) =>
table.cell(t, 0, 0, "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, 0, 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, 0, 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, 0, sub, text_color = T_MUTED, bgcolor = T_HEAD, text_size = hsz, text_halign = text.align_right)
if ncols > 3
table.merge_cells(t, 1, 0, ncols - 2, 0)
// 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 ===================
//==============================================================================
// STATE
//==============================================================================
float A = ta.atr(14)
f_hash(float k) =>
int m = int(math.floor(time / 60000.0)) % 100003
float x = math.sin(m * 12.9898 + seed * 4.1414 + k) * 43758.5453
x - math.floor(x)
type TL
int c1
float y1
int c2
float y2
float s
int conf
int side
line core
line h1
line h2
line anc
f_y(TL l, int j) =>
l.y2 + l.s * (j - l.c2)
var TL res = na
var TL sup = na
var int lastPHi = na
var float lastPHy = na
var int lastPLi = na
var float lastPLy = na
var array<TL> old = array.new<TL>()
// pending entries (breaks and random)
var array<int> pI = array.new<int>()
var array<int> pD = array.new<int>()
var array<float> pC = array.new<float>()
var array<float> pA = array.new<float>()
var array<bool> pB = array.new<bool>()
var array<bool> pF = array.new<bool>()
var array<float> pS = array.new<float>()
var array<float> pY = array.new<float>()
var array<int> pX = array.new<int>()
var array<int> pT = array.new<int>()
// cumulative counts: [0] = before the forward date (in-sample), [1] = forward. idx: bn bh bf rn rh bnU bhU bnD bhD rnU rhU rnD rhD
var array<int> cnt = array.new<int>(26, 0)
f_add(int s, int q, int v) =>
cnt.set(s * 13 + q, cnt.get(s * 13 + q) + v)
f_g(int s, int q) => cnt.get(s * 13 + q)
int xBrk = 0
int xRnd = 0
int xBres = 0
int xBhit = 0
int xBfal = 0
int xRres = 0
int xRhit = 0
// gradient colors for line + glow
color C_RES = UB_LIGHT ? #E0352B : #FF5C52
color C_SUP = UB_LIGHT ? #0FA86E : #2EF0A6
f_ink(color c) => color.new(c, 0)
// Panel text: the kit's muted grey is too light on a light chart, so the panel uses a darker one there.
color T_MUT = UB_LIGHT ? #5A544B : T_MUTED
var TL gRes = na
var TL gSup = na
// label spacing state [last bar, last side, stack step] (an array: functions may not reassign globals)
var array<int> lblSt = array.from(-1000, 0, 0)
f_retire(TL l, bool broke) =>
if not na(l)
color c = l.side > 0 ? C_RES : C_SUP
// stop at the last drawn bar (the break bar), keep a faded glow
int xe = math.min(l.core.get_x2(), bar_index)
float ye = f_y(l, xe)
l.core.set_xy2(xe, ye)
l.h1.set_xy2(xe, ye)
l.h2.set_xy2(xe, ye)
l.h1.set_color(color.new(c, 90))
l.h2.set_color(color.new(c, 80))
if not broke
l.h1.delete()
l.h2.delete()
l.core.set_width(broke ? 3 : 2)
l.core.set_color(color.new(c, broke ? 35 : 55))
l.core.set_style(broke ? line.style_solid : line.style_dashed)
old.push(l)
while old.size() > keepOld
TL o = old.shift()
o.core.delete()
o.h1.delete()
o.h2.delete()
o.anc.delete()
f_draw(TL l) =>
color c = l.side > 0 ? C_RES : C_SUP
float yc = f_y(l, l.conf)
// Pine transparency: 0 = opaque, 100 = invisible. Halo lines go first so the core sits on top.
// Active lines run to the latest bar plus futBars along their slope (f_extend, every bar); display only.
// Created in this order (halo 1, halo 2, core) so the core is drawn on top.
int x2 = bar_index + futBars
l.h1 := glowOn ? line.new(l.conf, yc, x2, f_y(l, x2), color = color.new(c, UB_LIGHT ? 80 : 75), width = 14) : na
l.h2 := glowOn ? line.new(l.conf, yc, x2, f_y(l, x2), color = color.new(c, UB_LIGHT ? 62 : 55), width = 7) : na
l.core := line.new(l.conf, yc, x2, f_y(l, x2), color = f_ink(c), width = 3)
l.anc := showAnc ? line.new(l.c1, l.y1, l.conf, yc, color = color.new(c, 70), width = 1, style = line.style_dotted) : na
f_extend(TL l) =>
if not na(l)
int x2 = bar_index + futBars
float y = f_y(l, x2)
l.core.set_xy2(x2, y)
l.h1.set_xy2(x2, y)
l.h2.set_xy2(x2, y)
//==============================================================================
// 1. SCORE PENDING ENTRIES (made on earlier bars)
//==============================================================================
if pI.size() > 0
for i = pI.size() - 1 to 0
int d = pD.get(i)
float c0 = pC.get(i)
float a0 = pA.get(i)
bool stp = d > 0 ? low <= c0 - a0 : high >= c0 + a0
bool run = d > 0 ? high >= c0 + a0 : low <= c0 - a0
int dec = stp ? 2 : run ? 1 : 0
if pB.get(i) and not pF.get(i) and dec != 1
float y = pY.get(i) + pS.get(i) * (bar_index - pX.get(i))
if d > 0 ? close <= y : close >= y
pF.set(i, true)
if dec == 0 and bar_index - pI.get(i) >= hzBars
dec := 3
if dec != 0
int s = pT.get(i) >= oosT ? 1 : 0
int h = dec == 1 ? 1 : 0
if pB.get(i)
xBres += 1
xBhit += h
xBfal += pF.get(i) ? 1 : 0
f_add(s, 0, 1)
f_add(s, 1, h)
f_add(s, 2, pF.get(i) ? 1 : 0)
f_add(s, d > 0 ? 5 : 7, 1)
f_add(s, d > 0 ? 6 : 8, h)
else
xRres += 1
xRhit += h
f_add(s, 3, 1)
f_add(s, 4, h)
f_add(s, d > 0 ? 9 : 11, 1)
f_add(s, d > 0 ? 10 : 12, h)
pI.remove(i)
pD.remove(i)
pC.remove(i)
pA.remove(i)
pB.remove(i)
pF.remove(i)
pS.remove(i)
pY.remove(i)
pX.remove(i)
pT.remove(i)
f_pend(int d, bool brk, float s, float y2, int x2) =>
pI.push(bar_index)
pD.push(d)
pC.push(close)
pA.push(A)
pB.push(brk)
pF.push(false)
pS.push(s)
pY.push(y2)
pX.push(x2)
pT.push(time)
f_lbl(int d, float y) =>
if showLbl
float k = na(A) or A == 0 ? 0.5 : math.min(1.0, math.abs(close - y) / A)
color base = d > 0 ? C_SUP : C_RES
// Opaque fills: a weak break is the line color mixed 40% toward the chart background, a 1 ATR break is full color.
color fillC = color.from_gradient(k, 0, 1, color.from_gradient(40, 0, 100, base, T_BG), base)
// Placed off the wick (0.5 ATR below the low / above the high). A label within 6 bars of the last one on
// the same side steps a further 0.9 ATR away so labels never stack.
int stk = lblSt.get(1) == d and bar_index - lblSt.get(0) <= 6 ? lblSt.get(2) + 1 : 0
lblSt.set(0, bar_index)
lblSt.set(1, d)
lblSt.set(2, stk)
float off = (na(A) ? 0.0 : A) * (0.5 + 0.9 * stk)
label.new(bar_index, d > 0 ? low - off : high + off, d > 0 ? "BRK ^" : "BRK v", style = d > 0 ? label.style_label_up : label.style_label_down, color = fillC, textcolor = ub_txtOn(fillC), size = lblSize == "Small" ? size.small : lblSize == "Large" ? size.large : size.normal, text_formatting = text.format_bold, tooltip = (d > 0 ? "Break up. " : "Break down. ") + "Closed " + str.tostring(math.abs(close - y), format.mintick) + " beyond the line (" + str.tostring(k, "0.00") + " ATR). Bar close only; never moved later. Label placed 0.5 ATR or more off the wick.")
//==============================================================================
// 2. EXPIRY, THEN BREAKS (bar close)
//==============================================================================
if not na(res)
if bar_index - res.c2 > maxAge
if keepLast
// display only: stays drawn, dashed, no glow; no longer counted (it is not the active line)
f_retire(gRes, false)
res.h1.delete()
res.h2.delete()
res.core.set_style(line.style_dashed)
res.core.set_width(2)
gRes := res
else
f_retire(res, false)
res := na
else if bar_index > res.conf and close > f_y(res, bar_index)
float yb = f_y(res, bar_index)
xBrk := 1
f_extend(res)
if not na(A)
f_pend(1, true, res.s, res.y2, res.c2)
f_lbl(1, yb)
f_retire(res, true)
res := na
if not na(sup)
if bar_index - sup.c2 > maxAge
if keepLast
// display only: stays drawn, dashed, no glow; no longer counted (it is not the active line)
f_retire(gSup, false)
sup.h1.delete()
sup.h2.delete()
sup.core.set_style(line.style_dashed)
sup.core.set_width(2)
gSup := sup
else
f_retire(sup, false)
sup := na
else if bar_index > sup.conf and close < f_y(sup, bar_index)
float yb = f_y(sup, bar_index)
xBrk := -1
f_extend(sup)
if not na(A)
f_pend(-1, true, sup.s, sup.y2, sup.c2)
f_lbl(-1, yb)
f_retire(sup, true)
sup := na
//==============================================================================
// 3. RANDOM ENTRY AT THIS CLOSE (seeded, reproducible)
//==============================================================================
if not na(A) and f_hash(0.0) < pRand
int d = f_hash(78.233) < 0.5 ? 1 : -1
xRnd := d
f_pend(d, false, 0.0, 0.0, 0)
//==============================================================================
// 4. PIVOTS CONFIRMED ON THIS BAR -> NEW LINES
//==============================================================================
float hiPrev = ta.highest(high, pivLen)[pivLen + 1]
float hiNext = ta.highest(high, pivLen)
float loPrev = ta.lowest(low, pivLen)[pivLen + 1]
float loNext = ta.lowest(low, pivLen)
if bar_index >= 2 * pivLen
int c = bar_index - pivLen
float ph = high[pivLen]
float pl = low[pivLen]
if ph > hiPrev and ph >= hiNext
if not na(lastPHi) and lastPHy > ph and not na(A)
float s = (ph - lastPHy) / (c - lastPHi)
if math.abs(s) <= slopeMax * A
bool ok = true
for j = lastPHi + 1 to bar_index
if close[bar_index - j] > lastPHy + s * (j - lastPHi)
ok := false
break
if ok
if not na(res)
f_retire(res, false)
res := TL.new(lastPHi, lastPHy, c, ph, s, bar_index, 1)
f_draw(res)
lastPHi := c
lastPHy := ph
if pl < loPrev and pl <= loNext
if not na(lastPLi) and lastPLy < pl and not na(A)
float s = (pl - lastPLy) / (c - lastPLi)
if math.abs(s) <= slopeMax * A
bool ok = true
for j = lastPLi + 1 to bar_index
if close[bar_index - j] < lastPLy + s * (j - lastPLi)
ok := false
break
if ok
if not na(sup)
f_retire(sup, false)
sup := TL.new(lastPLi, lastPLy, c, pl, s, bar_index, -1)
f_draw(sup)
lastPLi := c
lastPLy := pl
// last-line ghosts (display only): end on a close through them or when a new line of the side exists
if not na(gRes) and (close > f_y(gRes, bar_index) or not na(res))
f_retire(gRes, false)
gRes := na
if not na(gSup) and (close < f_y(gSup, bar_index) or not na(sup))
f_retire(gSup, false)
gSup := na
f_extend(res)
f_extend(sup)
f_extend(gRes)
f_extend(gSup)
//==============================================================================
// EXPORT MODE (unique 'TL XP' columns; every value is per bar, no running totals)
//==============================================================================
plot(xpOn ? 100 : na, "TL XP ver", display = display.data_window)
plot(xpOn ? xBrk : na, "TL XP brk", display = display.data_window)
plot(xpOn ? xRnd : na, "TL XP rnd", display = display.data_window)
plot(xpOn ? xBres : na, "TL XP b_res", display = display.data_window)
plot(xpOn ? xBhit : na, "TL XP b_hit", display = display.data_window)
plot(xpOn ? xBfal : na, "TL XP b_false", display = display.data_window)
plot(xpOn ? xRres : na, "TL XP r_res", display = display.data_window)
plot(xpOn ? xRhit : na, "TL XP r_hit", display = display.data_window)
plot(xpOn and not na(res) ? f_y(res, bar_index) : na, "TL XP res_y", display = display.data_window)
plot(xpOn and not na(sup) ? f_y(sup, bar_index) : na, "TL XP sup_y", display = display.data_window)
alertcondition(xBrk == 1, "Trendline break up", "Unbroke Trendlines: close above a resistance line")
alertcondition(xBrk == -1, "Trendline break down", "Unbroke Trendlines: close below a support line")
//==============================================================================
// PROOF PANEL (descriptive; no verdict before the registered read)
//==============================================================================
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_rt(int h, int n) => n > 0 ? str.tostring(100.0 * h / n, "0") + "%" : "-"
f_ci(int h, int n) =>
string out = "-"
if n > 0
float z = 1.96
float p = h / float(n)
float d = 1 + z * z / n
float m = (p + z * z / (2 * n)) / d
float w = z * math.sqrt(p * (1 - p) / n + z * z / (4.0 * n * n)) / d
out := str.tostring(100 * (m - w), "0") + "-" + str.tostring(100 * (m + w), "0") + "%"
out
f_both(int q) => f_g(0, q) + f_g(1, q)
f_row(int r, string name, int h, int n, string tip, color c) =>
f_cell(0, r, name, T_TEXT, tip)
f_cell(1, r, str.tostring(n), n >= 30 ? T_TEXT : T_MUT)
color bg = n > 0 ? ub_strength(100.0 * h / n, 20, 80) : T_ROW
panel.cell(2, r, f_rt(h, n), text_color = n > 0 ? ub_txtOn(bg) : T_MUT, bgcolor = bg, text_size = ub_size(dashSize), text_formatting = text.format_bold, tooltip = "Cell color: red = low, grey = 50%, green = high. Not a verdict.")
f_cell(3, r, f_ci(h, n), T_MUT, "Wilson 95% interval.")
f_cell(4, r, n < 30 ? "small sample" : "descriptive", n < 30 ? UB_AMBER : T_MUT)
if barstate.islast and showPanel
bool topSide = panelPos == "Top Right"
if na(panel)
panel := table.new(ub_pos(panelPos), 5, 12, bgcolor = T_BG, frame_color = T_FRAME, frame_width = 1, border_color = T_BORDER, border_width = 1)
panel.clear(0, 0, 4, 11)
int r = 0
if panelLift > 0 and topSide
// spacer: an empty-string row collapses, so the height is set in % of the chart
panel.cell(0, r, "", bgcolor = color.new(T_BG, 100), height = panelLift)
panel.merge_cells(0, r, 4, r)
r += 1
// header (same layout as ub_header, but on row r)
panel.cell(0, r, "UNBROKE", text_color = T_FRAME, bgcolor = T_HEAD, text_size = ub_size(dashSize), text_halign = text.align_left, text_formatting = text.format_bold)
panel.cell(1, r, "Trendline Breaks", text_color = T_TEXT, bgcolor = T_HEAD, text_size = ub_size(dashSize), text_halign = text.align_left, text_formatting = text.format_bold)
panel.cell(4, r, timeframe.period + " | " + str.tostring(hzBars) + " bars", text_color = T_MUT, bgcolor = T_HEAD, text_size = ub_size(dashSize), text_halign = text.align_right)
panel.merge_cells(1, r, 3, r)
r += 1
color hc = T_MUT
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, "Ran 1 ATR", text_color = hc, bgcolor = T_BG, text_size = ub_size(dashSize), tooltip = "Share that ran 1 ATR(14) their way before 1 ATR against, within the horizon.")
panel.cell(3, r, "95% CI", 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 bn = f_g(0, 0)
int bh = f_g(0, 1)
int rn = f_g(0, 3)
int rh = f_g(0, 4)
f_row(r, "Trendline breaks", bh, bn, "Close beyond the line, entry at that close. Loaded history before the forward date, which includes data seen while building the tool: descriptive only.", UB_SKY)
if not detailed
f_cell(4, r, "false " + f_rt(f_g(0, 2), bn), UB_AMBER, "False breaks: a bar closed back across the line before the 1 ATR run (descriptive). Detailed panel shows the CI.")
r += 1
f_row(r, "Random bars (same rule)", rh, rn, "Seeded random bars (" + str.tostring(pRand * 100, "0.0") + "% of bars, 50/50 long / short), scored with the same 1 ATR rule.", UB_NEUTRAL)
r += 1
if detailed
if bn > 0 and rn > 0
float p1 = bh / float(bn)
float p0 = rh / float(rn)
float dd = 100 * (p1 - p0)
float m = 196 * math.sqrt(p1 * (1 - p1) / bn + p0 * (1 - p0) / rn)
f_cell(0, r, "Difference (breaks - random)", T_TEXT, "Percentage points. Approximate normal interval, no day clustering; the registered interval comes from the mirror's CME-session bootstrap.")
f_cell(1, r, "", T_MUT)
f_cell(2, r, (dd >= 0 ? "+" : "") + str.tostring(dd, "0.0") + " pts", T_TEXT)
f_cell(3, r, str.tostring(dd - m, "0.0") + " to " + str.tostring(dd + m, "0.0"), T_MUT)
f_cell(4, r, "approx., no claim", T_MUT)
else
f_cell(0, r, "Difference (breaks - random)", T_TEXT)
panel.merge_cells(0, r, 4, r)
r += 1
f_cell(0, r, "False breaks", T_TEXT, "Share of breaks where a bar closed back across the line before the 1 ATR run.")
f_cell(1, r, str.tostring(bn), T_MUT)
f_cell(2, r, f_rt(f_g(0, 2), bn), UB_AMBER)
f_cell(3, r, f_ci(f_g(0, 2), bn), T_MUT)
f_cell(4, r, "descriptive", T_MUT)
r += 1
f_row(r, "Breaks up / random long", f_g(0, 6), f_g(0, 5), "Up breaks only. Random long: " + f_rt(f_g(0, 10), f_g(0, 9)) + " of " + str.tostring(f_g(0, 9)) + ".", UB_BULL)
r += 1
f_row(r, "Breaks down / random short", f_g(0, 8), f_g(0, 7), "Down breaks only. Random short: " + f_rt(f_g(0, 12), f_g(0, 11)) + " of " + str.tostring(f_g(0, 11)) + ".", UB_BEAR)
r += 1
f_cell(0, r, "swing " + str.tostring(pivLen) + "/" + str.tostring(pivLen) + " | slope <= " + str.tostring(slopeMax) + " ATR/bar | retire " + str.tostring(maxAge) + " bars | seed " + str.tostring(seed) + " | bar close only" + (showAnc ? " | dotted = before confirmation" : ""), T_MUT)
panel.merge_cells(0, r, 4, r)
r += 1
// forward: hidden until the registered read
int fn = f_g(1, 0)
int frn = f_g(1, 3)
string fTip = "Pre-registered forward test (prereg rev1/rev2): entries from the forward date on, read on the registered date only. This n counts only bars loaded on this chart; the registered count comes from the hashed data archive."
f_cell(0, r, "Forward (registered)", T_TEXT, fTip)
f_cell(1, r, str.tostring(fn), T_MUT)
if time < readT
f_cell(2, r, "hidden", T_MUT, fTip)
f_cell(3, r, "hidden", T_MUT, fTip)
f_cell(4, r, "recording n " + str.tostring(fn), UB_AMBER, fTip)
else
f_cell(2, r, f_rt(f_g(1, 1), fn), T_TEXT, fTip)
f_cell(3, r, "random " + f_rt(f_g(1, 4), frn), T_MUT, fTip)
f_cell(4, r, fn < 30 or frn < 30 ? "insufficient data" : "see mirror read", T_MUT, "The decision uses the mirror's bootstrap interval on the archive.")
r += 1
string disc = "auto trendlines 1.0.4 | Drawing aid: lines from confirmed swings. Not a signal. Educational, not financial advice. Past results don't predict future results."
if detailed
f_cell(0, r, disc, T_MUT)
panel.merge_cells(0, r, 4, r)
r += 1
// Visible disclosure in the default panel (two rows so it stays readable at panel text size).
panel.cell(0, r, "Drawing aid, not a signal. Educational, not financial advice.", text_color = T_MUT, bgcolor = T_BG, text_size = ub_size(dashSize), text_halign = text.align_left, tooltip = disc)
panel.merge_cells(0, r, 4, r)
r += 1
panel.cell(0, r, "Past results don't predict future results.", text_color = T_MUT, bgcolor = T_BG, text_size = ub_size(dashSize), text_halign = text.align_left, tooltip = disc)
panel.merge_cells(0, r, 4, r)
r += 1
if panelLift > 0 and not topSide
panel.cell(0, r, "", bgcolor = color.new(T_BG, 100), height = panelLift)
panel.merge_cells(0, r, 4, r)