本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// YTP Zones - YieldTerminal Pro (original script, 2026-11-07).
// Zones are built on a ZONE TIMEFRAME (default 4h) and drawn on whatever chart you are viewing, so a 15-min
// chart shows a few fat 4h zones instead of many thin 15-min slices.
// A zone = confirmed swing pivots (zone-timeframe highs and lows) that cluster within a fraction of ATR, merged
// with neighbours that sit close, ranked by strength (how far price moved away from its pivots, in ATR).
// Zones within 0.5 ATR of the nearest zone of an area (same side) count as one area; only its strongest zone is drawn,
// nearest areas first. Zones price has closed through stay and are tagged
// "was support" / "was resistance" (a broken support is often the next resistance).
// Each zone carries its record on zone-timeframe bars: tests, held (moved +R ATR away within H bars) or
// broke (closed B ATR through). The table compares zones with DECOYS (same bands shifted to empty prices)
// and with any zones you type in, scored by the identical test.
// Reference only. Past behaviour, not a prediction; a zone is not claimed to hold.
indicator('YTP Zones', overlay = true, max_boxes_count = 60, max_labels_count = 60)
zoneTf = input.timeframe('240', 'Zone timeframe (zones built from these bars)')
pivLen = input.int(3, 'Pivot bars each side', minval = 1, maxval = 10)
look = input.int(300, 'Lookback (zone-timeframe bars)', minval = 60, maxval = 1000)
mergeAtr = input.float(0.6, 'Cluster pivots within (x ATR)', minval = 0.1, step = 0.1)
minPiv = input.int(1, 'Min pivots per zone (1 suits 4h/daily zones)', minval = 1)
gapAtr = input.float(0.75, 'Merge zones closer than (x ATR)', minval = 0.0, step = 0.25)
minW = input.float(0.5, 'Minimum zone height (x ATR)', minval = 0.1, step = 0.1)
maxH = input.float(1.0, 'Maximum merged zone height (x ATR)', minval = 0.5, step = 0.25)
nEach = input.int(2, 'Zones shown above / below', minval = 1, maxval = 8)
dedupAtr = input.float(0.5, 'One zone per area: zones within (x ATR) of the nearest count as one area', minval = 0.0, step = 0.25)
H = input.int(10, 'Test window (zone-timeframe bars)', minval = 2, maxval = 60)
R = input.float(1.0, 'Held = moves away by (x ATR)', minval = 0.1, step = 0.25)
B = input.float(0.25, 'Broke = closes through by (x ATR)', minval = 0.0, step = 0.05)
decoyAtr = input.float(2.0, 'Decoy offset (x ATR)', minval = 0.5, step = 0.5)
showRec = input.bool(true, 'Show each zone\'s record')
showTbl = input.bool(true, 'Show record table')
cmpTxt = input.string('', 'Compare other zones: lo-hi, comma separated (scored with the same test)')
cUp = color.new(#ff6b6b, 0)
cDn = color.new(#27c08d, 0)
cIn = color.new(#ffd166, 0)
// Zone timeframe: never lower than the chart's own (a lower one would need intrabar data).
tfEff = timeframe.in_seconds(zoneTf) >= timeframe.in_seconds(timeframe.period) ? zoneTf : timeframe.period
// Last COMPLETED zone-timeframe bar, delivered once on the first chart bar after it closes
// ([1] + lookahead_on + gaps_on is the standard no-repaint form).
[zH_, zL_, zC_, zA_, zPH, zPL, zLowR, zHighR, zPT, zT] = request.security(syminfo.tickerid, tfEff, [high[1], low[1], close[1], ta.atr(14)[1], ta.pivothigh(high, pivLen, pivLen)[1], ta.pivotlow(low, pivLen, pivLen)[1], ta.lowest(low, pivLen)[1], ta.highest(high, pivLen)[1], time[1 + pivLen], time[1]], lookahead = barmerge.lookahead_on, gaps = barmerge.gaps_on)
var int hb = -1 // zone-timeframe bar counter
var array<float> aH = array.new_float() // zone-timeframe bar history (oldest first)
var array<float> aL = array.new_float()
var array<float> aC = array.new_float()
var array<float> aA = array.new_float()
var array<float> pp = array.new_float() // pivots: price, zone-tf index, time, strength
var array<int> pb = array.new_int()
var array<int> pt = array.new_int()
var array<float> pd = array.new_float()
if not na(zC_)
hb := hb + 1
array.push(aH, zH_)
array.push(aL, zL_)
array.push(aC, zC_)
array.push(aA, zA_)
if array.size(aC) > look + 80
array.shift(aH)
array.shift(aL)
array.shift(aC)
array.shift(aA)
if not na(zPH)
array.push(pp, zPH)
array.push(pb, hb - pivLen)
array.push(pt, zPT)
array.push(pd, zA_ > 0 ? (zPH - zLowR) / zA_ : 0.0)
if not na(zPL)
array.push(pp, zPL)
array.push(pb, hb - pivLen)
array.push(pt, zPT)
array.push(pd, zA_ > 0 ? (zHighR - zPL) / zA_ : 0.0)
// Pine's "and" does not short-circuit, so test the size before reading index 0.
while array.size(pb) > 0
if array.get(pb, 0) < hb - look
array.shift(pp)
array.shift(pb)
array.shift(pt)
array.shift(pd)
else
break
// Record of a band [lo, hi] on zone-timeframe bars, from offset 'from' (bars back) to the latest bar.
// A test = the bar reaches the band from outside (prior close beyond it). Returns [held, broke].
f_eval(float lo, float hi, int from) =>
int held = 0
int broke = 0
int N = array.size(aC)
int o = math.min(from, N - 2)
while o >= 1
int i = N - 1 - o
float pc = array.get(aC, i - 1)
float a = array.get(aA, i)
bool fromAbove = pc > hi and array.get(aL, i) <= hi
bool fromBelow = pc < lo and array.get(aH, i) >= lo
if (fromAbove or fromBelow) and not na(a)
int res = 0
int e = 0
while e <= H and res == 0
int q = i + e
if q > N - 1
break
if fromAbove
if array.get(aC, q) < lo - B * a
res := -1
res
else if array.get(aH, q) >= hi + R * a
res := 1
res
else
if array.get(aC, q) > hi + B * a
res := -1
res
else if array.get(aL, q) <= lo - R * a
res := 1
res
e := e + 1
e
if res == 1
held := held + 1
held
else if res == -1
broke := broke + 1
broke
o := o - math.max(e, 1)
o
else
o := o - 1
o
[held, broke]
// True if no zone-timeframe close has gone through the band (to the far side) since index 'since'.
f_fresh(float lo, float hi, int since, bool isAbove) =>
int N = array.size(aC)
int o0 = math.max(0, math.min(hb - since, N - 1))
bool fresh = true
for o = o0 to 0 by 1
float cc = array.get(aC, N - 1 - o)
if isAbove and cc > hi
fresh := false
fresh
if not isAbove and cc < lo
fresh := false
fresh
fresh
pct(h, b) =>
h + b > 0 ? str.tostring(math.round(100.0 * h / (h + b))) + '%' : 'n/a'
rec(h, b) =>
', tested ' + str.tostring(h + b) + 'x: ' + str.tostring(h) + ' held, ' + str.tostring(b) + ' broke'
var array<box> bx = array.new_box()
var array<label> lb = array.new_label()
var table tb = table.new(position.top_right, 4, 6, bgcolor = color.new(#08090e, 10), border_color = color.new(#3a3f55, 0), border_width = 1, frame_color = color.new(#3a3f55, 0), frame_width = 1)
// Price levels the chart's autoscale must include (TradingView autoscale ignores boxes, so a nearby
// resistance just above the candles could sit off-screen). Set on the last bar, plotted invisibly below.
var float scaleHi = na
var float scaleLo = na
if barstate.islast and array.size(aC) > 20
scaleHi := na
scaleLo := na
for bxi in bx
box.delete(bxi)
for lbi in lb
label.delete(lbi)
array.clear(bx)
array.clear(lb)
float a0 = array.get(aA, array.size(aA) - 1)
float tol = mergeAtr * a0
int barMs = time - time[1]
int xR = time + 6 * barMs
int xL = time + 2 * barMs
// 1) cluster pivots by price
array<float> zlo = array.new_float()
array<float> zhi = array.new_float()
array<int> zn = array.new_int()
array<int> zst = array.new_int()
array<int> ztm = array.new_int()
array<float> zd = array.new_float()
array<int> ze = array.new_int()
int n = array.size(pp)
if n > 0
array<int> idx = array.sort_indices(pp, order.ascending)
int j0 = array.get(idx, 0)
float cLo = array.get(pp, j0)
float cHi = cLo
int cN = 1
int cS = array.get(pb, j0)
int cT = array.get(pt, j0)
float cD = array.get(pd, j0)
int cE = cS
if n > 1
for k = 1 to n - 1 by 1
int j = array.get(idx, k)
float p = array.get(pp, j)
if p - cLo <= tol
cHi := p
cN := cN + 1
cS := math.min(cS, array.get(pb, j))
cT := math.min(cT, array.get(pt, j))
cE := math.max(cE, array.get(pb, j))
cD := cD + array.get(pd, j)
cD
else
array.push(zlo, cLo)
array.push(zhi, cHi)
array.push(zn, cN)
array.push(zst, cS)
array.push(ztm, cT)
array.push(zd, cD)
array.push(ze, cE)
cLo := p
cHi := p
cN := 1
cS := array.get(pb, j)
cT := array.get(pt, j)
cE := cS
cD := array.get(pd, j)
cD
array.push(zlo, cLo)
array.push(zhi, cHi)
array.push(zn, cN)
array.push(zst, cS)
array.push(ztm, cT)
array.push(zd, cD)
array.push(ze, cE)
// 2) keep zones with enough pivots; give each a minimum height
array<float> kLo = array.new_float()
array<float> kHi = array.new_float()
array<int> kS = array.new_int()
array<int> kT = array.new_int()
array<float> kD = array.new_float()
array<int> kE = array.new_int()
int nz = array.size(zlo)
if nz > 0
for k = 0 to nz - 1 by 1
if array.get(zn, k) >= minPiv
float lo = array.get(zlo, k)
float hi = array.get(zhi, k)
float mid = (lo + hi) / 2
float w = 0.5 * minW * a0
array.push(kLo, math.min(lo, mid - w))
array.push(kHi, math.max(hi, mid + w))
array.push(kS, array.get(zst, k))
array.push(kT, array.get(ztm, k))
array.push(kD, array.get(zd, k))
array.push(kE, array.get(ze, k))
// 2b) merge neighbours closer than gapAtr, as long as the merged zone stays under maxH
if array.size(kLo) > 1
array<float> mLo = array.new_float()
array<float> mHi = array.new_float()
array<int> mS = array.new_int()
array<int> mT = array.new_int()
array<float> mD = array.new_float()
array<int> mE = array.new_int()
array.push(mLo, array.get(kLo, 0))
array.push(mHi, array.get(kHi, 0))
array.push(mS, array.get(kS, 0))
array.push(mT, array.get(kT, 0))
array.push(mD, array.get(kD, 0))
array.push(mE, array.get(kE, 0))
for k = 1 to array.size(kLo) - 1 by 1
int t = array.size(mLo) - 1
if array.get(kLo, k) - array.get(mHi, t) <= gapAtr * a0 and math.max(array.get(mHi, t), array.get(kHi, k)) - array.get(mLo, t) <= maxH * a0
array.set(mHi, t, math.max(array.get(mHi, t), array.get(kHi, k)))
array.set(mS, t, math.min(array.get(mS, t), array.get(kS, k)))
array.set(mT, t, math.min(array.get(mT, t), array.get(kT, k)))
array.set(mD, t, array.get(mD, t) + array.get(kD, k))
array.set(mE, t, math.max(array.get(mE, t), array.get(kE, k)))
else
array.push(mLo, array.get(kLo, k))
array.push(mHi, array.get(kHi, k))
array.push(mS, array.get(kS, k))
array.push(mT, array.get(kT, k))
array.push(mD, array.get(kD, k))
array.push(mE, array.get(kE, k))
kLo := mLo
kHi := mHi
kS := mS
kT := mT
kD := mD
kE := mE
kE
// 3) record for every zone, and for its decoys (same band, stepped out until it overlaps no zone)
int zH = 0
int zB = 0
int dH = 0
int dB = 0
int nk = array.size(kLo)
array<int> hArr = array.new_int(nk, 0)
array<int> bArr = array.new_int(nk, 0)
if nk > 0
for k = 0 to nk - 1 by 1
float lo = array.get(kLo, k)
float hi = array.get(kHi, k)
int from = hb - array.get(kS, k) - pivLen - 1
if from >= 1
[h, b] = f_eval(lo, hi, from)
array.set(hArr, k, h)
array.set(bArr, k, b)
zH := zH + h
zB := zB + b
for s = 0 to 1 by 1
float sgn = s == 0 ? 1.0 : -1.0
for t = 0 to 5 by 1
float off = sgn * (decoyAtr + 0.5 * t) * a0
float dlo = lo + off
float dhi = hi + off
bool clash = false
for m = 0 to nk - 1 by 1
if dhi >= array.get(kLo, m) and dlo <= array.get(kHi, m)
clash := true
clash
if not clash and dlo > 0
[h2, b2] = f_eval(dlo, dhi, from)
dH := dH + h2
dB := dB + b2
break
// 3b) someone else's zones, typed in, scored with the identical test over the whole lookback
int oH = 0
int oB = 0
int oN = 0
if str.length(cmpTxt) > 0
array<string> parts = str.split(str.replace_all(cmpTxt, ' ', ''), ',')
for ptx in parts
array<string> ab = str.split(ptx, '-')
if array.size(ab) == 2
float x = str.tonumber(array.get(ab, 0))
float y = str.tonumber(array.get(ab, 1))
if not na(x) and not na(y)
float olo = math.min(x, y)
float ohi = math.max(x, y)
if ohi - olo < 0.3 * a0
float om = (olo + ohi) / 2
olo := om - 0.15 * a0
ohi := om + 0.15 * a0
ohi
oN := oN + 1
[h3, b3] = f_eval(olo, ohi, look)
oH := oH + h3
oB := oB + b3
array.push(bx, box.new(time - 60 * barMs, ohi, xR, olo, xloc = xloc.bar_time, border_color = color.new(#aab4bd, 30), border_style = line.style_dashed, bgcolor = color.new(#aab4bd, 94)))
// 4) draw: the zone price is inside (if any), then per side the nearest zone first and the strongest after it
float c = close
int sH = 0
int sB = 0
int sN = 0
array<float> upScore = array.new_float()
array<int> upIdx = array.new_int()
array<float> dnScore = array.new_float()
array<int> dnIdx = array.new_int()
if nk > 0
for k = 0 to nk - 1 by 1
float lo = array.get(kLo, k)
float hi = array.get(kHi, k)
if lo <= c and hi >= c
sH := sH + array.get(hArr, k)
sB := sB + array.get(bArr, k)
sN := sN + 1
array.push(bx, box.new(array.get(kT, k), hi, xR, lo, xloc = xloc.bar_time, border_color = color.new(cIn, 40), bgcolor = color.new(cIn, 88)))
string txt = 'Price inside zone ' + str.tostring(lo, format.mintick) + '-' + str.tostring(hi, format.mintick)
if showRec
txt := txt + '\nstrength ' + str.tostring(array.get(kD, k), '#.0') + rec(array.get(hArr, k), array.get(bArr, k))
txt
array.push(lb, label.new(xL, (lo + hi) / 2, txt, xloc = xloc.bar_time, style = label.style_label_left, color = color.new(#08090e, 20), textcolor = cIn, size = size.small))
else if lo > c
array.push(upScore, lo - c)
array.push(upIdx, k)
else
array.push(dnScore, c - hi)
array.push(dnIdx, k)
// Per side: walk zones from nearest to farthest; zones within dedupAtr of each other form one AREA, and only
// the most relevant zone of each area (highest strength) is drawn. The nearest nEach areas are shown.
for side = 0 to 1 by 1
array<float> sc = side == 0 ? upScore : dnScore
array<int> ix = side == 0 ? upIdx : dnIdx
int m = array.size(sc)
if m > 0
array<int> ord = array.sort_indices(sc, order.ascending)
array<int> reps = array.new_int()
int bestK = -1
float gLo = na
float gHi = na
for r = 0 to m - 1 by 1
int k = array.get(ix, array.get(ord, r))
float lo = array.get(kLo, k)
float hi = array.get(kHi, k)
if bestK == -1
bestK := k
gLo := lo
gHi := hi
else if lo <= gHi + dedupAtr * a0 and hi >= gLo - dedupAtr * a0
if array.get(kD, k) > array.get(kD, bestK)
bestK := k
else
array.push(reps, bestK)
bestK := k
gLo := lo
gHi := hi
if bestK != -1
array.push(reps, bestK)
for r = 0 to math.min(nEach, array.size(reps)) - 1 by 1
int k = array.get(reps, r)
float lo = array.get(kLo, k)
float hi = array.get(kHi, k)
color col = side == 0 ? cUp : cDn
bool flipped = not f_fresh(lo, hi, array.get(kE, k), side == 0)
sH := sH + array.get(hArr, k)
sB := sB + array.get(bArr, k)
sN := sN + 1
array.push(bx, box.new(array.get(kT, k), hi, xR, lo, xloc = xloc.bar_time, border_color = color.new(col, 40), bgcolor = color.new(col, 85)))
string txt = (side == 0 ? 'Resistance ' : 'Support ') + str.tostring(lo, format.mintick) + '-' + str.tostring(hi, format.mintick) + (flipped ? side == 0 ? ' (was support)' : ' (was resistance)' : '')
if showRec
txt := txt + '\nstrength ' + str.tostring(array.get(kD, k), '#.0') + rec(array.get(hArr, k), array.get(bArr, k))
txt
array.push(lb, label.new(xL, (lo + hi) / 2, txt, xloc = xloc.bar_time, style = label.style_label_left, color = color.new(#08090e, 20), textcolor = col, size = size.small))
if r == 0
if side == 0
scaleHi := hi
else
scaleLo := lo
// No zone above (price at/above its range highs) or below: show the range extreme as a plain reference
// so the chart is never one-sided, and say plainly that it is not a zone.
int nb = math.min(look, array.size(aH))
if array.size(upIdx) == 0 and nb > 0
float rh = array.max(array.slice(aH, array.size(aH) - nb, array.size(aH)))
if rh > c
array.push(bx, box.new(time - 40 * barMs, rh, xR, rh - 0.25 * a0, xloc = xloc.bar_time, border_color = color.new(cUp, 50), border_style = line.style_dotted, bgcolor = color.new(cUp, 92)))
array.push(lb, label.new(xL, rh, 'Range high ' + str.tostring(rh, format.mintick) + ' (no zone above)', xloc = xloc.bar_time, style = label.style_label_left, color = color.new(#08090e, 20), textcolor = cUp, size = size.small))
scaleHi := rh
if array.size(dnIdx) == 0 and nb > 0
float rl = array.min(array.slice(aL, array.size(aL) - nb, array.size(aL)))
if rl < c
array.push(bx, box.new(time - 40 * barMs, rl + 0.25 * a0, xR, rl, xloc = xloc.bar_time, border_color = color.new(cDn, 50), border_style = line.style_dotted, bgcolor = color.new(cDn, 92)))
array.push(lb, label.new(xL, rl, 'Range low ' + str.tostring(rl, format.mintick) + ' (no zone below)', xloc = xloc.bar_time, style = label.style_label_left, color = color.new(#08090e, 20), textcolor = cDn, size = size.small))
scaleLo := rl
if showTbl
color tc = color.new(#c8cde0, 0)
table.cell(tb, 0, 0, 'YTP Zones (' + tfEff + ')', text_color = color.new(#27c08d, 0), text_size = size.small)
table.cell(tb, 1, 0, 'tests', text_color = tc, text_size = size.small)
table.cell(tb, 2, 0, 'held', text_color = tc, text_size = size.small)
table.cell(tb, 3, 0, 'held %', text_color = tc, text_size = size.small)
table.cell(tb, 0, 1, 'All zones (' + str.tostring(nk) + ')', text_color = tc, text_size = size.small)
table.cell(tb, 1, 1, str.tostring(zH + zB), text_color = tc, text_size = size.small)
table.cell(tb, 2, 1, str.tostring(zH), text_color = tc, text_size = size.small)
table.cell(tb, 3, 1, pct(zH, zB), text_color = tc, text_size = size.small)
table.cell(tb, 0, 2, 'Shown (' + str.tostring(sN) + ')', text_color = color.new(#ffd166, 0), text_size = size.small)
table.cell(tb, 1, 2, str.tostring(sH + sB), text_color = tc, text_size = size.small)
table.cell(tb, 2, 2, str.tostring(sH), text_color = tc, text_size = size.small)
table.cell(tb, 3, 2, pct(sH, sB), text_color = tc, text_size = size.small)
table.cell(tb, 0, 3, 'Decoys (+/-' + str.tostring(decoyAtr) + ' ATR)', text_color = tc, text_size = size.small)
table.cell(tb, 1, 3, str.tostring(dH + dB), text_color = tc, text_size = size.small)
table.cell(tb, 2, 3, str.tostring(dH), text_color = tc, text_size = size.small)
table.cell(tb, 3, 3, pct(dH, dB), text_color = tc, text_size = size.small)
if oN > 0
table.cell(tb, 0, 4, 'Other zones (' + str.tostring(oN) + ')', text_color = color.new(#aab4bd, 0), text_size = size.small)
table.cell(tb, 1, 4, str.tostring(oH + oB), text_color = tc, text_size = size.small)
table.cell(tb, 2, 4, str.tostring(oH), text_color = tc, text_size = size.small)
table.cell(tb, 3, 4, pct(oH, oB), text_color = tc, text_size = size.small)
table.cell(tb, 0, 5, 'Last ' + str.tostring(look) + ' ' + tfEff + ' bars, held = +' + str.tostring(R) + ' ATR within ' + str.tostring(H) + ' bars. Past behaviour, not a prediction.', text_color = color.new(#8890b0, 0), text_size = size.tiny)
table.merge_cells(tb, 0, 5, 3, 5)
// Invisible plots so autoscale keeps the nearest resistance / support (or range extremes) on screen.
plot(scaleHi, 'Autoscale top', color = color.new(color.black, 100), editable = false, display = display.pane)
plot(scaleLo, 'Autoscale bottom', color = color.new(color.black, 100), editable = false, display = display.pane)