本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (c) Unbroke - free & open-source. Not financial advice. For educational purposes only.
// ema trend cloud 1.0.4
//@version=6
indicator("Unbroke EMA Trend Cloud", shorttitle = "UB EMA Cld", overlay = true, max_labels_count = 500, max_bars_back = 500)
// A fast / slow EMA cloud colored by trend, a chop filter that mutes the cloud and its flip labels when volatility
// is low, and a 5-timeframe trend table. Proof panel: how often cloud flips (with and without the chop filter) and
// full timeframe agreement ran 1 ATR their way before 1 ATR against, next to seeded random bars scored the same way.
// Higher-timeframe values come from closed bars only ([1] + lookahead_on), so they do not repaint.
// Python port: tools/ema_trend_cloud_mirror.py. Spec: specs/ema_trend_cloud_spec.md. Original code.
//==============================================================================
// INPUTS
//==============================================================================
G_EMA = "1. Cloud"
fastLen = input.int(21, "Fast EMA", minval = 2, maxval = 400, group = G_EMA, tooltip = "Fast EMA length. The cloud is green when the fast EMA is above the slow EMA, red when below.", display = display.none)
slowLen = input.int(50, "Slow EMA", minval = 3, maxval = 500, group = G_EMA, tooltip = "Slow EMA length. A flip is the bar close where the fast EMA crosses the slow EMA.", display = display.none)
G_CHP = "2. Chop filter"
chopOn = input.bool(true, "Use chop filter", group = G_CHP, tooltip = "When on, flips that happen while the market is quiet are greyed out and get no label or alert. Quiet = ATR(14) below the ratio x ATR(100).", display = display.none)
chopK = input.float(0.8, "Quiet when ATR(14) / ATR(100) below", minval = 0.1, maxval = 2.0, step = 0.05, group = G_CHP, tooltip = "Default 0.8: the last 14 bars move less than 80% of the usual 100-bar range.", display = display.none)
G_MTF = "3. Timeframe table"
showMtf = input.bool(true, "Show timeframe table", inline = "mt", group = G_MTF, tooltip = "Trend of the same two EMAs on 5 timeframes, from each timeframe's last CLOSED bar (no repaint).", display = display.none)
mtfPos = input.string("Middle Right", "", options = ["Middle Right", "Top Right", "Bottom Right", "Top Left", "Middle Left"], inline = "mt", group = G_MTF, display = display.none)
tf1 = input.timeframe("5", "Timeframe 1", group = G_MTF, tooltip = "A timeframe below the chart timeframe shows 'below chart' and is left out of the agreement count.", display = display.none)
tf2 = input.timeframe("15", "Timeframe 2", group = G_MTF, display = display.none)
tf3 = input.timeframe("60", "Timeframe 3", group = G_MTF, display = display.none)
tf4 = input.timeframe("240", "Timeframe 4", group = G_MTF, display = display.none)
tf5 = input.timeframe("D", "Timeframe 5", group = G_MTF, display = display.none)
G_PRF = "4. Proof panel"
showPanel = input.bool(true, "Show Proof panel", inline = "pp", group = G_PRF, tooltip = "How often each signal ran 1 ATR its way before 1 ATR against, compared with random bars.", display = display.none)
panelPos = input.string("Top Right", "", options = ["Top Right", "Bottom Left", "Middle Left", "Top Left", "Bottom Right", "Middle Right"], inline = "pp", group = G_PRF, display = display.none)
hzBars = input.int(24, "Horizon (bars)", minval = 2, maxval = 500, group = G_PRF, tooltip = "A signal is a hit when price runs 1 ATR(14) its way before 1 ATR against within this many bars, measured from the signal bar close. Both on one bar = against.", display = display.none)
minN = input.int(20, "Minimum cases for a verdict", minval = 5, maxval = 500, group = G_PRF, 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 that get a random entry, 50/50 long or short, seeded so it is the same every time.", display = display.none)
seed = input.int(7, "Random seed", group = G_PRF, display = display.none)
G_DSP = "5. 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 the fast EMA", group = G_DSP, display = display.none)
candleOn = input.bool(false, "Trend-colored candles", group = G_DSP, display = display.none)
showLbl = input.bool(true, "Flip markers", group = G_DSP, tooltip = "Small triangles on flips outside chop. Hover a marker for the details.", display = display.none)
lblGap = input.int(10, "Min bars between markers", minval = 0, maxval = 200, group = G_DSP, tooltip = "Display only: a flip within this many bars of the last drawn marker is not drawn, so choppy stretches stay readable. It still counts in the Proof panel and still fires its alert.", display = display.none)
showMute = input.bool(false, "Mark muted flips (small x)", group = G_DSP, tooltip = "Shows a small grey x where a flip happened during chop.", display = display.none)
dashSize = input.string("Small", "Table text size", options = ["Tiny", "Small", "Normal"], group = G_DSP, display = display.none)
xpOn = input.bool(false, "Export mode (port check)", group = G_DSP, tooltip = "Adds 'ETC XP' columns to the Data Window and Export chart data, for tools/ema_trend_cloud_mirror.py.", display = display.none)
//==============================================================================
// UNBROKE STYLE KIT for Pine v6 (style kit 1.3, pasted unchanged below the header). Original code. ASCII only.
//==============================================================================
// ---- 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 or ub_lum(chart.fg_color) < 100
bool UB_LIGHT = ubThemeMode == "Light" or (ubThemeMode == "Auto" and ub_isLight())
color T_BG = color.new(UB_LIGHT ? UB_PAPER : ubThemeMode == "Auto" ? chart.bg_color : 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
// Verdict vs random: two-sample 95% margin (normal approximation).
// Returns [text, color]. Below minN samples on either side -> "Not enough data".
ub_verdict(float rate, int n, float base, int nBase, int minN) =>
string txt = "Not enough data"
color col = UB_LIGHT ? color.from_gradient(45, 0, 100, T_MUTED, UB_INK) : T_MUTED
if n >= minN and nBase >= minN and not na(rate) and not na(base)
float p1 = rate / 100.0
float p0 = base / 100.0
float m = 196.0 * math.sqrt(p1 * (1 - p1) / n + p0 * (1 - p0) / nBase)
float edge = rate - base
if edge > m
txt := "Beats random"
col := UB_LIGHT ? color.from_gradient(45, 0, 100, UB_BULL, UB_INK) : UB_BULL
else if edge < -m
txt := "Worse than random"
col := UB_LIGHT ? color.from_gradient(35, 0, 100, UB_BEAR, UB_INK) : UB_BEAR
else
txt := "Inside noise"
col := UB_LIGHT ? color.from_gradient(55, 0, 100, UB_AMBER, UB_INK) : UB_AMBER
[txt, col]
// 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)
// One proof row. hits/n = this signal, baseRate/nBase = the random baseline.
ub_proofRow(table t, int row, string name, color c, int hits, int n, float baseRate, int nBase, int minN, tsz) =>
float rate = ub_pct(hits, n)
[vTxt, vCol] = ub_verdict(rate, n, baseRate, nBase, minN)
float edge = na(rate) or na(baseRate) ? na : rate - baseRate
string eTxt = na(edge) ? "-" : (edge >= 0 ? "+" : "") + str.tostring(edge, "#") + " pts"
table.cell(t, 0, row, name, text_color = T_TEXT, bgcolor = color.from_gradient(UB_LIGHT ? 84 : 55, 0, 100, color.new(c, 0), T_ROW), text_size = tsz, text_halign = text.align_left)
table.cell(t, 1, row, str.tostring(n), text_color = n >= minN ? T_TEXT : T_MUTED, bgcolor = T_ROW, text_size = tsz)
color hitBg = na(rate) ? T_ROW : UB_LIGHT ? color.from_gradient(45, 0, 100, ub_strength(rate, 0, 100), T_ROW) : ub_strength(rate, 0, 100)
table.cell(t, 2, row, na(rate) ? "-" : str.tostring(rate, "#") + "%", text_color = na(rate) ? T_MUTED : ub_txtOn(hitBg), bgcolor = hitBg, text_size = tsz, text_formatting = text.format_bold, tooltip = "Hit rate. Cell color: red = low, grey = 50%, green = high.")
table.cell(t, 3, row, eTxt, text_color = na(edge) ? T_MUTED : edge >= 0 ? UB_BULL : UB_BEAR, bgcolor = T_ROW, text_size = tsz)
table.cell(t, 4, row, vTxt, text_color = vCol, bgcolor = T_ROW, text_size = tsz, text_formatting = text.format_bold, tooltip = "95% margin test vs the random baseline. Needs at least " + str.tostring(minN) + " samples on both sides.")
//==============================================================================
// CORE SERIES (all ta.* calls at global scope)
//==============================================================================
float eF = ta.ema(close, fastLen)
float eS = ta.ema(close, slowLen)
float A = ta.atr(14)
float AL = ta.atr(100)
int WARM = 300
bool ok = bar_index >= WARM and not na(A) and not na(AL)
int tr = na(eF) or na(eS) ? 0 : eF > eS ? 1 : -1
float chopR = na(AL) or AL == 0 ? na : A / AL
bool quiet = chopOn and not na(chopR) and chopR < chopK
color C_UP = UB_LIGHT ? #0FA86E : #2EF0A6
color C_DN = UB_LIGHT ? #E0352B : #FF5C52
// Trend and EMA spread (in ATR) of a timeframe, from its last closed bar.
float spr = na(A) or A == 0 ? na : (eF - eS) / A
f_req(string tf) =>
request.security(syminfo.tickerid, tf, [tr[1], spr[1]], lookahead = barmerge.lookahead_on)
[h1, s1] = f_req(tf1)
[h2, s2] = f_req(tf2)
[h3, s3] = f_req(tf3)
[h4, s4] = f_req(tf4)
[h5, s5] = f_req(tf5)
int chartSec = timeframe.in_seconds()
// state: 2 = below chart TF (not counted), else trend -1 / 0 / 1. Same TF as chart = the chart's own bar-close trend.
f_state(string tf, int hTr) =>
int sec = timeframe.in_seconds(tf)
sec < chartSec ? 2 : sec == chartSec ? tr : nz(hTr)
f_sp(string tf, float hSp) =>
timeframe.in_seconds(tf) == chartSec ? spr : hSp
array<string> tfs = array.from(tf1, tf2, tf3, tf4, tf5)
array<int> st = array.from(f_state(tf1, h1), f_state(tf2, h2), f_state(tf3, h3), f_state(tf4, h4), f_state(tf5, h5))
array<float> sp = array.from(f_sp(tf1, s1), f_sp(tf2, s2), f_sp(tf3, s3), f_sp(tf4, s4), f_sp(tf5, s5))
int nUp = 0
int nDn = 0
int nVal = 0
for i = 0 to 4
int v = st.get(i)
if v != 2
nVal += 1
nUp += v == 1 ? 1 : 0
nDn += v == -1 ? 1 : 0
int align = nVal >= 3 and nUp == nVal ? 1 : nVal >= 3 and nDn == nVal ? -1 : 0 // every counted timeframe agrees (needs 3+)
var int alignPrev = 0
//==============================================================================
// PROOF ENGINE: pending entries scored from the next bar on
//==============================================================================
// kinds: 0 random, 1 all flips (plain EMA cross), 2 flips outside chop (shown), 3 flips inside chop (muted), 4 new full agreement
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<int> pK = array.new<int>()
var array<int> cN = array.new<int>(5, 0)
var array<int> cH = array.new<int>(5, 0)
array<int> xr = array.new<int>(2, 0) // resolved, hits on this bar (all kinds)
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)
if pI.size() > 0
for j = pI.size() - 1 to 0
int d = pD.get(j)
float cE = pC.get(j)
float aE = pA.get(j)
bool stp = d > 0 ? low <= cE - aE : high >= cE + aE
bool run = d > 0 ? high >= cE + aE : low <= cE - aE
int dec = stp ? 0 : run ? 1 : bar_index - pI.get(j) >= hzBars ? 0 : -1
if dec >= 0
int k = pK.get(j)
cN.set(k, cN.get(k) + 1)
cH.set(k, cH.get(k) + dec)
xr.set(0, xr.get(0) + 1)
xr.set(1, xr.get(1) + dec)
pI.remove(j)
pD.remove(j)
pC.remove(j)
pA.remove(j)
pK.remove(j)
f_pend(int d, int k) =>
pI.push(bar_index)
pD.push(d)
pC.push(close)
pA.push(A)
pK.push(k)
bool flip = tr != 0 and tr[1] != 0 and tr != tr[1]
bool flipShow = flip and not quiet
bool flipMute = flip and quiet
bool alignNew = align != 0 and align != alignPrev
int xRnd = 0
if ok
if flip
f_pend(tr, 1)
f_pend(tr, quiet ? 3 : 2)
if alignNew
f_pend(align, 4)
if f_hash(0.0) < pRand
xRnd := f_hash(78.233) < 0.5 ? 1 : -1
f_pend(xRnd, 0)
alignPrev := align
//==============================================================================
// DRAWING: gradient cloud, glow on the fast EMA, flip labels
//==============================================================================
color cBase = quiet ? UB_NEUTRAL : tr == 1 ? C_UP : tr == -1 ? C_DN : UB_NEUTRAL
int gT1 = UB_LIGHT ? 82 : 78
int gT2 = UB_LIGHT ? 64 : 58
plot(glowOn ? eF : na, "Glow wide", color.new(cBase, gT1), 8, display = display.pane)
plot(glowOn ? eF : na, "Glow", color.new(cBase, gT2), 4, display = display.pane)
pF = plot(eF, "Fast EMA", color.new(cBase, 0), 2)
pS = plot(eS, "Slow EMA", color.new(cBase, 40), 1)
float cTop = math.max(eF, eS)
float cBot = math.min(eF, eS)
int fEdge = quiet ? (UB_LIGHT ? 82 : 88) : UB_LIGHT ? 50 : 58
fill(pF, pS, cTop, cBot, color.new(cBase, tr == 1 ? fEdge : UB_LIGHT ? 88 : 94), color.new(cBase, tr == 1 ? UB_LIGHT ? 88 : 94 : fEdge), "Cloud")
barcolor(candleOn ? (quiet ? UB_NEUTRAL : tr == 1 ? C_UP : tr == -1 ? C_DN : na) : na, title = "Trend candles")
plotshape(showMute and flipMute and barstate.isconfirmed, "Muted flip", shape.xcross, location.abovebar, color.new(UB_NEUTRAL, 20), size = size.tiny, display = display.pane)
var int lastLbl = -100000
if showLbl and flipShow and barstate.isconfirmed and bar_index - lastLbl > lblGap
lastLbl := bar_index
float off = 0.5 * nz(A, high - low)
color lc = tr == 1 ? C_UP : C_DN
label.new(bar_index, tr == 1 ? low - off : high + off, "", style = tr == 1 ? label.style_triangleup : label.style_triangledown, color = color.new(lc, 0), textcolor = lc, size = size.tiny, tooltip = (tr == 1 ? "Flip UP" : "Flip DOWN") + ": fast EMA crossed the slow EMA at this close, outside chop. ATR(14)/ATR(100) = " + str.tostring(chopR, "0.00") + (ok ? "" : " (warm-up: not counted)"))
alertcondition(flipShow and tr == 1, "EMA Cloud flip up", "Unbroke EMA Trend Cloud: flip up at bar close (outside chop)")
alertcondition(flipShow and tr == -1, "EMA Cloud flip down", "Unbroke EMA Trend Cloud: flip down at bar close (outside chop)")
alertcondition(alignNew and align == 1, "All timeframes up", "Unbroke EMA Trend Cloud: every counted timeframe now trends up")
alertcondition(alignNew and align == -1, "All timeframes down", "Unbroke EMA Trend Cloud: every counted timeframe now trends down")
alertcondition(quiet and not quiet[1], "Chop filter on", "Unbroke EMA Trend Cloud: market turned quiet, flips muted")
// Export columns (per-bar values only)
plot(xpOn ? 100 : na, "ETC XP ver", display = display.data_window)
plot(xpOn ? tr : na, "ETC XP trend", display = display.data_window)
plot(xpOn ? (flip ? tr : 0) : na, "ETC XP flip", display = display.data_window)
plot(xpOn ? (quiet ? 1 : 0) : na, "ETC XP quiet", display = display.data_window)
plot(xpOn ? align : na, "ETC XP align", display = display.data_window)
plot(xpOn ? xRnd : na, "ETC XP rnd", display = display.data_window)
plot(xpOn ? xr.get(0) : na, "ETC XP res", display = display.data_window)
plot(xpOn ? xr.get(1) : na, "ETC XP hit", display = display.data_window)
//==============================================================================
// TIMEFRAME TABLE
//==============================================================================
var table mtf = na
f_tfName(string tf) =>
tf == "D" or tf == "1D" ? "1D" : tf == "W" or tf == "1W" ? "1W" : tf == "M" or tf == "1M" ? "1M" : timeframe.in_seconds(tf) >= 3600 and timeframe.in_seconds(tf) % 3600 == 0 ? str.tostring(timeframe.in_seconds(tf) / 3600) + "h" : timeframe.in_seconds(tf) < 60 ? tf : str.tostring(timeframe.in_seconds(tf) / 60) + "m"
if barstate.islast and showMtf
if na(mtf)
mtf := table.new(ub_pos(mtfPos), 3, 9, bgcolor = na, frame_color = T_FRAME, frame_width = 1, border_color = T_BORDER, border_width = 1)
mtf.set_frame_color(T_FRAME)
mtf.set_border_color(T_BORDER)
mtf.clear(0, 0, 2, 8)
tsz = ub_size(dashSize)
ub_header(mtf, 3, "EMA Trend", str.tostring(fastLen) + "/" + str.tostring(slowLen), tsz)
mtf.cell(0, 1, "Timeframe", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz, text_halign = text.align_left)
mtf.cell(1, 1, "Trend", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz)
mtf.cell(2, 1, "Spread", text_color = T_MUTED, bgcolor = T_BG, text_size = tsz, tooltip = "Fast EMA minus slow EMA, in ATR(14) of that timeframe, at its last closed bar.")
for i = 0 to 4
int v = st.get(i)
float s = sp.get(i)
color c = v == 1 ? C_UP : v == -1 ? C_DN : T_ROW
string tx = v == 2 ? "below chart" : v == 1 ? "UP" : v == -1 ? "DOWN" : "-"
mtf.cell(0, 2 + i, f_tfName(tfs.get(i)), text_color = T_TEXT, bgcolor = T_ROW, text_size = tsz, text_halign = text.align_left)
mtf.cell(1, 2 + i, tx, text_color = v == 1 or v == -1 ? ub_txtOn(c) : T_MUTED, bgcolor = c, text_size = tsz, text_formatting = text.format_bold)
mtf.cell(2, 2 + i, v == 2 or na(s) ? "-" : (s >= 0 ? "+" : "") + str.tostring(s, "0.00"), text_color = T_TEXT, bgcolor = T_ROW, text_size = tsz)
color ac = nVal == 0 ? T_ROW : ub_strength(nUp - nDn, -nVal, nVal)
mtf.cell(0, 7, "Agreement", text_color = T_TEXT, bgcolor = T_ROW, text_size = tsz, text_halign = text.align_left)
mtf.cell(1, 7, nUp >= nDn ? str.tostring(nUp) + "/" + str.tostring(nVal) + " up" : str.tostring(nDn) + "/" + str.tostring(nVal) + " down", text_color = ub_txtOn(ac), bgcolor = ac, text_size = tsz, text_formatting = text.format_bold)
mtf.cell(2, 7, align != 0 ? "all agree" : "mixed", text_color = align != 0 ? T_TEXT : T_MUTED, bgcolor = T_ROW, text_size = tsz)
mtf.cell(0, 8, "Chop filter", text_color = T_TEXT, bgcolor = T_ROW, text_size = tsz, text_halign = text.align_left)
mtf.cell(1, 8, not chopOn ? "off" : quiet ? "QUIET" : "active", text_color = quiet ? UB_AMBER : T_MUTED, bgcolor = T_ROW, text_size = tsz, text_formatting = text.format_bold)
mtf.cell(2, 8, na(chopR) ? "-" : str.tostring(chopR, "0.00") + "x", text_color = T_MUTED, bgcolor = T_ROW, text_size = tsz, tooltip = "ATR(14) / ATR(100). Quiet below " + str.tostring(chopK, "0.00") + ".")
//==============================================================================
// PROOF PANEL
//==============================================================================
var table panel = na
if barstate.islast and showPanel
if na(panel)
panel := table.new(ub_pos(panelPos), 5, 10, bgcolor = na, frame_color = T_FRAME, frame_width = 1, border_color = T_BORDER, border_width = 1)
panel.set_frame_color(T_FRAME)
panel.set_border_color(T_BORDER)
panel.clear(0, 0, 4, 9)
tsz = ub_size(dashSize)
ub_header(panel, 5, "EMA Cloud Proof", timeframe.period + " | " + str.tostring(hzBars) + " bars", tsz)
ub_proofHead(panel, 1, "Signal (history)", tsz)
float base = ub_pct(cH.get(0), cN.get(0))
int nB = cN.get(0)
panel.cell(0, 2, "Random bars", text_color = T_TEXT, bgcolor = T_ROW, text_size = tsz, text_halign = text.align_left, tooltip = "Seeded random bars, " + str.tostring(pRand * 100, "0.0") + "% of bars, 50/50 long or short, scored with the same rule. This is the line to beat.")
panel.cell(1, 2, str.tostring(nB), text_color = T_TEXT, bgcolor = T_ROW, text_size = tsz)
panel.cell(2, 2, na(base) ? "-" : str.tostring(base, "#") + "%", text_color = T_TEXT, bgcolor = T_ROW, text_size = tsz, text_formatting = text.format_bold)
panel.cell(3, 2, "baseline", text_color = T_MUTED, bgcolor = T_ROW, text_size = tsz)
panel.cell(4, 2, "-", text_color = T_MUTED, bgcolor = T_ROW, text_size = tsz)
ub_proofRow(panel, 3, "Flips, chop filter on", UB_GREEN, cH.get(2), cN.get(2), base, nB, minN, tsz)
ub_proofRow(panel, 4, "All flips (plain cross)", UB_SKY, cH.get(1), cN.get(1), base, nB, minN, tsz)
ub_proofRow(panel, 5, "Flips in chop (muted)", UB_NEUTRAL, cH.get(3), cN.get(3), base, nB, minN, tsz)
ub_proofRow(panel, 6, "New full agreement", UB_VIOLET, cH.get(4), cN.get(4), base, nB, minN, tsz)
ub_footer(panel, 5, 7, "Hit = 1 ATR(14) its way before 1 ATR against within " + str.tostring(hzBars) + " bars. Closed bars, loaded history only.")
ub_footer(panel, 5, 8, "Trend tool, not a signal service. Past results do not guarantee future results.")