本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Bollinger + 200 MA Ladder", shorttitle = "BB/200 Ladder", overlay = true)
// ─────────────────────────────────────────────
// SETTINGS (tuned for a daily chart)
// ─────────────────────────────────────────────
maLen = input.int(200, "Trend MA length", minval = 1, group = "Trend filter")
maType = input.string("SMA", "Trend MA type", options = ["SMA", "EMA"], group = "Trend filter")
bandMode = input.string("Bollinger", "Band type", options = ["Bollinger", "Anchored VWAP"], group = "Bands")
bbLen = input.int(200, "Bollinger length", minval = 2, group = "Bands")
anchorIn = input.string("Year", "VWAP anchor (VWAP mode only)", options = ["Year", "Quarter", "Month"], group = "Bands")
mult1 = input.float(1.0, "Band 1 (SD)", step = 0.25, group = "Bands")
mult2 = input.float(2.0, "Band 2 (SD)", step = 0.25, group = "Bands")
heavyTrig = input.string("Band 1", "BUY HEAVY at or below", options = ["Band 1", "Band 2"], group = "Bands")
showBands = input.bool(true, "Show bands on chart", group = "Bands")
alNext = input.bool(true, "Alert when NEXT buy changes", group = "Alerts")
alSize = input.bool(true, "Alert when NEXT switches BUY ↔ BUY HEAVY", group = "Alerts")
alSell = input.bool(true, "Alert when any asset flips to SELL", group = "Alerts")
alDaily = input.bool(false, "Daily summary of current NEXT (every bar close)", group = "Alerts")
tblPosIn = input.string("Top Right", "Table position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = "Table")
tblSize = input.string("Small", "Table text size", options = ["Tiny", "Small", "Normal"], group = "Table")
// ─────────────────────────────────────────────
// LADDER (priority order) — any TradingView symbol works.
// "On" = include in ladder. "Cap" = at weight cap, skip for NEXT.
// ─────────────────────────────────────────────
s1 = input.symbol("TSX:VFV", "1", inline = "1", group = "Ladder (priority order)")
o1 = input.bool(true, "On", inline = "1", group = "Ladder (priority order)")
k1 = input.bool(false, "Cap", inline = "1", group = "Ladder (priority order)")
s2 = input.symbol("TSX:ZGLD", "2", inline = "2", group = "Ladder (priority order)")
o2 = input.bool(true, "On", inline = "2", group = "Ladder (priority order)")
k2 = input.bool(false, "Cap", inline = "2", group = "Ladder (priority order)")
s3 = input.symbol("TSX:XEF", "3", inline = "3", group = "Ladder (priority order)")
o3 = input.bool(true, "On", inline = "3", group = "Ladder (priority order)")
k3 = input.bool(false, "Cap", inline = "3", group = "Ladder (priority order)")
s4 = input.symbol("TSX:VCN", "4", inline = "4", group = "Ladder (priority order)")
o4 = input.bool(true, "On", inline = "4", group = "Ladder (priority order)")
k4 = input.bool(false, "Cap", inline = "4", group = "Ladder (priority order)")
s5 = input.symbol("TSX:XEC", "5", inline = "5", group = "Ladder (priority order)")
o5 = input.bool(true, "On", inline = "5", group = "Ladder (priority order)")
k5 = input.bool(false, "Cap", inline = "5", group = "Ladder (priority order)")
s6 = input.symbol("TSX:ZUT", "6", inline = "6", group = "Ladder (priority order)")
o6 = input.bool(true, "On", inline = "6", group = "Ladder (priority order)")
k6 = input.bool(false, "Cap", inline = "6", group = "Ladder (priority order)")
s7 = input.symbol("TSX:ZGI", "7", inline = "7", group = "Ladder (priority order)")
o7 = input.bool(true, "On", inline = "7", group = "Ladder (priority order)")
k7 = input.bool(false, "Cap", inline = "7", group = "Ladder (priority order)")
s8 = input.symbol("TSX:COPP", "8", inline = "8", group = "Ladder (priority order)")
o8 = input.bool(true, "On", inline = "8", group = "Ladder (priority order)")
k8 = input.bool(false, "Cap", inline = "8", group = "Ladder (priority order)")
s9 = input.symbol("TSX:HURA", "9", inline = "9", group = "Ladder (priority order)")
o9 = input.bool(true, "On", inline = "9", group = "Ladder (priority order)")
k9 = input.bool(false, "Cap", inline = "9", group = "Ladder (priority order)")
s10 = input.symbol("TSX:BTCC.B", "10", inline = "10", group = "Ladder (priority order)")
o10 = input.bool(true, "On", inline = "10", group = "Ladder (priority order)")
k10 = input.bool(false, "Cap", inline = "10", group = "Ladder (priority order)")
// ─────────────────────────────────────────────
// HELPERS
// ─────────────────────────────────────────────
anchorTf = anchorIn == "Year" ? "12M" : anchorIn == "Quarter" ? "3M" : "1M"
heavyZ = heavyTrig == "Band 1" ? mult1 : mult2
ma(x, len) =>
maType == "SMA" ? ta.sma(x, len) : ta.ema(x, len)
// Band centre and 1-SD width, for either mode
bandCalc() =>
bbC = ta.sma(close, bbLen)
bbS = ta.stdev(close, bbLen)
[vw, vu, vl] = ta.vwap(hlc3, timeframe.change(anchorTf), 1.0)
c = bandMode == "Bollinger" ? bbC : vw
s = bandMode == "Bollinger" ? bbS : vu - vw
[c, s]
// Returns [close, trend MA, band centre, 1 SD] in the requested symbol's context
calc() =>
[c, s] = bandCalc()
[close, ma(close, maLen), c, s]
getData(sym) =>
request.security(sym, "D", calc())
cleanSym(s) =>
parts = str.split(s, ":")
array.size(parts) > 1 ? array.get(parts, 1) : s
actName(a) =>
a == 3 ? "BUY HEAVY" : a == 2 ? "BUY" : a == 1 ? "HOLD" : a == 0 ? "SELL" : "?"
// ─────────────────────────────────────────────
// CHART PLOTS
// ─────────────────────────────────────────────
maVal = ma(close, maLen)
[cC, cS] = bandCalc()
u1 = cC + mult1 * cS
l1 = cC - mult1 * cS
u2 = cC + mult2 * cS
l2 = cC - mult2 * cS
plot(maVal, "Trend MA", color.red, 2)
plot(showBands ? cC : na, "Band centre", color.new(color.aqua, 0), 1, style = plot.style_linebr)
pU1 = plot(showBands ? u1 : na, "Upper 1", color.new(color.orange, 40), 1, style = plot.style_linebr)
pL1 = plot(showBands ? l1 : na, "Lower 1", color.new(color.green, 30), 1, style = plot.style_linebr)
pU2 = plot(showBands ? u2 : na, "Upper 2", color.new(color.red, 20), 1, style = plot.style_linebr)
pL2 = plot(showBands ? l2 : na, "Lower 2", color.new(color.lime, 10), 1, style = plot.style_linebr)
fill(pU1, pL1, color = showBands ? color.new(color.aqua, 94) : na, title = "±1 SD fill")
fill(pL1, pL2, color = showBands ? color.new(color.green, 85) : na, title = "Buy zone fill")
fill(pU1, pU2, color = showBands ? color.new(color.red, 90) : na, title = "Stretched zone fill")
// ─────────────────────────────────────────────
// TABLE DATA
// ─────────────────────────────────────────────
[p1, m1, c1, d1] = getData(s1)
[p2, m2, c2, d2] = getData(s2)
[p3, m3, c3, d3] = getData(s3)
[p4, m4, c4, d4] = getData(s4)
[p5, m5, c5, d5] = getData(s5)
[p6, m6, c6, d6] = getData(s6)
[p7, m7, c7, d7] = getData(s7)
[p8, m8, c8, d8] = getData(s8)
[p9, m9, c9, d9] = getData(s9)
[p10, m10, c10, d10] = getData(s10)
syms = array.from(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10)
ons = array.from(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10)
caps = array.from(k1, k2, k3, k4, k5, k6, k7, k8, k9, k10)
px = array.from(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
mas = array.from(m1, m2, m3, m4, m5, m6, m7, m8, m9, m10)
ctr = array.from(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10)
sds = array.from(d1, d2, d3, d4, d5, d6, d7, d8, d9, d10)
// Action codes: 3 = BUY HEAVY, 2 = BUY, 1 = HOLD, 0 = SELL, -1 = no data / off
actions = array.new_int(10, -1)
zs = array.new_float(10, na)
for i = 0 to 9
p = array.get(px, i)
m = array.get(mas, i)
c = array.get(ctr, i)
d = array.get(sds, i)
z = (na(p) or na(c) or na(d) or d <= 0) ? na : (p - c) / d
array.set(zs, i, z)
act = -1
if array.get(ons, i) and not na(p) and not na(m)
if p < m
act := (not na(z) and z <= -heavyZ) ? 3 : 2
else
act := (not na(z) and z >= mult2) ? 0 : 1
array.set(actions, i, act)
// NEXT buy: strictly by priority — first enabled, uncapped asset that is BUY or BUY HEAVY
int nextIdx = -1
for i = 0 to 9
a = array.get(actions, i)
if array.get(ons, i) and not array.get(caps, i) and (a == 2 or a == 3)
nextIdx := i
break
nextTxt = nextIdx >= 0 ? cleanSym(array.get(syms, nextIdx)) : "CASH"
nextCode = nextIdx >= 0 ? array.get(actions, nextIdx) : -1
nextAct = nextIdx >= 0 ? (nextCode == 3 ? "heavy buy" : "normal buy") : "nothing qualifies"
// ─────────────────────────────────────────────
// ALERTS — create ONE alert on this indicator: Condition → "Any alert() function call"
// ─────────────────────────────────────────────
var string lastNext = ""
var int lastNextCode = -2
var array<int> lastActs = array.new_int(10, -2)
if barstate.isconfirmed
// NEXT buy changed (e.g. ZGLD → VFV, or ZGLD → CASH)
if alNext and lastNext != "" and nextTxt != lastNext
alert("BB Ladder: NEXT changed " + lastNext + " → " + nextTxt + " (" + nextAct + ")", alert.freq_once_per_bar_close)
// Same NEXT asset, but sizing changed
else if alSize and lastNext != "" and nextTxt == lastNext and nextIdx >= 0 and nextCode != lastNextCode
alert("BB Ladder: " + nextTxt + " now " + actName(nextCode) + " (" + nextAct + ")", alert.freq_once_per_bar_close)
// Any asset newly flipped to SELL
if alSell
for i = 0 to 9
a = array.get(actions, i)
la = array.get(lastActs, i)
if array.get(ons, i) and a == 0 and la != 0 and la != -2
alert("BB Ladder: " + cleanSym(array.get(syms, i)) + " flipped to SELL — trim if over cap", alert.freq_once_per_bar_close)
// Optional daily reminder
if alDaily
alert("BB Ladder: today's NEXT = " + nextTxt + " (" + nextAct + ")", alert.freq_once_per_bar_close)
lastNext := nextTxt
lastNextCode := nextCode
for i = 0 to 9
array.set(lastActs, i, array.get(actions, i))
// ─────────────────────────────────────────────
// TABLE
// ─────────────────────────────────────────────
tblPos = switch tblPosIn
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
txtSize = switch tblSize
"Tiny" => size.tiny
"Normal" => size.normal
=> size.small
zoneTxt(z) =>
if na(z)
"—"
else if z <= -mult2
"≤ -" + str.tostring(mult2) + "σ"
else if z <= -mult1
"-" + str.tostring(mult1) + " to -" + str.tostring(mult2) + "σ"
else if z < mult1
"±" + str.tostring(mult1) + "σ"
else if z < mult2
"+" + str.tostring(mult1) + " to +" + str.tostring(mult2) + "σ"
else
"≥ +" + str.tostring(mult2) + "σ"
zoneCol(z) =>
na(z) ? color.gray : z <= -mult2 ? color.lime : z <= -mult1 ? color.green : z < mult1 ? color.gray : z < mult2 ? color.orange : color.red
var table tb = table.new(tblPos, 6, 12, border_width = 1)
if barstate.islast
table.clear(tb, 0, 0, 5, 11)
txt = chart.fg_color
hdrBg = color.new(color.gray, 40)
zHdr = bandMode == "Bollinger" ? "BB σ" : "VWAP σ"
table.cell(tb, 0, 0, "#", text_color = txt, bgcolor = hdrBg, text_size = txtSize)
table.cell(tb, 1, 0, "Asset", text_color = txt, bgcolor = hdrBg, text_size = txtSize)
table.cell(tb, 2, 0, "vs " + str.tostring(maLen), text_color = txt, bgcolor = hdrBg, text_size = txtSize)
table.cell(tb, 3, 0, zHdr, text_color = txt, bgcolor = hdrBg, text_size = txtSize)
table.cell(tb, 4, 0, "Zone", text_color = txt, bgcolor = hdrBg, text_size = txtSize)
table.cell(tb, 5, 0, "Action", text_color = txt, bgcolor = hdrBg, text_size = txtSize)
r = 0
for i = 0 to 9
if array.get(ons, i)
r += 1
p = array.get(px, i)
m = array.get(mas, i)
z = array.get(zs, i)
act = array.get(actions, i)
cap = array.get(caps, i)
pctStr = (na(p) or na(m)) ? "—" : ((p / m - 1) * 100 >= 0 ? "+" : "") + str.tostring((p / m - 1) * 100, "#.0") + "%"
zStr = na(z) ? "—" : (z >= 0 ? "+" : "") + str.tostring(z, "#.00")
actTxt = cap and (act == 2 or act == 3) ? actName(act) + " (CAP)" : actName(act)
actCol = cap ? color.gray : act == 3 ? color.lime : act == 2 ? color.green : act == 1 ? color.gray : act == 0 ? color.red : color.gray
rowBg = i == nextIdx ? color.new(color.green, 75) : cap ? color.new(color.gray, 80) : color.new(color.gray, 90)
table.cell(tb, 0, r, str.tostring(i + 1), text_color = txt, bgcolor = rowBg, text_size = txtSize)
table.cell(tb, 1, r, cleanSym(array.get(syms, i)), text_color = txt, bgcolor = rowBg, text_size = txtSize)
table.cell(tb, 2, r, pctStr, text_color = txt, bgcolor = rowBg, text_size = txtSize)
table.cell(tb, 3, r, zStr, text_color = zoneCol(z), bgcolor = rowBg, text_size = txtSize)
table.cell(tb, 4, r, zoneTxt(z), text_color = zoneCol(z), bgcolor = rowBg, text_size = txtSize)
table.cell(tb, 5, r, actTxt, text_color = actCol, bgcolor = rowBg, text_size = txtSize)
footRow = r + 1
footBg = nextIdx >= 0 ? color.new(color.green, 30) : color.new(color.blue, 50)
table.cell(tb, 0, footRow, "NEXT", text_color = color.white, bgcolor = footBg, text_size = txtSize)
table.cell(tb, 1, footRow, nextTxt, text_color = color.white, bgcolor = footBg, text_size = txtSize)
table.cell(tb, 2, footRow, nextAct, text_color = color.white, bgcolor = footBg, text_size = txtSize)
table.cell(tb, 3, footRow, "", bgcolor = footBg)
table.cell(tb, 4, footRow, "", bgcolor = footBg)
table.cell(tb, 5, footRow, "", bgcolor = footBg)