本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Expected Move Cone + Range Forecast [Rider Algo]", shorttitle="Expected Move Cone + Range Forecast [Rider Algo]", overlay=true, max_lines_count=10, max_labels_count=60, max_polylines_count=60)
// Developed by Rider Algo
//
// How far can price travel in the next candles, up or down? The cone answers it from this chart's own history.
//
// How it works (validated 2026-10-18 on 24 assets: 12 cryptos, 9 stocks and ETFs, gold, oil and EURUSD; 1H, 4H, 1D
// and 1W; every forecast made only with the data known at that close):
// 1. Volatility = a 50/50 blend of a fast and a slow average of squared returns (EWMA 0.94 and 0.99). The fast one
// reacts to the last days, the slow one remembers the normal level. A fast-only measure (or ATR 14) gave cones
// too narrow in calm markets and too wide in wild ones; the blend kept its promise in calm, normal and wild
// markets alike.
// 2. Every past close is turned into "how many volatility units did price move N candles later". The bands are
// percentiles of those moves (68% and 95% by default), so the cone carries this asset's real fat tails instead
// of a bell curve. A classic bell-curve cone broke its 95% edge far more often than the 5% it promised.
// 3. Up and down are mirrored: the cone measures HOW MUCH price moves, never WHICH WAY.
// 4. Range forecast: the same idea applied to the distance between the highest high and the lowest low of the
// next N candles.
// Everything starts from the last CLOSED candle: the cone stays fixed while a candle is forming and never repaints.
//──────────────────────────────────────────────
// QUICK GUIDE
//──────────────────────────────────────────────
groupGuide = "Quick Guide"
quickGuide = input.string("Hover the (i) on the right", "How to read it", group=groupGuide, display=display.none, tooltip="1. The cone starts at the last closed candle and opens to the right: how far price usually travels over the next candles, up or down.\n2. Inner band (1σ): the close stayed inside it 68% of the time. Outer band (2σ): 95%. A close beyond the outer band is an unusual move.\n3. The badge shows the expected move in % and the range forecast: how far apart the highest high and the lowest low of those candles usually are.\n4. The faded cones behind the price are past cones, made with only the data known at the time. Dot at the end: green = ended inside 1σ, amber = inside 2σ, red = broke out.\n5. The table shows how often the bands held on THIS chart, with a verdict.\nUse it to size positions, put stops outside the normal noise, set realistic targets and pick option strikes. It does not predict direction.")
//──────────────────────────────────────────────
// 1 · CONE
//──────────────────────────────────────────────
groupCone = "1 · Cone"
hzMode = input.string("Auto", "Candles ahead", options=["Auto", "Custom"], group=groupCone, inline="hz", tooltip="Auto: one period of the next timeframe up, counted on this chart. 1D → one week (7 candles on crypto, 5 on stocks), 4H → one day on crypto or one week on stocks, 1H → one day, 1W → one month, 15m → 4 hours, 5m → 1 hour.\nCustom: the number on the right (2 to 60).")
hzCustom = input.int(10, "", minval=2, maxval=60, group=groupCone, inline="hz")
pInIn = input.int(68, "Inner band (%)", minval=20, maxval=90, group=groupCone, tooltip="How often the close stayed inside the band. 68% is the classic 1σ expected move used by options traders.")
pOutIn = input.int(95, "Outer band (%)", minval=50, maxval=99, group=groupCone, tooltip="95% is the classic 2σ move: a close beyond it happens about 1 time in 20. Always kept wider than the inner band.")
lookback = input.int(2000, "History to learn from (candles)", minval=300, maxval=4500, step=100, group=groupCone, tooltip="How many past closes shape the bands. If the chart has fewer, it uses all it has. The cone needs about 320 candles to start.")
//──────────────────────────────────────────────
// 2 · LOOK
//──────────────────────────────────────────────
groupLook = "2 · Look"
showOuter = input.bool(true, "Show outer band", group=groupLook)
showLbls = input.bool(true, "Show price labels", group=groupLook, tooltip="Price and % change at the end of each band.")
showBadge = input.bool(true, "Show expected move badge", group=groupLook)
upCol = input.color(#1be5a4, "Colors: Up", group=groupLook, inline="col")
dnCol = input.color(#f4362a, "Down", group=groupLook, inline="col")
//──────────────────────────────────────────────
// 3 · PAST CONES
//──────────────────────────────────────────────
groupPast = "3 · Past cones"
showPast = input.bool(true, "Show past cones", group=groupPast, tooltip="Every N candles, the cone made at that close (with only the data known then) stays on the chart, so you can see for yourself how often price stayed inside.\nDot at the end: green = the close ended inside the inner band, amber = inside the outer band, red = broke out.")
pastN = input.int(8, "How many", minval=1, maxval=20, group=groupPast)
//──────────────────────────────────────────────
// 4 · TABLE
//──────────────────────────────────────────────
groupTb = "4 · Table"
showTable = input.bool(true, "Show table", group=groupTb)
tblPosIn = input.string("Top Right", "Position", options=["Top Right", "Middle Right", "Bottom Right", "Top Left", "Bottom Left"], group=groupTb, inline="tb")
tblSizeIn = input.string("Small", "Size", options=["Tiny", "Small", "Normal"], group=groupTb, inline="tb")
trackN = input.int(0, "Judge the last N candles (0 = all)", minval=0, maxval=20000, step=100, group=groupTb, tooltip="Every closed candle's cone is checked N candles later: did the close end inside each band? 0 = judge every cone on the chart.")
minJudged = input.int(100, "Cones needed for a verdict", minval=20, maxval=5000, group=groupTb, tooltip="Below this many judged cones the table says NOT ENOUGH DATA.")
tolIn = input.float(5.0, "Reliable within ± points: inner", minval=0.5, maxval=20, step=0.5, group=groupTb, inline="tol")
tolOut = input.float(2.5, "outer", minval=0.5, maxval=10, step=0.5, group=groupTb, inline="tol", tooltip="RELIABLE = the band held within this many points of its promise (with the defaults, 63-73% for the 68% band and 92.5-97.5% for the 95% band). MIXED = within twice that. NOT RELIABLE = further away: this market is moving more (or less) than its own history says.")
tblSize = tblSizeIn == "Tiny" ? size.tiny : tblSizeIn == "Normal" ? size.normal : size.small
tblPos = switch tblPosIn
"Top Right" => position.top_right
"Middle Right" => position.middle_right
"Bottom Right" => position.bottom_right
"Top Left" => position.top_left
"Bottom Left" => position.bottom_left
//──────────────────────────────────────────────
// CONSTANTS
//──────────────────────────────────────────────
HMAX = 60 // most candles a cone can project
WARM = 100 // returns needed before the first close is stored (the slow volatility settles)
MINN = 200 // past moves needed to draw a band
SEED = 60 // returns averaged to start both volatility averages
LAM_F = 0.94 // fast volatility memory
LAM_S = 0.99 // slow volatility memory
pIn = pInIn / 100.0
pOut = math.max(pOutIn, pInIn + 1) / 100.0
nmIn = pInIn == 68 ? "1σ" : str.tostring(pInIn) + "%"
nmOut = math.max(pOutIn, pInIn + 1) == 95 ? "2σ" : str.tostring(math.max(pOutIn, pInIn + 1)) + "%"
C_IN = #1be5a4 // past cone ended inside the inner band
C_MID = #ffb74d // inside the outer band
C_OUT = #f4362a // broke out
//──────────────────────────────────────────────
// VOLATILITY (blend of a fast and a slow EWMA of squared log returns)
//──────────────────────────────────────────────
logC = math.log(close)
ret = logC - logC[1]
var float vF = na
var float vS = na
var int nR = 0
var float sR = 0.0
if not na(ret)
float r2 = ret * ret
nR += 1
if nR <= SEED
sR += r2
vF := sR / nR
vS := sR / nR
else
vF := LAM_F * vF + (1 - LAM_F) * r2
vS := LAM_S * vS + (1 - LAM_S) * r2
// Never below one tick: flat candles in old history would otherwise blow up the moves measured in volatility units
sigma = nR >= WARM and close > 0 ? math.max(math.sqrt(0.5 * vF + 0.5 * vS), syminfo.mintick / close) : na
sigOk = not na(sigma)
volRank = ta.percentrank(sigma, 365)
sqz = volRank <= 10
sqzNew = sqz and not sqz[1]
//──────────────────────────────────────────────
// AUTO HORIZON: one period of the next timeframe up, counted on this chart
//──────────────────────────────────────────────
var array<string> P_TF = array.from("60", "240", "1D", "1W", "1M", "3M", "12M")
var array<string> P_NAME = array.from("hour", "4 hours", "day", "week", "month", "quarter", "year")
ch0 = timeframe.change("60")
ch1 = timeframe.change("240")
ch2 = timeframe.change("1D")
ch3 = timeframe.change("1W")
ch4 = timeframe.change("1M")
ch5 = timeframe.change("3M")
ch6 = timeframe.change("12M")
// Candles in each of the last 6 completed periods, per candidate timeframe
var matrix<int> ring = matrix.new<int>(7, 6, 0)
var array<int> cnt = array.new<int>(7, 0)
var array<int> done = array.new<int>(7, 0)
f_count(int i, bool ch) =>
if ch and cnt.get(i) > 0
ring.set(i, done.get(i) % 6, cnt.get(i))
done.set(i, done.get(i) + 1)
cnt.set(i, 0)
cnt.set(i, cnt.get(i) + 1)
true
if barstate.isconfirmed
f_count(0, ch0)
f_count(1, ch1)
f_count(2, ch2)
f_count(3, ch3)
f_count(4, ch4)
f_count(5, ch5)
f_count(6, ch6)
// First period longer than one candle that holds 5 to 60 candles (the most seen in its last 6 periods)
chartSec = timeframe.in_seconds()
int hAuto = na
string hName = ""
for i = 0 to 6
if na(hAuto) and timeframe.in_seconds(P_TF.get(i)) > chartSec
int mx = 0
for j = 0 to 5
mx := math.max(mx, ring.get(i, j))
if mx >= 5 and mx <= HMAX
hAuto := mx
hName := P_NAME.get(i)
autoOk = hzMode == "Auto" and not na(hAuto)
H = hzMode == "Auto" ? (na(hAuto) ? 10 : hAuto) : hzCustom
hTxt = autoOk ? "next " + hName : "next " + str.tostring(H) + " candles"
//──────────────────────────────────────────────
// MEMORY OF CLOSED CANDLES
//──────────────────────────────────────────────
// The engine reads these arrays instead of using [] on series: the cone is only computed on the last candles, and
// inside a function [] walks back through the function's own calls, not through the chart (warning CW10003).
var array<float> aLc = array.new<float>() // log close
var array<float> aLh = array.new<float>() // log high
var array<float> aLl = array.new<float>() // log low
var array<float> aSig = array.new<float>() // volatility at that close
var array<float> aQi = array.new<float>() // inner and outer band at H candles known at that close (track record)
var array<float> aQo = array.new<float>()
var array<float> aQ1 = array.new<float>() // outer band for one candle (unusual-candle alert)
var array<float> sw = array.new<float>() // moves at H candles of the last `lookback` closes, sorted
var array<float> fw = array.new<float>() // the same moves in arrival order, to know which one leaves
var array<float> sw1 = array.new<float>() // same for one candle
var array<float> fw1 = array.new<float>()
var int lastBar = -1 // bar_index of the newest stored close
CAP = lookback + (pastN + 2) * HMAX + 20
JCAP = trackN > 0 ? trackN : 90000 // arrays hold at most 100,000 items
f_idx(int b) => aLc.size() - 1 - (lastBar - b) // array position of the close with bar_index b
// Move from close s to close s + h, in volatility units, up or down alike
f_z(int s, int h) => math.abs(aLc.get(s + h) - aLc.get(s)) / (aSig.get(s) * math.sqrt(h))
// First position whose value is >= v (array sorted ascending)
f_lower(array<float> a, float v) =>
int lo = 0
int hi = a.size()
while lo < hi
int mid = int(math.floor((lo + hi) / 2))
if a.get(mid) < v
lo := mid + 1
else
hi := mid
lo
// Adds a move to a sorted window and drops the oldest one when the window is full
f_insert(array<float> s, array<float> f, float v, int cap) =>
s.insert(f_lower(s, v), v)
f.push(v)
if f.size() > cap
float old = f.shift()
int p = f_lower(s, old)
if p < s.size()
s.remove(p)
true
// Percentile of an ascending array, linear interpolation
f_quant(array<float> sorted, float p) =>
int n = sorted.size()
float pos = p * (n - 1)
int lo = int(math.floor(pos))
int hi = math.min(lo + 1, n - 1)
sorted.get(lo) + (sorted.get(hi) - sorted.get(lo)) * (pos - lo)
//──────────────────────────────────────────────
// STORE EACH CLOSED CANDLE AND JUDGE THE CONE MADE H CANDLES AGO
//──────────────────────────────────────────────
var int builtH = -1
var int validFrom = 0 // first close whose stored bands belong to the current horizon
var float rankNow = na
var array<int> jRes = array.new<int>() // 2 = ended inside the inner band, 1 = inside the outer band, 0 = broke out
var int jIn = 0
var int jOut = 0
if barstate.isconfirmed and sigOk
aLc.push(logC)
aLh.push(math.log(high))
aLl.push(math.log(math.max(low, syminfo.mintick)))
aSig.push(sigma)
lastBar := bar_index
rankNow := volRank
int k = aLc.size() - 1
if H != builtH
// New horizon: rebuild the window and restart the track record
sw.clear()
fw.clear()
int sTo = k - H
if sTo >= 0
for s = math.max(0, sTo - lookback + 1) to sTo
f_insert(sw, fw, f_z(s, H), lookback)
builtH := H
validFrom := bar_index
jRes.clear()
jIn := 0
jOut := 0
else if k - H >= 0
int a = k - H
float zz = f_z(a, H)
float qi = aQi.get(a)
float qo = aQo.get(a)
if not na(qi) and bar_index - H >= validFrom
int res = zz <= qi ? 2 : zz <= qo ? 1 : 0
jRes.push(res)
jIn += res == 2 ? 1 : 0
jOut += res >= 1 ? 1 : 0
if jRes.size() > JCAP
int old = jRes.shift()
jIn -= old == 2 ? 1 : 0
jOut -= old >= 1 ? 1 : 0
f_insert(sw, fw, zz, lookback)
if k >= 1
f_insert(sw1, fw1, f_z(k - 1, 1), lookback)
aQi.push(sw.size() >= MINN ? f_quant(sw, pIn) : na)
aQo.push(sw.size() >= MINN ? f_quant(sw, pOut) : na)
aQ1.push(sw1.size() >= MINN ? f_quant(sw1, pOut) : na)
if aLc.size() > CAP
aLc.shift()
aLh.shift()
aLl.shift()
aSig.shift()
aQi.shift()
aQo.shift()
aQ1.shift()
//──────────────────────────────────────────────
// CONE ENGINE
//──────────────────────────────────────────────
// Half-width of each band (log units) at every candle 0..h0 for the close at position k. The percentiles are exact at
// up to K horizons and linear in between; a band is never narrower than at the candle before.
f_curve(int k, int h0, int K) =>
array<int> hs = array.from(1)
for j = 1 to K
int hh = math.max(1, math.min(h0, math.round(j * h0 * 1.0 / K)))
if hh > hs.last()
hs.push(hh)
array<float> sI = array.new<float>()
array<float> sO = array.new<float>()
bool ok = true
for hh in hs
int sTo = k - hh
int sFrom = math.max(0, sTo - lookback + 1)
if sTo - sFrom + 1 < MINN
ok := false
else
array<float> zz = array.new<float>()
for s = sFrom to sTo
zz.push(f_z(s, hh))
zz.sort()
sI.push(f_quant(zz, pIn))
sO.push(f_quant(zz, pOut))
array<float> wI = array.new<float>(h0 + 1, 0.0)
array<float> wO = array.new<float>(h0 + 1, 0.0)
if ok
float sg = aSig.get(k)
int seg = 0
for hh = 1 to h0
while seg < hs.size() - 2 and hs.get(seg + 1) < hh
seg += 1
int ha = hs.get(seg)
int hb = hs.get(seg + 1)
float t = hb > ha ? math.max(0.0, math.min(1.0, (hh - ha) * 1.0 / (hb - ha))) : 0.0
float qI = sI.get(seg) + t * (sI.get(seg + 1) - sI.get(seg))
float qO = sO.get(seg) + t * (sO.get(seg + 1) - sO.get(seg))
wI.set(hh, math.max(qI * sg * math.sqrt(hh), wI.get(hh - 1)))
wO.set(hh, math.max(qO * sg * math.sqrt(hh), wO.get(hh - 1), wI.get(hh)))
[ok, wI, wO]
// Range forecast: highest high minus lowest low of the h0 candles after each past close, in volatility units.
// Returns the median and the 80th percentile.
f_range(int k, int h0) =>
int sTo = k - h0
int sFrom = math.max(0, sTo - lookback + 1)
float r50 = na
float r80 = na
if sTo - sFrom + 1 >= MINN
array<float> rr = array.new<float>()
for s = sFrom to sTo
float mx = aLh.get(s + 1)
float mn = aLl.get(s + 1)
for j = 2 to h0
mx := math.max(mx, aLh.get(s + j))
mn := math.min(mn, aLl.get(s + j))
rr.push((mx - mn) / (aSig.get(s) * math.sqrt(h0)))
rr.sort()
r50 := f_quant(rr, 0.5)
r80 := f_quant(rr, 0.8)
[r50, r80]
//──────────────────────────────────────────────
// PAST CONES (made at their own close, kept fixed)
//──────────────────────────────────────────────
type Cone
int x0
int t0
float c0
int h
array<float> wI
array<float> wO
var array<Cone> past = array.new<Cone>()
if showPast and barstate.isconfirmed and sigOk and lastBar == bar_index and bar_index % H == 0 and bar_index >= last_bar_index - (pastN + 1) * H
[okP, wIP, wOP] = f_curve(aLc.size() - 1, H, math.min(H, 8))
if okP
past.push(Cone.new(bar_index, time, close, H, wIP, wOP))
if past.size() > pastN + 1
past.shift()
//──────────────────────────────────────────────
// ALERTS
//──────────────────────────────────────────────
// Unusual candle: this candle moved beyond the outer band for one candle, measured from the close before it
bool unusual = false
if sigOk and aLc.size() >= 2
int a1 = lastBar == bar_index ? aLc.size() - 2 : aLc.size() - 1
float q1 = aQ1.get(a1)
if not na(q1)
unusual := math.abs(logC - aLc.get(a1)) > q1 * aSig.get(a1)
alertcondition(unusual, "Unusual candle (beyond the outer band)", "{{ticker}} {{interval}}: this candle moved beyond the Expected Move Cone's outer band. Unusual move.")
alertcondition(sqzNew, "Volatility squeeze", "{{ticker}} {{interval}}: the expected move is in the narrowest 10% of the last 365 candles.")
//──────────────────────────────────────────────
// FORMATTING
//──────────────────────────────────────────────
f_px(float x) => str.tostring(x, format.mintick)
f_pc(float x) => str.tostring(x, math.abs(x) < 1 ? "0.00" : "0.0") + "%"
f_sgn(float x) => (x >= 0 ? "+" : "−") + f_pc(math.abs(x))
// Every cell uses a monospace font and is padded to CELL_W characters with non-breaking spaces (the table does not
// trim them); no cell is merged. Long explanations live in the tooltips.
CELL_W = 18
MONO = font.family_monospace
f_pad(string txt) =>
string t = str.length(txt) > CELL_W ? str.substring(txt, 0, CELL_W) : txt
int extra = CELL_W - str.length(t)
extra > 0 ? t + str.repeat(" ", extra) : t
// Verdict for a band: how far the share of closes that ended inside is from its promise
f_verdict(int n, int held, float target, float tol) =>
float rate = n > 0 ? held * 100.0 / n : 0.0
float err = math.abs(rate - target)
string txt = switch
n < minJudged => "NOT ENOUGH DATA"
err <= tol => "● RELIABLE"
err <= 2 * tol => "● MIXED"
=> "● NOT RELIABLE"
color bg = switch
n < minJudged => color.new(color.gray, 30)
err <= tol => color.new(color.green, 30)
err <= 2 * tol => color.new(color.orange, 30)
=> color.new(color.red, 30)
[txt, bg, rate]
//──────────────────────────────────────────────
// DRAWING (last candle only, redrawn when a candle closes)
//──────────────────────────────────────────────
var array<polyline> polys = array.new<polyline>()
var array<line> lns = array.new<line>()
var array<label> lbls = array.new<label>()
var table tbl = na
f_cell(int col, int row, string txt, color bg, string tip) =>
tbl.cell(col, row, f_pad(txt), text_color=color.white, text_size=tblSize, bgcolor=bg, text_halign=text.align_left, text_font_family=MONO, tooltip=tip)
var int drawnBar = -1
var int drawnH = -1
if barstate.islast and aLc.size() > 0 and (lastBar != drawnBar or H != drawnH)
drawnBar := lastBar
drawnH := H
for pl in polys
pl.delete()
polys.clear()
for ln in lns
ln.delete()
lns.clear()
for lb in lbls
lb.delete()
lbls.clear()
if not na(tbl)
tbl.delete()
tbl := showTable ? table.new(tblPos, 2, 8, border_width=1, border_color=color.new(color.gray, 70)) : na
bool ok = false
array<float> wI = na
array<float> wO = na
float r50 = na
float r80 = na
int k = aLc.size() - 1
int x0 = lastBar
float c0 = k >= 0 ? math.exp(aLc.get(k)) : na
if k >= 0
[okC, wIC, wOC] = f_curve(k, H, math.min(H, 15))
ok := okC
wI := wIC
wO := wOC
if ok
[ra, rb] = f_range(k, H)
r50 := ra
r80 := rb
// Numbers at the end of the cone, shared by the labels, the badge, the table and the alert
float u1 = na
float d1 = na
float u2 = na
float d2 = na
float em1 = na // average of the up and down % move
float em2 = na
float rg50 = na
float rg80 = na
string hz = str.tostring(H) + " candles"
string tipIn = ""
string tipOut = ""
string tipRg = ""
if ok
u1 := c0 * math.exp(wI.get(H))
d1 := c0 * math.exp(-wI.get(H))
u2 := c0 * math.exp(wO.get(H))
d2 := c0 * math.exp(-wO.get(H))
em1 := (math.exp(wI.get(H)) - math.exp(-wI.get(H))) / 2 * 100
em2 := (math.exp(wO.get(H)) - math.exp(-wO.get(H))) / 2 * 100
rg50 := (math.exp(r50 * aSig.get(k) * math.sqrt(H)) - 1) * 100
rg80 := (math.exp(r80 * aSig.get(k) * math.sqrt(H)) - 1) * 100
tipIn := str.tostring(pInIn) + "% of the time, the close " + hz + " later ended between " + f_px(d1) + " and " + f_px(u1) + "."
tipOut := str.tostring(math.round(pOut * 100)) + "% of the time, the close " + hz + " later ended between " + f_px(d2) + " and " + f_px(u2) + ". Beyond it = unusual move."
tipRg := "Over the next " + hz + ", the highest high and the lowest low were usually (median) " + f_pc(rg50) + " apart. 1 time in 5 they were more than " + f_pc(rg80) + " apart."
// Past cones: inner band, faded, with a dot where the close ended
if showPast and k >= 0
for c in past
if c.x0 + c.h <= lastBar and c.x0 >= lastBar - k
array<chart.point> pts = array.new<chart.point>()
for h = 0 to c.h
pts.push(chart.point.from_index(c.x0 + h, c.c0 * math.exp(c.wI.get(h))))
for h = c.h to 0
pts.push(chart.point.from_index(c.x0 + h, c.c0 * math.exp(-c.wI.get(h))))
polys.push(polyline.new(pts, closed=true, line_color=color.new(chart.fg_color, 75), fill_color=color.new(chart.fg_color, 93)))
float endLc = aLc.get(f_idx(c.x0 + c.h))
float mv = endLc - math.log(c.c0)
int res = math.abs(mv) <= c.wI.get(c.h) ? 2 : math.abs(mv) <= c.wO.get(c.h) ? 1 : 0
color dot = res == 2 ? C_IN : res == 1 ? C_MID : C_OUT
string verdictTxt = res == 2 ? "ended inside the " + nmIn + " band" : res == 1 ? "ended inside the " + nmOut + " band" : "broke out of the " + nmOut + " band"
string tip = "Cone made at " + str.format_time(c.t0, "yyyy-MM-dd HH:mm", syminfo.timezone) + ": " + nmIn + " ±" + f_pc((math.exp(c.wI.get(c.h)) - 1) * 100) + ", " + nmOut + " ±" + f_pc((math.exp(c.wO.get(c.h)) - 1) * 100) + ". " + str.tostring(c.h) + " candles later the close moved " + f_sgn((math.exp(mv) - 1) * 100) + ": " + verdictTxt + "."
lbls.push(label.new(c.x0 + c.h, math.exp(endLc), "", style=label.style_circle, color=dot, size=size.tiny, tooltip=tip))
if ok
// Forward cone: up half and down half, inner and outer band
array<chart.point> up1 = array.new<chart.point>()
array<chart.point> dn1 = array.new<chart.point>()
array<chart.point> up2 = array.new<chart.point>()
array<chart.point> dn2 = array.new<chart.point>()
for h = 0 to H
up1.push(chart.point.from_index(x0 + h, c0 * math.exp(wI.get(h))))
dn1.push(chart.point.from_index(x0 + h, c0 * math.exp(-wI.get(h))))
up2.push(chart.point.from_index(x0 + h, c0 * math.exp(wO.get(h))))
dn2.push(chart.point.from_index(x0 + h, c0 * math.exp(-wO.get(h))))
chart.point endMid = chart.point.from_index(x0 + H, c0)
array<chart.point> fUp1 = up1.copy()
fUp1.push(endMid)
array<chart.point> fDn1 = dn1.copy()
fDn1.push(endMid)
polys.push(polyline.new(fUp1, closed=true, line_color=color.new(upCol, 100), fill_color=color.new(upCol, 78)))
polys.push(polyline.new(fDn1, closed=true, line_color=color.new(dnCol, 100), fill_color=color.new(dnCol, 78)))
if showOuter
array<chart.point> fUp2 = up1.copy()
array<chart.point> fDn2 = dn1.copy()
for h = H to 0
fUp2.push(up2.get(h))
fDn2.push(dn2.get(h))
polys.push(polyline.new(fUp2, closed=true, line_color=color.new(upCol, 100), fill_color=color.new(upCol, 91)))
polys.push(polyline.new(fDn2, closed=true, line_color=color.new(dnCol, 100), fill_color=color.new(dnCol, 91)))
polys.push(polyline.new(up2, line_color=color.new(upCol, 35), line_style=line.style_dashed, line_width=1))
polys.push(polyline.new(dn2, line_color=color.new(dnCol, 35), line_style=line.style_dashed, line_width=1))
polys.push(polyline.new(up1, line_color=upCol, line_width=2))
polys.push(polyline.new(dn1, line_color=dnCol, line_width=2))
lns.push(line.new(x0, c0, x0 + H, c0, color=color.new(chart.fg_color, 50), style=line.style_dotted, width=1))
if showLbls
lbls.push(label.new(x0 + H, u1, nmIn + " ▲ " + f_px(u1) + " " + f_sgn((u1 / c0 - 1) * 100), style=label.style_label_left, color=color.new(upCol, 100), textcolor=upCol, size=size.small, tooltip=tipIn))
lbls.push(label.new(x0 + H, d1, nmIn + " ▼ " + f_px(d1) + " " + f_sgn((d1 / c0 - 1) * 100), style=label.style_label_left, color=color.new(dnCol, 100), textcolor=dnCol, size=size.small, tooltip=tipIn))
if showOuter
lbls.push(label.new(x0 + H, u2, nmOut + " ▲ " + f_px(u2) + " " + f_sgn((u2 / c0 - 1) * 100), style=label.style_label_left, color=color.new(upCol, 100), textcolor=color.new(upCol, 25), size=size.small, tooltip=tipOut))
lbls.push(label.new(x0 + H, d2, nmOut + " ▼ " + f_px(d2) + " " + f_sgn((d2 / c0 - 1) * 100), style=label.style_label_left, color=color.new(dnCol, 100), textcolor=color.new(dnCol, 25), size=size.small, tooltip=tipOut))
if showBadge
lbls.push(label.new(x0 + H, c0, "EXPECTED MOVE ±" + f_pc(em1) + "\n" + hTxt + " · range ≈ " + f_pc(rg50), style=label.style_label_left, color=color.new(#131722, 10), textcolor=color.white, size=size.normal, tooltip=tipIn + "\n" + tipRg))
if barstate.isrealtime and barstate.isconfirmed
alert(syminfo.ticker + " " + timeframe.period + " · expected move " + hTxt + ": ±" + f_pc(em1) + " (" + nmIn + " " + f_px(d1) + " – " + f_px(u1) + "; " + nmOut + " " + f_px(d2) + " – " + f_px(u2) + "). Range forecast " + f_pc(rg50) + ".", alert.freq_once_per_bar_close)
// Table
if showTable
color hdr = color.new(color.gray, 40)
color row = color.new(color.black, 30)
int nJ = jRes.size()
string rule = "Each closed candle's cone is checked " + str.tostring(H) + " candles later, using only the data known at that close: did the close end inside each band? The share should match the promise (" + str.tostring(pInIn) + "% and " + str.tostring(math.round(pOut * 100)) + "%). RELIABLE = within ±" + str.tostring(tolIn) + " / ±" + str.tostring(tolOut) + " points, MIXED = within twice that. Needs " + str.tostring(minJudged) + "+ cones. Tune it in Settings → 4 · Table."
f_cell(0, 0, "EM Cone " + syminfo.ticker, hdr, "Expected Move Cone + Range Forecast [Rider Algo] on " + syminfo.tickerid + ".")
f_cell(1, 0, timeframe.period + " · next " + str.tostring(H), hdr, autoOk ? "Auto horizon: one " + hName + " = " + str.tostring(H) + " candles on this chart." : "Custom horizon: " + str.tostring(H) + " candles.")
if not ok
f_cell(0, 1, "Not enough history", row, "")
f_cell(1, 1, "needs ~" + str.tostring(WARM + MINN + H + 20) + " candles", row, "The cone needs about " + str.tostring(WARM + MINN + H + 20) + " closed candles on this chart.")
else
f_cell(0, 1, "Expected move " + nmIn, row, tipIn)
f_cell(1, 1, "±" + f_pc(em1), row, tipIn + " Up " + f_sgn((u1 / c0 - 1) * 100) + ", down " + f_sgn((d1 / c0 - 1) * 100) + ".")
f_cell(0, 2, "Wide move " + nmOut, row, tipOut)
f_cell(1, 2, "±" + f_pc(em2), row, tipOut)
f_cell(0, 3, "Range forecast", row, tipRg)
f_cell(1, 3, f_pc(rg50) + " high-low", row, tipRg)
string vTxt = na(rankNow) ? "–" : rankNow <= 20 ? "Quiet · rank " + str.tostring(math.round(rankNow)) : rankNow >= 80 ? "Wild · rank " + str.tostring(math.round(rankNow)) : "Normal · rank " + str.tostring(math.round(rankNow))
f_cell(0, 4, "Volatility now", row, "Where today's volatility ranks among the last 365 candles (0 = calmest, 100 = wildest). The cone already accounts for it.")
f_cell(1, 4, vTxt, row, "Rank " + (na(rankNow) ? "–" : str.tostring(math.round(rankNow))) + " of 100 among the last 365 candles. Quiet = 20 or less, Wild = 80 or more.")
[vIn, bgIn, rIn] = f_verdict(nJ, jIn, pInIn, tolIn)
[vOut, bgOut, rOut] = f_verdict(nJ, jOut, pOut * 100, tolOut)
string accIn = nJ > 0 ? str.tostring(math.round(rIn)) + "% " : ""
string accOut = nJ > 0 ? str.tostring(math.round(rOut)) + "% " : ""
f_cell(0, 5, "Held " + nmIn + " band", row, rule)
f_cell(1, 5, nJ >= minJudged ? accIn + vIn : nJ > 0 ? accIn + "· only " + str.tostring(nJ) : "no cones yet", bgIn, str.tostring(jIn) + " of " + str.tostring(nJ) + " cones (" + str.tostring(rIn, "#.#") + "%) ended inside the " + nmIn + " band. Promise: " + str.tostring(pInIn) + "%.")
f_cell(0, 6, "Held " + nmOut + " band", row, rule)
f_cell(1, 6, nJ >= minJudged ? accOut + vOut : nJ > 0 ? accOut + "· only " + str.tostring(nJ) : "no cones yet", bgOut, str.tostring(jOut) + " of " + str.tostring(nJ) + " cones (" + str.tostring(rOut, "#.#") + "%) ended inside the " + nmOut + " band. Promise: " + str.tostring(math.round(pOut * 100)) + "%.")
f_cell(0, 7, "Judged: " + str.tostring(nJ), row, rule)
f_cell(1, 7, "Hover for the rule", row, rule)