本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Liquidity Heatmap", overlay = true, max_bars_back = 500, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
// ═════════════════════════════ INPUTS ═════════════════════════════
grpD = "Detection"
swingLen = input.int(5, "Swing length", minval = 1, maxval = 50, group = grpD)
mergeTol = input.float(0.25, "Merge levels closer than (x ATR)", minval = 0, step = 0.05, group = grpD)
bandMult = input.float(0.2, "Stop band thickness (x ATR)", minval = 0.01, step = 0.05, group = grpD)
useVol = input.bool(true, "Weight pools by volume", group = grpD)
maxPools = input.int(20, "Max pools per side", minval = 1, maxval = 100, group = grpD)
grpS = "Sweep signls"
minStrength = input.float(2.0, "Minimum pool strength for a signal", minval = 0, step = 0.5, group = grpS)
needReject = input.bool(true, "Require a rejection candle", group = grpS)
showSwept = input.bool(true, "Mark swept / taken levels", group = grpS)
grpV = "Display"
zoneExt = input.int(20, "Extend pools right (bars)", minval = 0, maxval = 200, group = grpV)
showText = input.bool(true, "Show strength on pools", group = grpV)
showProfile = input.bool(true, "Show liquidity profile on the right", group = grpV)
profLen = input.int(30, "Profile max width (bars)", minval = 5, maxval = 200, group = grpV)
coldCol = input.color(#2962ff, "Weak liquidity", group = grpV)
warmCol = input.color(#ffd600, "Medium liquidity", group = grpV)
hotCol = input.color(#ff1744, "Strong liquidity", group = grpV)
bullCol = input.color(#00c896, "Bullish signal", group = grpV)
bearCol = input.color(#ff4d5e, "Bearish signal", group = grpV)
showDash = input.bool(true, "Show dashboard", group = grpV)
dashPosIn = input.string("Top Right", "Dashboard position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = grpV)
// ═════════════════════════════ TYPES & STATE ═════════════════════════════
type Pool
float level
float strength
int touches
int firstBar
box bx
var array<Pool> highPools = array.new<Pool>() // buy-side liquidity (above highs)
var array<Pool> lowPools = array.new<Pool>() // sell-side liquidity (below lows)
var array<box> profBoxes = array.new<box>()
var string lastEvent = "—"
color clear = color.new(color.black, 100)
// ═════════════════════════════ CALCULATIONS ═════════════════════════════
float atr = ta.atr(14)
bool conf = barstate.isconfirmed
float ph = ta.pivothigh(high, swingLen, swingLen)
float pl = ta.pivotlow(low, swingLen, swingLen)
float volAvg = ta.sma(volume, 50)
float volRat = useVol and nz(volAvg) > 0 ? math.min(nz(volume) / volAvg, 3.0) : 1.0
// ═════════════════════════════ HELPERS ═════════════════════════════
// Heat colour: 0 = cold, 1 = hot. Stronger pools are also more opaque.
f_heat(float r, float transp) =>
color c = r < 0.5 ? color.from_gradient(r, 0.0, 0.5, coldCol, warmCol) : color.from_gradient(r, 0.5, 1.0, warmCol, hotCol)
color.new(c, transp)
// Add a swing as liquidity, merging it into a nearby pool on the same side
f_addPool(array<Pool> ps, float lvl, float w, bool above) =>
bool merged = false
float tol = atr * mergeTol
float band = atr * bandMult
for p in ps
if not merged and math.abs(p.level - lvl) <= tol
p.level := above ? math.max(p.level, lvl) : math.min(p.level, lvl)
p.strength := p.strength + w
p.touches := p.touches + 1
p.bx.set_top(above ? p.level + band : p.level)
p.bx.set_bottom(above ? p.level : p.level - band)
merged := true
if not merged
int left = bar_index - swingLen
box b = box.new(left, above ? lvl + band : lvl, bar_index + zoneExt, above ? lvl : lvl - band, border_color = clear, bgcolor = f_heat(0.3, 70), text_size = size.tiny, text_halign = text.align_right, text_color = color.white)
ps.push(Pool.new(lvl, w, 1, left, b))
if ps.size() > maxPools
Pool old = ps.shift()
old.bx.delete()
// Check pools against this bar. Swept = wick through, close back inside.
// Taken = close through. Either way the liquidity is used up and removed.
// Returns [strongest swept strength, its level, strongest taken strength]
f_check(array<Pool> ps, bool above) =>
float sweptS = 0.0
float sweptL = na
float takenS = 0.0
int n = ps.size()
if n > 0
for i = n - 1 to 0
Pool p = ps.get(i)
bool pierced = above ? high > p.level : low < p.level
if pierced
bool taken = above ? close > p.level : close < p.level
color mc = taken ? color.gray : (above ? bearCol : bullCol)
if showSwept
line.new(p.firstBar, p.level, bar_index, p.level, color = color.new(mc, 30), style = line.style_dotted)
label.new(bar_index, p.level, (taken ? "Taken " : "Swept ") + str.tostring(p.strength, "#.0"), style = above ? label.style_label_lower_left : label.style_label_upper_left, color = clear, textcolor = mc, size = size.tiny)
if taken
takenS := math.max(takenS, p.strength)
else if p.strength > sweptS
sweptS := p.strength
sweptL := p.level
p.bx.delete()
ps.remove(i)
[sweptS, sweptL, takenS]
// Nearest pool, strongest pool, and distance-weighted pull of one side
f_stats(array<Pool> ps) =>
float nearL = na
float nearS = na
float bestL = na
float bestS = 0.0
float pull = 0.0
for p in ps
float d = math.abs(p.level - close)
if na(nearL) or d < math.abs(nearL - close)
nearL := p.level
nearS := p.strength
if p.strength > bestS
bestS := p.strength
bestL := p.level
pull += p.strength / math.max(d / atr, 0.5)
[nearL, nearS, bestL, bestS, pull]
f_render(array<Pool> ps, float maxS) =>
for p in ps
float r = maxS > 0 ? p.strength / maxS : 0.0
p.bx.set_right(bar_index + zoneExt)
p.bx.set_bgcolor(f_heat(r, 80 - 50 * r))
p.bx.set_text(showText ? str.tostring(p.strength, "#.0") + (p.touches > 1 ? " ×" + str.tostring(p.touches) : "") : "")
if showProfile
int x1 = bar_index + zoneExt + 2
profBoxes.push(box.new(x1, p.bx.get_top(), x1 + math.max(1, int(profLen * r)), p.bx.get_bottom(), border_color = clear, bgcolor = f_heat(r, 15)))
// ═════════════════════════════ ENGINE (closed bars only) ═════════════════════════════
bool buySig = false
bool sellSig = false
bool bsTaken = false
bool ssTaken = false
float sigS = 0.0
if conf
// 1) Sweeps and breaks of existing pools
[hiSweptS, hiSweptL, hiTakenS] = f_check(highPools, true)
[loSweptS, loSweptL, loTakenS] = f_check(lowPools, false)
sellSig := hiSweptS >= minStrength and (not needReject or close < open)
buySig := loSweptS >= minStrength and (not needReject or close > open)
bsTaken := hiTakenS > 0
ssTaken := loTakenS > 0
if hiSweptS > 0
lastEvent := "Buy-side swept (" + str.tostring(hiSweptS, "#.0") + ")"
if loSweptS > 0
lastEvent := "Sell-side swept (" + str.tostring(loSweptS, "#.0") + ")"
if bsTaken
lastEvent := "Buy-side taken (" + str.tostring(hiTakenS, "#.0") + ")"
if ssTaken
lastEvent := "Sell-side taken (" + str.tostring(loTakenS, "#.0") + ")"
if sellSig
sigS := hiSweptS
alert("LiqHeat SELL " + syminfo.ticker + " (" + timeframe.period + ") — buy-side liquidity swept at " + str.tostring(hiSweptL, format.mintick) + ", strength " + str.tostring(hiSweptS, "#.0"), alert.freq_once_per_bar_close)
if buySig
sigS := loSweptS
alert("LiqHeat BUY " + syminfo.ticker + " (" + timeframe.period + ") — sell-side liquidity swept at " + str.tostring(loSweptL, format.mintick) + ", strength " + str.tostring(loSweptS, "#.0"), alert.freq_once_per_bar_close)
if bsTaken or ssTaken
alert("LiqHeat " + syminfo.ticker + " (" + timeframe.period + "): " + lastEvent, alert.freq_once_per_bar_close)
// 2) New liquidity from confirmed swings (weight = base + volume at the swing)
float w = useVol ? 0.5 + nz(volRat[swingLen], 1.0) : 1.0
if not na(ph)
f_addPool(highPools, ph, w, true)
if not na(pl)
f_addPool(lowPools, pl, w, false)
// ═════════════════════════════ HEATMAP RENDER (last bar) ═════════════════════════════
if barstate.islast
float maxS = 0.0
for p in highPools
maxS := math.max(maxS, p.strength)
for p in lowPools
maxS := math.max(maxS, p.strength)
for b in profBoxes
b.delete()
profBoxes.clear()
f_render(highPools, maxS)
f_render(lowPools, maxS)
// ═════════════════════════════ SIGNALS & ALERT CONDITIONS ═════════════════════════════
plotshape(sellSig, "Sweep sell", shape.labeldown, location.abovebar, bearCol, text = "SELL\nsweep", textcolor = color.white, size = size.small)
plotshape(buySig, "Sweep buy", shape.labelup, location.belowbar, bullCol, text = "BUY\nsweep", textcolor = color.white, size = size.small)
alertcondition(buySig, "Buy: sell-side liquidity swept", "LiqHeat BUY {{ticker}} at {{close}}")
alertcondition(sellSig, "Sell: buy-side liquidity swept", "LiqHeat SELL {{ticker}} at {{close}}")
alertcondition(bsTaken, "Buy-side liquidity taken", "LiqHeat buy-side liquidity taken on {{ticker}}")
alertcondition(ssTaken, "Sell-side liquidity taken", "LiqHeat sell-side liquidity taken on {{ticker}}")
// ═════════════════════════════ DASHBOARD ═════════════════════════════
string dashPos = switch dashPosIn
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
var table dash = table.new(dashPos, 4, 8, bgcolor = color.new(#0e1117, 8), frame_color = color.new(color.gray, 60), frame_width = 1, border_color = color.new(color.gray, 80), border_width = 1)
f_cell(int c, int r, string txt, color tc) =>
table.cell(dash, c, r, txt, text_color = tc, text_size = size.small, text_halign = c == 0 ? text.align_left : text.align_right)
f_lvl(float l) =>
na(l) ? "—" : str.tostring(l, format.mintick)
f_dist(float l) =>
na(l) ? "—" : str.tostring(math.abs(l - close) / atr, "#.00")
f_str(float s) =>
na(s) or s == 0 ? "—" : str.tostring(s, "#.0")
if showDash and barstate.islast
color neutral = color.new(color.white, 10)
color head = color.new(color.white, 40)
[aNearL, aNearS, aBestL, aBestS, aPull] = f_stats(highPools)
[bNearL, bNearS, bBestL, bBestS, bPull] = f_stats(lowPools)
float total = aPull + bPull
float upPct = total > 0 ? 100 * aPull / total : 50
string magnet = total == 0 ? "—" : upPct >= 55 ? "▲ Above " + str.tostring(upPct, "#") + "%" : upPct <= 45 ? "▼ Below " + str.tostring(100 - upPct, "#") + "%" : "Balanced"
color magCol = upPct >= 55 ? hotCol : upPct <= 45 ? coldCol : neutral
f_cell(0, 0, "LIQUIDITY HEATMAP", color.white)
f_cell(1, 0, "Level", head)
f_cell(2, 0, "ATR away", head)
f_cell(3, 0, "Strength", head)
f_cell(0, 1, "Nearest above", bearCol)
f_cell(1, 1, f_lvl(aNearL), neutral)
f_cell(2, 1, f_dist(aNearL), neutral)
f_cell(3, 1, f_str(aNearS), neutral)
f_cell(0, 2, "Strongest above", bearCol)
f_cell(1, 2, f_lvl(aBestL), neutral)
f_cell(2, 2, f_dist(aBestL), neutral)
f_cell(3, 2, f_str(aBestS), neutral)
f_cell(0, 3, "Nearest below", bullCol)
f_cell(1, 3, f_lvl(bNearL), neutral)
f_cell(2, 3, f_dist(bNearL), neutral)
f_cell(3, 3, f_str(bNearS), neutral)
f_cell(0, 4, "Strongest below", bullCol)
f_cell(1, 4, f_lvl(bBestL), neutral)
f_cell(2, 4, f_dist(bBestL), neutral)
f_cell(3, 4, f_str(bBestS), neutral)
f_cell(0, 5, "Liquidity magnet", neutral)
f_cell(1, 5, magnet, magCol)
f_cell(2, 5, "", neutral)
f_cell(3, 5, "", neutral)
f_cell(2, 5, "Active pools", neutral)
f_cell(3, 5, str.tostring(highPools.size()) + " above / " + str.tostring(lowPools.size()) + " below", neutral)
//f_cell(2, 6, "", neutral)
//f_cell(3, 6, "", neutral)
//f_cell(0, 7, "Last event", neutral)
//f_cell(1, 7, lastEvent, neutral)
//f_cell(2, 7, "", neutral)
//f_cell(3, 7, "", neutral)