本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// ============================================================================
// Liquidity Heatmap, Liquidation Levels & Clusters [LunqFX]
// ----------------------------------------------------------------------------
// Every bar is treated as a place where leveraged longs and shorts were
// opened. For each leverage tier the script projects where those positions
// would be liquidated, removes every level price has already traded through,
// and paints what is left as a heatmap: the brighter the band, the more
// untouched liquidation fuel is stacked there. A side profile ranks the
// bands, and the two biggest clusters above and below price are labelled.
//
// This is a model of where liquidations would sit, built from price and
// (optionally) volume. It is not exchange liquidation data.
// ============================================================================
indicator("Liquidity Heatmap, Liquidation Levels & Clusters [LunqFX]", "Liquidity Heatmap [LunqFX]",
overlay = true, max_bars_back = 1500, max_lines_count = 500, max_labels_count = 50, max_boxes_count = 200)
// ─────────────────────────────────────────────────────────────────────────
// INPUTS
// ─────────────────────────────────────────────────────────────────────────
gL = "Leverage"
lb = input.int(400, "Bars of history", minval = 50, maxval = 1400, group = gL, tooltip = "How many bars back positions are modelled. Levels older than this are not drawn.")
useL1 = input.bool(true, "", inline = "l1", group = gL)
lev1 = input.float(25, "Tier 1 ×", minval = 2, maxval = 500, inline = "l1", group = gL)
useL2 = input.bool(true, "", inline = "l2", group = gL)
lev2 = input.float(50, "Tier 2 ×", minval = 2, maxval = 500, inline = "l2", group = gL)
useL3 = input.bool(true, "", inline = "l3", group = gL)
lev3 = input.float(100, "Tier 3 ×", minval = 2, maxval = 500, inline = "l3", group = gL,
tooltip = "Liquidation distance = 1 / leverage from the bar's close (25× = 4%, 50× = 2%, 100× = 1%). Crypto traders use 25–100×; for forex and gold try 100–500×.")
weightBy = input.string("Volume", "Weight each bar by", options = ["Volume", "Range", "Equal"], group = gL,
tooltip = "Volume: bars that traded more open more positions. Range: bigger candles count more (for symbols without volume). Equal: every bar counts the same.")
gH = "Heatmap"
rows = input.int(80, "Price rows", minval = 20, maxval = 150, group = gH, tooltip = "How finely the price axis is split when the levels are summed into heat.")
minHeat = input.int(15, "Hide bands cooler than (%)", minval = 0, maxval = 95, group = gH)
maxLines = input.int(350, "Max levels drawn", minval = 50, maxval = 480, group = gH)
showProf = input.bool(true, "Side profile", group = gH)
profW = input.int(40, "Profile width (bars)", minval = 10, maxval = 150, group = gH)
showTop = input.bool(true, "Label the biggest cluster above and below", group = gH)
gP = "Panel"
showTbl = input.bool(true, "Show panel", group = gP)
tblPos = input.string("Top Right", "Position", options = ["Top Right", "Bottom Right", "Top Left", "Bottom Left"], group = gP)
tblSize = input.string("Normal", "Text size", options = ["Small", "Normal", "Large"], group = gP)
gA = "Alerts"
alNear = input.float(0.3, "Alert when a top cluster is within (%)", minval = 0.05, maxval = 5, step = 0.05, group = gA)
candOn_ = input.bool(true, "Neon candles", group = "Visuals")
candGlw = input.bool(true, "Glow candles", group = "Visuals", tooltip = "Each body is drawn in four bands that brighten towards the close. Off = flat translucent bodies.")
// ─────────────────────────────────────────────────────────────────────────
// THEME — inferno heat on graphite candles
// ─────────────────────────────────────────────────────────────────────────
bgLum = 0.2126 * color.r(chart.bg_color) + 0.7152 * color.g(chart.bg_color) + 0.0722 * color.b(chart.bg_color)
isLight = bgLum > 128
H0 = #3B0F70 // cold
H1 = #C42E7A // warm
H2 = #FF7A1F // hot
H3 = #FFE45C // white-hot
TXT = isLight ? #1A1D24 : #E8EAF0
MUTE = isLight ? #5E6472 : #8F96A6
PANEL = isLight ? #FFFFFF : #0A0B10
ROW = isLight ? #EEF0F5 : #15151F
INK = #0A0B10
tSize = tblSize == "Small" ? size.small : tblSize == "Large" ? size.large : size.normal
heatCol(float h, int tr) =>
c = h < 0.33 ? color.from_gradient(h, 0.0, 0.33, H0, H1) : h < 0.66 ? color.from_gradient(h, 0.33, 0.66, H1, H2) : color.from_gradient(h, 0.66, 1.0, H2, H3)
color.new(c, tr)
px(float v) =>
str.tostring(v, format.mintick)
// ─────────────────────────────────────────────────────────────────────────
// THE MAP — rebuilt on the last bar
// ─────────────────────────────────────────────────────────────────────────
var array<line> lns = array.new<line>()
var array<box> prof = array.new<box>()
var array<label> lbs = array.new<label>()
var table tb = na
// summary for the panel and alerts
var float upP = na
var float upH = na
var float dnP = na
var float dnH = na
var float fuelUp = 0.0
var float fuelDn = 0.0
if barstate.islast
for o in lns
line.delete(o)
for o in prof
box.delete(o)
for o in lbs
label.delete(o)
array.clear(lns)
array.clear(prof)
array.clear(lbs)
tiers = array.new_float()
if useL1
array.push(tiers, lev1)
if useL2
array.push(tiers, lev2)
if useL3
array.push(tiers, lev3)
lvP = array.new_float() // price of every level still alive
lvW = array.new_float() // its weight
lvB = array.new_int() // bar it was opened on
lvS = array.new_int() // 1 = short liquidation (above), -1 = long liquidation (below)
n = math.max(1, math.min(lb, bar_index))
// walk back from the newest bar; keep the extremes traded AFTER each bar,
// so a level is alive only if no later bar has reached it
hiAfter = high
loAfter = low
for i = 1 to n
w = weightBy == "Volume" ? nz(volume[i], 1.0) : weightBy == "Range" ? high[i] - low[i] : 1.0
for t in tiers
lp = close[i] * (1 - 1 / t)
sp = close[i] * (1 + 1 / t)
if loAfter > lp
array.push(lvP, lp)
array.push(lvW, w)
array.push(lvB, bar_index - i)
array.push(lvS, -1)
if hiAfter < sp
array.push(lvP, sp)
array.push(lvW, w)
array.push(lvB, bar_index - i)
array.push(lvS, 1)
hiAfter := math.max(hiAfter, high[i])
loAfter := math.min(loAfter, low[i])
m = array.size(lvP)
if m > 0
lo = array.min(lvP)
hi = array.max(lvP)
step = (hi - lo) / rows
heat = array.new_float(rows, 0.0)
for k = 0 to m - 1
r = step > 0 ? math.min(rows - 1, int((array.get(lvP, k) - lo) / step)) : 0
array.set(heat, r, array.get(heat, r) + array.get(lvW, k))
hMax = array.max(heat)
// biggest band above and below the current close
upP := na
upH := na
dnP := na
dnH := na
fuelUp := 0.0
fuelDn := 0.0
for r = 0 to rows - 1
v = array.get(heat, r)
mid = lo + (r + 0.5) * step
if mid > close
fuelUp += v
if na(upH) or v > upH
upH := v
upP := mid
else
fuelDn += v
if na(dnH) or v > dnH
dnH := v
dnP := mid
// levels: the heaviest first, only in bands warm enough
idx = array.sort_indices(lvW, order.descending)
drawn = 0
for k = 0 to m - 1
if drawn >= maxLines
break
j = array.get(idx, k)
p = array.get(lvP, j)
r = step > 0 ? math.min(rows - 1, int((p - lo) / step)) : 0
h = hMax > 0 ? array.get(heat, r) / hMax : 0
if h * 100 >= minHeat
array.push(lns, line.new(array.get(lvB, j), p, bar_index, p, color = heatCol(h, int(85 - 70 * h)), width = h > 0.66 ? 2 : 1))
drawn += 1
// side profile
if showProf
for r = 0 to rows - 1
h = hMax > 0 ? array.get(heat, r) / hMax : 0
if h * 100 >= minHeat
x0 = bar_index + 3
array.push(prof, box.new(x0, lo + (r + 1) * step, x0 + math.max(1, int(profW * h)), lo + r * step,
border_color = color.new(heatCol(h, 0), 100), bgcolor = heatCol(h, int(60 - 50 * h))))
// the two clusters that matter most right now
if showTop and not na(upP)
array.push(lbs, label.new(bar_index + 3 + profW + 2, upP, "▲ SHORT LIQ CLUSTER " + px(upP) + " · " + str.tostring((upP / close - 1) * 100, "0.00") + "%",
style = label.style_label_left, color = heatCol(upH / hMax, 0), textcolor = INK, size = size.small))
if showTop and not na(dnP)
array.push(lbs, label.new(bar_index + 3 + profW + 2, dnP, "▼ LONG LIQ CLUSTER " + px(dnP) + " · " + str.tostring((1 - dnP / close) * 100, "0.00") + "%",
style = label.style_label_left, color = heatCol(dnH / hMax, 0), textcolor = INK, size = size.small))
// panel
if showTbl
pos = tblPos == "Top Right" ? position.top_right : tblPos == "Bottom Right" ? position.bottom_right : tblPos == "Top Left" ? position.top_left : position.bottom_left
table.delete(tb)
tb := table.new(pos, 2, 8, bgcolor = PANEL, border_color = PANEL, border_width = 2, frame_color = color.new(H2, 30), frame_width = 1)
tot = fuelUp + fuelDn
shUp = tot > 0 ? fuelUp / tot : 0.5
table.cell(tb, 0, 0, "LIQUIDITY HEATMAP", text_color = H3, bgcolor = PANEL, text_size = tSize, text_halign = text.align_left)
table.cell(tb, 1, 0, str.tostring(m) + " live levels", text_color = MUTE, bgcolor = PANEL, text_size = tSize, text_halign = text.align_right)
barTxt = ""
for k = 1 to 10
barTxt += k <= math.round(shUp * 10) ? "█" : "░"
table.cell(tb, 0, 1, "Fuel above · below", text_color = MUTE, bgcolor = ROW, text_size = tSize, text_halign = text.align_left)
table.cell(tb, 1, 1, str.tostring(shUp * 100, "0") + "% " + barTxt + " " + str.tostring((1 - shUp) * 100, "0") + "%", text_color = TXT, bgcolor = ROW, text_size = tSize, text_halign = text.align_right)
table.cell(tb, 0, 2, "Biggest cluster above", text_color = MUTE, bgcolor = ROW, text_size = tSize, text_halign = text.align_left)
table.cell(tb, 1, 2, na(upP) ? "—" : px(upP) + " · +" + str.tostring((upP / close - 1) * 100, "0.00") + "%", text_color = na(upP) ? MUTE : heatCol(upH / hMax, 0), bgcolor = ROW, text_size = tSize, text_halign = text.align_right)
table.cell(tb, 0, 3, "Biggest cluster below", text_color = MUTE, bgcolor = ROW, text_size = tSize, text_halign = text.align_left)
table.cell(tb, 1, 3, na(dnP) ? "—" : px(dnP) + " · −" + str.tostring((1 - dnP / close) * 100, "0.00") + "%", text_color = na(dnP) ? MUTE : heatCol(dnH / hMax, 0), bgcolor = ROW, text_size = tSize, text_halign = text.align_right)
near = na(upP) and na(dnP) ? "—" : na(dnP) or (not na(upP) and upP / close - 1 < 1 - dnP / close) ? "above · " + px(upP) : "below · " + px(dnP)
table.cell(tb, 0, 4, "Nearer of the two", text_color = MUTE, bgcolor = ROW, text_size = tSize, text_halign = text.align_left)
table.cell(tb, 1, 4, near, text_color = TXT, bgcolor = ROW, text_size = tSize, text_halign = text.align_right)
tierTxt = (useL1 ? str.tostring(lev1, "#") + "× " : "") + (useL2 ? str.tostring(lev2, "#") + "× " : "") + (useL3 ? str.tostring(lev3, "#") + "×" : "")
table.cell(tb, 0, 5, "Leverage tiers", text_color = MUTE, bgcolor = ROW, text_size = tSize, text_halign = text.align_left)
table.cell(tb, 1, 5, tierTxt + " · " + weightBy, text_color = TXT, bgcolor = ROW, text_size = tSize, text_halign = text.align_right)
table.cell(tb, 0, 6, "Heat scale", text_color = MUTE, bgcolor = PANEL, text_size = tSize, text_halign = text.align_left)
table.cell(tb, 1, 6, "cold ▁▂▃▄▅▆▇█ hot", text_color = H2, bgcolor = PANEL, text_size = tSize, text_halign = text.align_right)
table.cell(tb, 0, 7, "Modelled from price, not exchange data", text_color = MUTE, bgcolor = PANEL, text_size = size.tiny, text_halign = text.align_left)
table.merge_cells(tb, 0, 7, 1, 7)
// ─────────────────────────────────────────────────────────────────────────
// ALERTS — price approaching one of the two biggest clusters
// ─────────────────────────────────────────────────────────────────────────
nearUp = not na(upP) and (upP / close - 1) * 100 <= alNear
nearDn = not na(dnP) and (1 - dnP / close) * 100 <= alNear
if barstate.islast and barstate.isconfirmed and nearUp
alert(syminfo.ticker + " within " + str.tostring(alNear) + "% of the biggest short-liquidation cluster " + px(upP), alert.freq_once_per_bar_close)
if barstate.islast and barstate.isconfirmed and nearDn
alert(syminfo.ticker + " within " + str.tostring(alNear) + "% of the biggest long-liquidation cluster " + px(dnP), alert.freq_once_per_bar_close)
// ─────────────────────────────────────────────────────────────────────────
// GLOW CANDLES — graphite / white, so the heat is what stands out
// ─────────────────────────────────────────────────────────────────────────
GLOW_UP_DARK = #E8EAF0
GLOW_DN_DARK = #5A6070
GLOW_UP_LIGHT = #1A1D24
GLOW_DN_LIGHT = #9AA0AE
glowUp = isLight ? GLOW_UP_LIGHT : GLOW_UP_DARK
glowDn = isLight ? GLOW_DN_LIGHT : GLOW_DN_DARK
glowEdge = close >= open ? glowUp : glowDn
glowOn = candOn_ and candGlw
GLOW_NONE = color.new(color.black, 100)
plotcandle(candOn_ and not glowOn ? open : na, high, low, close, "Candles", color = color.new(glowEdge, 24), wickcolor = color.new(glowEdge, 18), bordercolor = glowEdge, display = display.pane)
gQ = (close - open) / 4.0
plotcandle(glowOn ? open : na, glowOn ? math.max(open, close) : na, glowOn ? math.min(open, close) : na, glowOn ? close : na, "Glow mask", color = chart.bg_color, wickcolor = GLOW_NONE, bordercolor = chart.bg_color, display = display.pane)
plotcandle(glowOn ? open : na, glowOn ? math.max(open, open + gQ) : na, glowOn ? math.min(open, open + gQ) : na, glowOn ? open + gQ : na, "Glow band 1", color = color.new(glowEdge, 78), wickcolor = GLOW_NONE, bordercolor = GLOW_NONE, display = display.pane)
plotcandle(glowOn ? open + gQ : na, glowOn ? math.max(open + gQ, open + 2 * gQ) : na, glowOn ? math.min(open + gQ, open + 2 * gQ) : na, glowOn ? open + 2 * gQ : na, "Glow band 2", color = color.new(glowEdge, 58), wickcolor = GLOW_NONE, bordercolor = GLOW_NONE, display = display.pane)
plotcandle(glowOn ? open + 2 * gQ : na, glowOn ? math.max(open + 2 * gQ, open + 3 * gQ) : na, glowOn ? math.min(open + 2 * gQ, open + 3 * gQ) : na, glowOn ? open + 3 * gQ : na, "Glow band 3", color = color.new(glowEdge, 36), wickcolor = GLOW_NONE, bordercolor = GLOW_NONE, display = display.pane)
plotcandle(glowOn ? open + 3 * gQ : na, glowOn ? math.max(open + 3 * gQ, close) : na, glowOn ? math.min(open + 3 * gQ, close) : na, glowOn ? close : na, "Glow band 4", color = color.new(glowEdge, 8), wickcolor = GLOW_NONE, bordercolor = GLOW_NONE, display = display.pane)
plotcandle(glowOn ? open : na, glowOn ? high : na, glowOn ? low : na, glowOn ? close : na, "Glow wick", color = GLOW_NONE, wickcolor = color.new(glowEdge, 10), bordercolor = color.new(glowEdge, 30), display = display.pane)