本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Smart Money Zones
// Draws an "Institutional Buy Zone" (blue) with a "Smart Money Buy Zone" (indigo) under it
// at the base of every strong rally that breaks a prior swing high.
// Mirror version for sell zones (slate + indigo on top) after a breakdown below a swing low.
// Zones extend to the right until price closes beyond them.
indicator('Smart Money Zones', overlay = true, max_boxes_count = 500, max_labels_count = 500, max_lines_count = 500, max_bars_back = 500)
// ---------- Inputs ----------
pivLen = input.int(5, 'Swing length (bars each side)', minval = 2)
dispMult = input.float(2.0, 'Rally strength needed (x ATR)', minval = 0.5, step = 0.1)
maxZones = input.int(4, 'Max active zones per side', minval = 1, maxval = 20)
showBear = input.bool(true, 'Show sell zones')
deleteBroken = input.bool(true, 'Remove zones once price closes through them')
showLabels = input.bool(true, 'Show swing high/low prices')
showChos = input.bool(true, 'Show ChoS (change of structure) lines')
// Cool, dark colors so the zones stand out on a warm red/orange chart background
colInst = input.color(color.new(#0d47a1, 40), 'Institutional Buy Zone', group = 'Colors')
colSmz = input.color(color.new(#311b92, 30), 'Smart Money Buy Zone', group = 'Colors')
colInstSell = input.color(color.new(#263238, 40), 'Institutional Sell Zone', group = 'Colors')
colSmzSell = input.color(color.new(#311b92, 30), 'Smart Money Sell Zone', group = 'Colors')
colText = input.color(color.white, 'Zone text', group = 'Colors')
colLabel = input.color(#0b1726, 'Swing price labels', group = 'Colors')
atr = ta.atr(14)
// ---------- Swing points ----------
ph = ta.pivothigh(high, pivLen, pivLen)
pl = ta.pivotlow(low, pivLen, pivLen)
if showLabels and not na(ph)
label.new(bar_index - pivLen, ph, str.tostring(ph, format.mintick), style = label.style_label_down, color = color.new(color.black, 100), textcolor = colLabel, size = size.small)
if showLabels and not na(pl)
label.new(bar_index - pivLen, pl, str.tostring(pl, format.mintick), style = label.style_label_up, color = color.new(color.black, 100), textcolor = colLabel, size = size.small)
var float lastPH = na
var int lastPHbar = na
var bool phUsed = true
var float lastPL = na
var int lastPLbar = na
var bool plUsed = true
if not na(ph)
lastPH := ph
lastPHbar := bar_index - pivLen
phUsed := false
phUsed
if not na(pl)
lastPL := pl
lastPLbar := bar_index - pivLen
plUsed := false
plUsed
var array<box> buyInst = array.new_box()
var array<box> buySmz = array.new_box()
var array<float> buyStop = array.new_float()
var array<box> sellInst = array.new_box()
var array<box> sellSmz = array.new_box()
var array<float> sellStop = array.new_float()
// ---------- New buy zone: close breaks above the last swing high ----------
if not phUsed and not na(lastPH) and close > lastPH
phUsed := true
span = math.min(bar_index - lastPHbar, 300)
// lowest point between the swing high and the breakout = origin of the rally
lo = 0
loVal = low
for i = 0 to span by 1
if low[i] < loVal
loVal := low[i]
lo := i
lo
// base candle: the last down candle at (or just before) that low
ob = lo
for j = lo to math.min(lo + 3, bar_index) by 1
if close[j] < open[j]
ob := j
break
obTop = high[ob]
smzTop = math.min(open[ob], close[ob])
if close - loVal >= dispMult * atr and obTop > loVal
// keep the smart money zone visible even when the base candle has almost no lower wick
if smzTop - loVal < 0.2 * (obTop - loVal)
smzTop := loVal + 0.25 * (obTop - loVal)
smzTop
left = bar_index - ob
array.push(buyInst, box.new(left, obTop, bar_index, smzTop, border_color = colInst, bgcolor = colInst, extend = extend.right, text = 'Institutional Buy Zone', text_color = colText, text_size = size.small))
array.push(buySmz, box.new(left, smzTop, bar_index, loVal, border_color = colSmz, bgcolor = colSmz, extend = extend.right, text = 'Smart Money Buy Zone', text_color = colText, text_size = size.small))
array.push(buyStop, loVal)
if showChos
line.new(lastPHbar, lastPH, bar_index, lastPH, color = colLabel, width = 1)
label.new(math.round((lastPHbar + bar_index) / 2), lastPH, 'ChoS', style = label.style_label_down, color = color.new(color.black, 100), textcolor = colLabel, size = size.small)
if array.size(buyStop) > maxZones
box.delete(array.shift(buyInst))
box.delete(array.shift(buySmz))
array.shift(buyStop)
// ---------- New sell zone: close breaks below the last swing low ----------
if showBear and not plUsed and not na(lastPL) and close < lastPL
plUsed := true
span = math.min(bar_index - lastPLbar, 300)
hi = 0
hiVal = high
for i = 0 to span by 1
if high[i] > hiVal
hiVal := high[i]
hi := i
hi
ob = hi
for j = hi to math.min(hi + 3, bar_index) by 1
if close[j] > open[j]
ob := j
break
obBot = low[ob]
smzBot = math.max(open[ob], close[ob])
if hiVal - close >= dispMult * atr and hiVal > obBot
if hiVal - smzBot < 0.2 * (hiVal - obBot)
smzBot := hiVal - 0.25 * (hiVal - obBot)
smzBot
left = bar_index - ob
array.push(sellInst, box.new(left, smzBot, bar_index, obBot, border_color = colInstSell, bgcolor = colInstSell, extend = extend.right, text = 'Institutional Sell Zone', text_color = colText, text_size = size.small))
array.push(sellSmz, box.new(left, hiVal, bar_index, smzBot, border_color = colSmzSell, bgcolor = colSmzSell, extend = extend.right, text = 'Smart Money Sell Zone', text_color = colText, text_size = size.small))
array.push(sellStop, hiVal)
if showChos
line.new(lastPLbar, lastPL, bar_index, lastPL, color = colLabel, width = 1)
label.new(math.round((lastPLbar + bar_index) / 2), lastPL, 'ChoS', style = label.style_label_up, color = color.new(color.black, 100), textcolor = colLabel, size = size.small)
if array.size(sellStop) > maxZones
box.delete(array.shift(sellInst))
box.delete(array.shift(sellSmz))
array.shift(sellStop)
// ---------- Retire broken zones ----------
retire(box bi, box bs) =>
if deleteBroken
box.delete(bi)
box.delete(bs)
else
box.set_extend(bi, extend.none)
box.set_right(bi, bar_index)
box.set_bgcolor(bi, color.new(color.gray, 85))
box.set_border_color(bi, color.new(color.gray, 85))
box.set_extend(bs, extend.none)
box.set_right(bs, bar_index)
box.set_bgcolor(bs, color.new(color.gray, 85))
box.set_border_color(bs, color.new(color.gray, 85))
if array.size(buyStop) > 0
for k = array.size(buyStop) - 1 to 0 by 1
if close < array.get(buyStop, k)
retire(array.get(buyInst, k), array.get(buySmz, k))
array.remove(buyInst, k)
array.remove(buySmz, k)
array.remove(buyStop, k)
if array.size(sellStop) > 0
for k = array.size(sellStop) - 1 to 0 by 1
if close > array.get(sellStop, k)
retire(array.get(sellInst, k), array.get(sellSmz, k))
array.remove(sellInst, k)
array.remove(sellSmz, k)
array.remove(sellStop, k)
// ---------- Alerts ----------
inInstBuy = false
inSmzBuy = false
if array.size(buyStop) > 0
for k = 0 to array.size(buyStop) - 1 by 1
bi = array.get(buyInst, k)
bs = array.get(buySmz, k)
if low <= box.get_top(bi) and high >= box.get_bottom(bi)
inInstBuy := true
inInstBuy
if low <= box.get_top(bs) and high >= box.get_bottom(bs)
inSmzBuy := true
inSmzBuy
inSell = false
if array.size(sellStop) > 0
for k = 0 to array.size(sellStop) - 1 by 1
if high >= box.get_bottom(array.get(sellInst, k)) and low <= array.get(sellStop, k)
inSell := true
inSell
alertcondition(inInstBuy and not inInstBuy[1], 'Entered Institutional Buy Zone', '{{ticker}} entered an Institutional Buy Zone')
alertcondition(inSmzBuy and not inSmzBuy[1], 'Entered Smart Money Buy Zone', '{{ticker}} entered a Smart Money Buy Zone')
alertcondition(inSell and not inSell[1], 'Entered Sell Zone', '{{ticker}} entered a Sell Zone')