本页源码来自 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 at https://mozilla.org/MPL/2.0/
// © Unclehk2000
//@version=6
indicator("Domain Expansion — London Range, Overnight ATR & CBDR Qualifier", "Domain Expansion", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
tz = "America/New_York"
// ═══════════════════════════════════════════════════════════════════
// INPUTS
// ═══════════════════════════════════════════════════════════════════
var g_S = "═════ Sessions (ET) ═════"
drSess = input.session("0200-0230", "Domain range (forms)", group = g_S)
exSess = input.session("0230-0250", "Execution window", group = g_S)
lonSess = input.session("0200-0500", "Domain → end of London", tooltip = "Every level starts at the domain range open and stops at the end of London (05:00 ET), each day on its own.", group = g_S)
onSess = input.session("1800-0230", "Overnight (for the ATR)", group = g_S)
ftSess = input.session("0500-0700", "Follow-through window", tooltip = "Some expansions finish after London. The follow-through label reads the close at the end of this window.", group = g_S)
var g_Q = "═════ Qualifier · checked at 02:30 ═════"
atrLen = input.int(5, "Overnights in the ATR", minval = 1, group = g_Q)
useTR = input.bool(true, "True range (include the gap from the 16:59 close)", group = g_Q)
coilMax = input.float(0.20, "Coiled: domain range ÷ overnight ATR at or below", minval = 0.01, step = 0.01, tooltip = "Used when expansion is measured in SD. A domain range that is small next to recent overnight volatility is coiled, e.g. a 26-pt range with a 207-pt overnight ATR = 0.13.\n\nNQ 2018–24 / 2025–26, 2 SD past the range at the London extreme: coiled days 63% / 57%, other days 29% / 23%. The ATR level on its own barely moves these odds.", group = g_Q)
atrBig = input.float(1.00, "Volatile: overnight ATR at or above (% of price)", minval = 0.05, step = 0.05, tooltip = "Used when expansion is measured in %. Overnight ATR = average overnight (18:00 → 02:29) true range of the last N overnights, today's included, read from 15-minute data.\n\nNQ 2018–2026, 0.3% close past the range at 04:59, by ATR: under 0.60% ~10% · 0.60–0.80% ~18% · 0.80–1.00% ~23% · 1.00%+ ~32% (all days 17%). Coiling does not help this measure.", group = g_Q)
cbSess = input.session("1400-2000", "CBDR session", tooltip = "CBDR (Central Bank Dealers Range) = the high-low range from 14:00 ET to 20:00 ET: the prior afternoon into the evening, across the 17:00 break.", group = g_Q)
cbMult = input.float(5.0, "CBDR wide (SD measure): at or above N domain ranges", minval = 0.5, step = 0.5, tooltip = "Used when expansion is measured in SD. NQ 2018–24 / 2025–26, 2 SD past the range at the London extreme:\nBoth (coiled + CBDR): 59% / 65%\nCoiled only: 50% / 33%\nCBDR only: 37% / 30%\nNeither: 20% / 17%\n4 and 6 ranges give the same picture.", group = g_Q)
cbPct = input.float(0.80, "CBDR wide (% measure): at or above % of price", minval = 0.05, step = 0.05, tooltip = "Used when expansion is measured in %. NQ 2018–24 / 2025–26, 0.3% close past the range at 04:59:\nBoth (volatile + CBDR): 35% / 37%\nVolatile only: 9% / 3% (rare)\nCBDR only: 21% / 20%\nNeither: 8% / 5%", group = g_Q)
colBoth = input.color(#ff6d00, "Both", inline = "qc", group = g_Q)
colF1 = input.color(#ffb300, "Coiled / volatile only", inline = "qc", group = g_Q)
colCB = input.color(#00acc1, "CBDR only", inline = "qc2", group = g_Q)
colN = input.color(color.gray, "Neither", inline = "qc2", group = g_Q)
var g_X2 = "═════ What counts as an expansion ═════"
expMode = input.string("SD at the London extreme", "Measure", options = ["SD at the London extreme", "SD at the 04:59 close", "% at the 04:59 close"], tooltip = "SD = multiples of the domain range beyond its high or low. The London extreme is the furthest point price reaches past the range between 02:30 and 04:59.", group = g_X2)
expSD = input.float(2.0, "SD needed", minval = 0.25, step = 0.25, group = g_X2)
expPct = input.float(0.30, "% needed", minval = 0.05, step = 0.05, group = g_X2)
showExp = input.bool(true, "Draw the expansion line", inline = "e", group = g_X2)
colExp = input.color(color.new(#5e35b1, 30), "", inline = "e", group = g_X2)
var g_R = "═════ Domain range ═════"
colRng = input.color(#5e35b1, "High / low", group = g_R)
showMid = input.bool(true, "Midpoint", inline = "m", group = g_R)
colMid = input.color(color.new(#5e35b1, 40), "", inline = "m", group = g_R)
showBox = input.bool(true, "Shade the range", inline = "b", group = g_R)
colBox = input.color(color.new(#5e35b1, 88), "", inline = "b", group = g_R)
var g_X = "═════ Standard deviations (domain range multiples) ═════"
sdMode = input.string("Always", "Draw", options = ["Always", "Both factors only", "At least one factor"], tooltip = "k SD = k domain ranges beyond the domain high (+) and low (−), drawn on both sides from 02:00 to 05:00.\n\nOn days that closed London 0.3% beyond the range (NQ 2018–2026) the move topped out at a median 3.1–3.2 SD (middle half 2.2–4.5). Reached: 2 SD ~83%, 2.5 SD ~68%, 3 SD ~56%, 4 SD ~30%, 5 SD ~16%.", group = g_X)
sd10 = input.bool(false, "1.0", inline = "s1", group = g_X)
sd15 = input.bool(false, "1.5", inline = "s1", group = g_X)
sd20 = input.bool(true, "2.0", inline = "s1", group = g_X)
sd25 = input.bool(true, "2.5", inline = "s1", group = g_X)
sd30 = input.bool(true, "3.0", inline = "s2", group = g_X)
sd40 = input.bool(true, "4.0", inline = "s2", group = g_X)
sd50 = input.bool(false, "5.0", inline = "s2", group = g_X)
colSD = input.color(color.new(#5e35b1, 45), "SD colour", group = g_X)
showSDc = input.bool(true, "Custom SDs", inline = "sc", group = g_X)
sdTxt = input.string("", "", inline = "sc", group = g_X, tooltip = "Your own multiples, separated by commas, e.g. 3.5, 6\nEach one is drawn above the high and below the low. Up to 10; anything already ticked is skipped.")
colSDc = input.color(color.new(#00897b, 30), "Custom colour", inline = "sc2", group = g_X)
sdSty = input.string("Dotted", "", options = ["Dotted", "Dashed", "Solid"], inline = "sc2", group = g_X)
var g_E = "═════ Execution & result ═════"
showWin = input.bool(true, "Shade the execution window", group = g_E)
showBrk = input.bool(true, "Mark the first break in the window", group = g_E)
showRes = input.bool(true, "Mark the result at the end of London", group = g_E)
showFT = input.bool(true, "Mark the follow-through at the end of its window", group = g_E)
showUK = input.bool(true, "Mark the 08:00 UK open", inline = "uk", tooltip = "London and Frankfurt cash open: 03:00 ET most of the year, 04:00 ET in the weeks US and UK clocks are out of step.", group = g_E)
colUK = input.color(color.new(color.gray, 30), "", inline = "uk", group = g_E)
var g_D = "═════ Drawing ═════"
maxDays = input.int(10, "Days to keep", minval = 1, group = g_D)
dateFmt = input.string("HH:mm MM/dd", "Label date format", group = g_D)
txtCol = input.color(color.black, "Text colour", group = g_D)
lblSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = g_D)
ageDays = input.int(5, "Fresh for first N trading days", minval = 1, group = g_D)
agedCol = input.color(color.gray, "Aged colour", group = g_D)
if not timeframe.isintraday or timeframe.in_seconds() > 15 * 60
runtime.error("Use an intraday chart of 15 minutes or less (1 min recommended).")
tSize = lblSize == "Tiny" ? size.tiny : lblSize == "Normal" ? size.normal : size.small
stampOf(int t) => str.format_time(t, dateFmt, tz)
hhmm(int t) => str.format_time(t, "HH:mm", tz)
pctTxt(float v) => str.tostring(v, "#.##") + "%"
sdTxtOf(float v) => str.tostring(v, "0.0#") + " SD"
sdStyle = sdSty == "Dashed" ? line.style_dashed : sdSty == "Solid" ? line.style_solid : line.style_dotted
modeSD = expMode != "% at the 04:59 close"
// custom SDs: commas, spaces or semicolons; signs ignored (each multiple is drawn on both sides)
parseSD(string txt) =>
clean = str.replace_all(str.replace_all(str.replace_all(str.replace_all(txt, "−", ""), "-", ""), ";", ","), " ", ",")
parts = str.split(clean, ",")
out = array.new<float>()
for part in parts
v = str.tonumber(part)
if not na(v) and v > 0 and out.size() < 10 and not out.includes(v)
out.push(v)
out
var array<float> sdCust = parseSD(sdTxt)
var array<float> sdPre = array.from(1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 5.0)
sdTicked(float k) =>
(sd10 and k == 1.0) or (sd15 and k == 1.5) or (sd20 and k == 2.0) or (sd25 and k == 2.5) or (sd30 and k == 3.0) or (sd40 and k == 4.0) or (sd50 and k == 5.0)
// ═══════════════════════════════════════════════════════════════════
// SESSIONS & TRADING DAY (18:00 ET start)
// ═══════════════════════════════════════════════════════════════════
sessOn(string s) => not na(time(timeframe.period, s, tz))
inDR = sessOn(drSess)
inEx = sessOn(exSess)
inLon = sessOn(lonSess)
inFT = sessOn(ftSess)
inUK8 = not na(time(timeframe.period, "0800-0900", "Europe/London"))
drStart = inDR and not inDR[1]
drEnd = not inDR and inDR[1]
lonEnd = not inLon and inLon[1]
ftEnd = not inFT and inFT[1]
dayStart = ta.change(time_tradingday) != 0 // new CME trading day (the 18:00 ET bar)
ukStart = inUK8 and not inUK8[1]
var int dayCount = 0
if dayStart
dayCount += 1
// ═══════════════════════════════════════════════════════════════════
// DRAWING STORE
// ═══════════════════════════════════════════════════════════════════
type Lv
line ln
label lb
box bx
float y
bool above
int day
bool hit
bool live
bool watch // dash it when price trades through after it forms
string grp // "dr" = runs to the end of London; anything else is fixed
var array<Lv> store = array.new<Lv>()
var array<label> evLbl = array.new<label>()
var array<int> evDay = array.new<int>()
mkLv(int t0, float y, color col, int w, string st, string txt, bool above, bool watch) =>
lv = Lv.new(line.new(t0, y, time, y, xloc = xloc.bar_time, color = col, style = st, width = w), label.new(time, y, txt, xloc = xloc.bar_time, style = label.style_label_left, color = #ffffff00, textcolor = txtCol, size = tSize), na, y, above, dayCount, false, true, watch, "dr")
store.push(lv)
lv
mkEvent(float y, string txt, color col, bool above) =>
evLbl.push(label.new(time, y, txt, xloc = xloc.bar_time, style = above ? label.style_label_down : label.style_label_up, color = color.new(col, 80), textcolor = txtCol, size = tSize))
evDay.push(dayCount)
if dayStart
while store.size() > 0 and store.get(0).day <= dayCount - maxDays
lv = store.shift()
if not na(lv.ln)
lv.ln.delete()
if not na(lv.bx)
lv.bx.delete()
lv.lb.delete()
while evDay.size() > 0 and evDay.get(0) <= dayCount - maxDays
evLbl.shift().delete()
evDay.shift()
// ═══════════════════════════════════════════════════════════════════
// OVERNIGHT ATR: average overnight (18:00 → 02:29) true range of the last N overnights, today's included
// ═══════════════════════════════════════════════════════════════════
// Computed on 15-minute data so it is ready from the first day on the chart (1-minute history is short).
// The value is updated on the first 15-minute bar after the overnight ends and uses only the bars before it,
// so reading it with lookahead on does not see the future.
nightCtx() =>
var float pc = na
var float oh = na
var float ol = na
var array<float> tp = array.new<float>()
var array<float> tq = array.new<float>()
var float ap = na
var float aq = na
inO = not na(time(timeframe.period, onSess, tz))
if ta.change(time_tradingday) != 0
pc := close[1]
if inO and not inO[1]
oh := high
ol := low
else if inO
oh := math.max(oh, high)
ol := math.min(ol, low)
if not inO and inO[1] and not na(oh)
hi = useTR and not na(pc) ? math.max(oh, pc) : oh
lo = useTR and not na(pc) ? math.min(ol, pc) : ol
tp.push(100 * (hi - lo) / close[1])
tq.push(hi - lo)
while tp.size() > atrLen
tp.shift()
tq.shift()
ap := tp.size() >= atrLen ? tp.avg() : na
aq := tq.size() >= atrLen ? tq.avg() : na
// CBDR 14:00 → 20:00 ET, finalised on the first 15-minute bar after it ends (same no-lookahead logic as the ATR)
var float ch = na
var float cl = na
var float cv = na
inC = not na(time(timeframe.period, cbSess, tz))
if inC and not inC[1]
ch := high
cl := low
else if inC
ch := math.max(ch, high)
cl := math.min(cl, low)
if not inC and inC[1] and not na(ch)
cv := ch - cl
[ap, aq, cv]
[atrPct, atrPts, cbPts] = request.security(syminfo.tickerid, "15", nightCtx(), lookahead = barmerge.lookahead_on)
// ═══════════════════════════════════════════════════════════════════
// DOMAIN RANGE (02:00 → 02:30), levels 02:00 → 05:00
// ═══════════════════════════════════════════════════════════════════
var float dH = na
var float dL = na
var int dHt = na
var int dLt = na
var int dT0 = na
if drStart
dH := high
dL := low
dHt := time
dLt := time
dT0 := time
else if inDR
if high > dH
dH := high
dHt := time
if low < dL
dL := low
dLt := time
var bool qual = false // factor 1: coiled (SD measure) or volatile (% measure)
var bool qualV = false
var bool qualC = false
var bool cbOK = false // factor 2: CBDR wide
var int tier = 0 // 3 both · 2 factor 1 only · 1 CBDR only · 0 neither
var string oddsT = ""
var float rD = na
var float coil = na
var float xUp = na
var float xDn = na
var bool brkDone = true
var int dDay = na
var float lonHi = na
var float lonLo = na
if drEnd and not na(dH) and dH > dL
dDay := dayCount
rD := dH - dL
coil := na(atrPts) ? na : rD / atrPts
qualC := not na(coil) and coil <= coilMax
qualV := not na(atrPct) and atrPct >= atrBig
qual := modeSD ? qualC : qualV
cbV = na(cbPts) ? na : 100 * cbPts / close[1]
cbR = na(cbPts) ? na : cbPts / rD
cbOK := modeSD ? (not na(cbR) and cbR >= cbMult) : (not na(cbV) and cbV >= cbPct)
tier := qual and cbOK ? 3 : qual ? 2 : cbOK ? 1 : 0
// historical odds by tier (NQ 2018–2026) at the default yardsticks: 2 SD at the London extreme, 0.3% close at 04:59
oddsT := modeSD ? (tier == 3 ? "~60%" : tier == 2 ? "~40%" : tier == 1 ? "~35%" : "~20%") : (tier == 3 ? "~35%" : tier == 2 ? "~5%" : tier == 1 ? "~21%" : "~8%")
xUp := modeSD ? dH + expSD * rD : dH * (1 + expPct / 100)
xDn := modeSD ? dL - expSD * rD : dL * (1 - expPct / 100)
brkDone := false
lonHi := high
lonLo := low
if showBox
store.push(Lv.new(na, label.new(dT0, dH, "", xloc = xloc.bar_time, style = label.style_none), box.new(dT0, dH, time, dL, xloc = xloc.bar_time, bgcolor = colBox, border_color = color.new(colBox, 100)), dH, true, dayCount, false, false, false, "box"))
mkLv(dT0, dH, colRng, 1, line.style_solid, stampOf(dHt) + " Domain high", true, true)
mkLv(dT0, dL, colRng, 1, line.style_solid, stampOf(dLt) + " Domain low", false, true)
if showMid
mkLv(dT0, (dH + dL) / 2, colMid, 1, line.style_dotted, stampOf(dT0) + " Domain 0.5", true, false)
sdOn = sdMode == "Always" or (sdMode == "Both factors only" and tier == 3) or (sdMode == "At least one factor" and tier >= 1)
// the expansion line, unless an SD line already sits on it
expOnSD = modeSD and sdOn and (sdTicked(expSD) or (showSDc and sdCust.includes(expSD)))
if showExp and not expOnSD
expLbl = modeSD ? sdTxtOf(expSD) : pctTxt(expPct)
mkLv(dT0, xUp, colExp, 1, line.style_solid, "+" + expLbl + " · expansion", true, true)
mkLv(dT0, xDn, colExp, 1, line.style_solid, "−" + expLbl + " · expansion", false, true)
if sdOn
for k in sdPre
if sdTicked(k)
tag = modeSD and k == expSD ? " · expansion" : ""
mkLv(dT0, dH + k * rD, colSD, 1, line.style_dotted, "+" + sdTxtOf(k) + tag, true, true)
mkLv(dT0, dL - k * rD, colSD, 1, line.style_dotted, "−" + sdTxtOf(k) + tag, false, true)
if showSDc and sdCust.size() > 0
for k in sdCust
if not sdTicked(k)
tag = modeSD and k == expSD ? " · expansion" : ""
mkLv(dT0, dH + k * rD, colSDc, 1, sdStyle, "+" + sdTxtOf(k) + tag, true, true)
mkLv(dT0, dL - k * rD, colSDc, 1, sdStyle, "−" + sdTxtOf(k) + tag, false, true)
f1Name = modeSD ? "coiled" : "volatile"
verdict = tier == 3 ? "◆◆ Domain Expansion: BOTH" : tier == 2 ? "◆ Domain Expansion: " + f1Name + " only" : tier == 1 ? "◆ Domain Expansion: CBDR only" : "◇ Domain Expansion: NEITHER"
// odds are only shown at the yardsticks they were measured on
oddsOn = (expMode == "SD at the London extreme" and expSD == 2.0) or (expMode == "% at the 04:59 close" and expPct == 0.30)
oddsTxt = oddsOn ? " · " + (modeSD ? "2 SD odds " : "0.3% odds ") + oddsT : ""
coilTxt = na(coil) ? "Coil n/a" : "Coil " + str.tostring(coil, "0.00") + " (" + str.tostring(math.round(rD)) + " ÷ " + str.tostring(math.round(atrPts)) + " pts)" + (modeSD ? (qualC ? " ✓" : " ✗") : "")
atrTxt = na(atrPct) ? "Overnight ATR warming up" : "Overnight ATR(" + str.tostring(atrLen) + ") " + pctTxt(atrPct) + (modeSD ? "" : (qualV ? " ✓" : " ✗"))
cbTxt = na(cbPts) ? "CBDR n/a" : "CBDR " + str.tostring(math.round(cbPts)) + " pts · " + (modeSD ? str.tostring(cbR, "0.0") + " ranges" : pctTxt(cbV)) + (cbOK ? " ✓" : " ✗")
f1Txt = modeSD ? coilTxt : atrTxt
mkEvent(dH, verdict + oddsTxt + "\n" + f1Txt + "\n" + cbTxt, tier == 3 ? colBoth : tier == 2 ? colF1 : tier == 1 ? colCB : colN, true)
qCol = tier == 3 ? colBoth : tier == 2 ? colF1 : tier == 1 ? colCB : colN
// London extremes after the range (02:30 → 04:59)
if inLon and not inDR and dDay == dayCount and not drEnd
lonHi := math.max(lonHi, high)
lonLo := math.min(lonLo, low)
// execution window
bgcolor(showWin and inEx and dDay == dayCount ? color.new(qCol, 88) : na, title = "Execution window")
// first break of the domain range inside the execution window
if showBrk and inEx and not brkDone and dDay == dayCount
if high > dH and low < dL
brkDone := true
mkEvent(high, "Both sides " + hhmm(time), colN, true)
else if high > dH
brkDone := true
mkEvent(high, "▲ Break " + hhmm(time), qCol, true)
else if low < dL
brkDone := true
mkEvent(low, "▼ Break " + hhmm(time), qCol, false)
// distance past the domain range: SD and %, positive above the high, negative below the low
pastSD(float p) => p > dH ? (p - dH) / rD : p < dL ? -(dL - p) / rD : 0.0
pastPct(float p) => p > dH ? 100 * (p - dH) / dH : p < dL ? -100 * (dL - p) / dL : 0.0
farTxt(float p) =>
s = pastSD(p)
s == 0 ? "inside the range" : sdTxtOf(math.abs(s)) + " (" + pctTxt(math.abs(pastPct(p))) + ")" + (s > 0 ? " above" : " below")
// result at the end of London
if showRes and lonEnd and dDay == dayCount and not na(rD)
c5 = close[1]
upX = (lonHi - dH) / rD
dnX = (dL - lonLo) / rD
isUp = expMode == "SD at the London extreme" ? (upX >= expSD and upX >= dnX) : c5 >= xUp
isDn = expMode == "SD at the London extreme" ? (dnX >= expSD and dnX > upX) : c5 <= xDn
exTxt = math.max(upX, dnX) <= 0 ? "stayed inside the range" : upX >= dnX ? "extreme " + sdTxtOf(upX) + " above" : "extreme " + sdTxtOf(dnX) + " below"
head = isUp ? "✓ Domain Expansion ▲" : isDn ? "✓ Domain Expansion ▼" : "✗ No expansion"
mkEvent(c5, head + "\n" + exTxt + " · close " + farTxt(c5), isUp or isDn ? qCol : colN, c5 >= (dH + dL) / 2)
// follow-through after London
if showFT and ftEnd and dDay == dayCount and not na(rD)
c7 = close[1]
mkEvent(c7, "Follow-through " + hhmm(time) + " · close " + farTxt(c7), colN, c7 >= (dH + dL) / 2)
// 08:00 UK open
if showUK and ukStart
store.push(Lv.new(line.new(time, low, time, high + syminfo.mintick, xloc = xloc.bar_time, extend = extend.both, color = colUK, style = line.style_dotted), label.new(time, high, "08:00 UK", xloc = xloc.bar_time, style = label.style_label_down, color = #ffffff00, textcolor = txtCol, size = tSize), na, high, true, dayCount, false, false, false, "uk"))
// ═══════════════════════════════════════════════════════════════════
// EVERY BAR: grow to the current candle, stop at the end of London, dash, age
// ═══════════════════════════════════════════════════════════════════
nS = store.size()
if nS > 0
for i = 0 to nS - 1
lv = store.get(i)
if lv.live and lv.day != dayCount
lv.live := false
stopNow = lv.live and lv.grp == "dr" and not inLon
if lv.live
lv.ln.set_x2(time)
lv.lb.set_x(time)
if lv.watch and lv.live and not stopNow and not lv.hit and (lv.above ? high >= lv.y : low <= lv.y)
lv.hit := true
lv.ln.set_style(line.style_dashed)
if stopNow
lv.live := false
if dayCount - lv.day >= ageDays
if not na(lv.ln)
lv.ln.set_color(agedCol)
if not na(lv.bx)
lv.bx.set_bgcolor(color.new(agedCol, 88))
lv.lb.set_textcolor(color.gray)
nE = evLbl.size()
if nE > 0
for i = 0 to nE - 1
if dayCount - evDay.get(i) >= ageDays
evLbl.get(i).set_color(color.new(agedCol, 85))
evLbl.get(i).set_textcolor(color.gray)