本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Volume-Based Liquidation Map", shorttitle="VBMap", overlay=true, max_boxes_count=500, max_labels_count=500)
// ════════════════════════════════════════════════════════════════
// LIQUIDATION RADAR
// 1) Right-edge heatmap: how much estimated liquidation has piled
// up at each price level (longer bar = denser zone)
// 2) Pull balance bar: total pull of the liquidation zones above
// vs. below price (strength / distance)
// 3) Explosion markers: when price touches a zone a 💥 appears,
// sized by the estimated liquidation strength
//
// NOTE: This is NOT real order book / open interest data. It is a
// volume-based estimate: bars with above-average volume are assumed
// to be where positions were opened.
// ════════════════════════════════════════════════════════════════
// ── Inputs ────────────────────────────────────────────────────
const string GRP_VOL = "Volume Detection"
const string GRP_LEV = "Leverage Levels"
const string GRP_HM = "Heatmap"
const string GRP_TUG = "Pull Balance Bar"
const string GRP_EX = "Explosion Markers"
volLen = input.int(50, "Volume average length", minval=5, group=GRP_VOL)
volMult = input.float(2.5, "Spike multiplier (x avg volume)", minval=1.0, step=0.1, group=GRP_VOL)
dirMode = input.string("By candle direction", "Position direction assumption", options=["By candle direction", "Both sides"], group=GRP_VOL, tooltip="By candle direction: a green spike = longs opened (long liquidation below), a red spike = shorts opened (short liquidation above).")
maxLevels = input.int(400, "Max active levels", minval=10, maxval=1500, group=GRP_VOL)
mmr = input.float(0.5, "Maintenance margin %", minval=0.0, maxval=5.0, step=0.1, group=GRP_LEV)
showL1 = input.bool(true, "Leverage 1", inline="l1", group=GRP_LEV)
lev1 = input.int(10, "", minval=2, maxval=125, inline="l1", group=GRP_LEV)
showL2 = input.bool(true, "Leverage 2", inline="l2", group=GRP_LEV)
lev2 = input.int(25, "", minval=2, maxval=125, inline="l2", group=GRP_LEV)
showL3 = input.bool(true, "Leverage 3", inline="l3", group=GRP_LEV)
lev3 = input.int(50, "", minval=2, maxval=125, inline="l3", group=GRP_LEV)
showL4 = input.bool(false, "Leverage 4", inline="l4", group=GRP_LEV)
lev4 = input.int(100, "", minval=2, maxval=125, inline="l4", group=GRP_LEV)
colLong = input.color(color.red, "Long liquidation color (below)", group=GRP_HM)
colShort = input.color(color.lime, "Short liquidation color (above)", group=GRP_HM)
nb = input.int(40, "Number of price buckets", minval=10, maxval=100, group=GRP_HM)
rangePct = input.float(10.0, "Coverage range % (± of price)", minval=1.0, step=0.5, group=GRP_HM)
maxLen = input.int(40, "Longest bar (bars)", minval=5, maxval=200, group=GRP_HM)
shift = input.int(5, "Right shift (bars)", minval=0, maxval=50, group=GRP_HM)
smoothOn = input.bool(true, "Smoothing", group=GRP_HM)
showMagnet = input.bool(true, "🧲 Label strongest zones", group=GRP_HM)
magColor = input.color(color.yellow, "Magnet label color", group=GRP_HM)
showTug = input.bool(true, "Show pull balance bar", group=GRP_TUG)
decayPct = input.float(3.0, "Pull decay distance %", minval=0.2, step=0.2, group=GRP_TUG, tooltip="A zone's pull drops to half when it is this far from price. Smaller value = only nearby zones matter.")
posStr = input.string("Top right", "Table position", options=["Top right", "Bottom right", "Top left", "Bottom left", "Top center", "Bottom center"], group=GRP_TUG)
showEx = input.bool(true, "💥 Show explosion markers", group=GRP_EX)
maxEx = input.int(100, "Max explosion markers", minval=10, maxval=300, group=GRP_EX)
const int NCELL = 20
tblPos = posStr == "Top right" ? position.top_right : posStr == "Bottom right" ? position.bottom_right : posStr == "Top left" ? position.top_left : posStr == "Bottom left" ? position.bottom_left : posStr == "Top center" ? position.top_center : position.bottom_center
// ── Data structure ────────────────────────────────────────────
type LiqLevel
float price
bool isLong
float strength
var array<LiqLevel> levels = array.new<LiqLevel>()
var array<int> levArr = array.new<int>()
var array<box> hmBoxes = array.new<box>()
var array<label> exLabels = array.new<label>()
var label magUpLb = na
var label magDnLb = na
var table tbl = table.new(tblPos, NCELL, 3, bgcolor=color.new(color.black, 30), frame_color=color.new(color.gray, 50), frame_width=1, border_width=0)
if barstate.isfirst
if showL1
levArr.push(lev1)
if showL2
levArr.push(lev2)
if showL3
levArr.push(lev3)
if showL4
levArr.push(lev4)
tbl.cell(0, 0, "🧲 Pull Balance", text_color=color.white, text_size=size.small)
tbl.cell(0, 2, "", text_color=color.white, text_size=size.small)
tbl.merge_cells(0, 0, NCELL - 1, 0)
tbl.merge_cells(0, 2, NCELL - 1, 2)
// ── Helper functions ──────────────────────────────────────────
liqPrice(float entry, bool isLong, int leverage) =>
isLong ? entry * (1.0 - 1.0 / leverage + mmr / 100.0) : entry * (1.0 + 1.0 / leverage - mmr / 100.0)
exLabel(float amt, bool isLong) =>
float r = amt / volMult
string sz = r >= 6.0 ? size.huge : r >= 3.0 ? size.large : r >= 1.5 ? size.normal : r >= 0.8 ? size.small : size.tiny
string tip = "Estimated " + (isLong ? "long" : "short") + " liquidation strength: " + str.tostring(amt, "#.#")
label.new(bar_index, isLong ? low : high, "💥", yloc=isLong ? yloc.belowbar : yloc.abovebar, style=label.style_none, textcolor=color.white, size=sz, tooltip=tip)
// ── Main logic (on bar close) ─────────────────────────────────
float avgVol = ta.sma(volume, volLen)
bool spike = volume > avgVol * volMult
bool bothSides = dirMode == "Both sides"
bool exLongNow = false
bool exShortNow = false
if barstate.isconfirmed
// 1) Find levels touched by price, trigger explosions, remove them
float exLong = 0.0
float exShort = 0.0
if levels.size() > 0
for i = levels.size() - 1 to 0
LiqLevel lv = levels.get(i)
bool hit = lv.isLong ? low <= lv.price : high >= lv.price
if hit
if lv.isLong
exLong += lv.strength
else
exShort += lv.strength
levels.remove(i)
exLongNow := exLong > 0.0
exShortNow := exShort > 0.0
if showEx
if exLongNow
exLabels.push(exLabel(exLong, true))
if exShortNow
exLabels.push(exLabel(exShort, false))
while exLabels.size() > maxEx
label.delete(exLabels.shift())
// 2) On a volume spike, add new liquidation levels
if spike
float entry = hlc3
bool bull = close >= open
float strength = volume / avgVol
for lv2 in levArr
if bothSides or bull
levels.push(LiqLevel.new(liqPrice(entry, true, lv2), true, strength))
if bothSides or not bull
levels.push(LiqLevel.new(liqPrice(entry, false, lv2), false, strength))
while levels.size() > maxLevels
levels.shift()
// ── Last bar: heatmap, magnet labels, pull balance bar ────────
if barstate.islast
for b in hmBoxes
box.delete(b)
hmBoxes.clear()
label.delete(magUpLb)
label.delete(magDnLb)
float lo = close * (1.0 - rangePct / 100.0)
float stp = close * 2.0 * rangePct / 100.0 / nb
array<float> w = array.new<float>(nb, 0.0)
float pullUp = 0.0
float pullDn = 0.0
for lv3 in levels
float dist = math.abs(lv3.price - close) / close * 100.0
float pull = lv3.strength / (1.0 + math.pow(dist / decayPct, 2))
if lv3.price > close
pullUp += pull
else
pullDn += pull
int idx = int(math.floor((lv3.price - lo) / stp))
if idx >= 0 and idx < nb
w.set(idx, w.get(idx) + lv3.strength)
// Smoothing
array<float> ws = array.new<float>(nb, 0.0)
for i = 0 to nb - 1
float wl = i > 0 ? w.get(i - 1) : 0.0
float wr = i < nb - 1 ? w.get(i + 1) : 0.0
ws.set(i, smoothOn ? 0.25 * wl + 0.5 * w.get(i) + 0.25 * wr : w.get(i))
float maxW = ws.max()
if maxW > 0.0
float bestUpW = 0.0
float bestUpMid = na
int bestUpLen = 0
float bestDnW = 0.0
float bestDnMid = na
int bestDnLen = 0
for i = 0 to nb - 1
float wi = ws.get(i)
if wi > 0.0
float bot = lo + i * stp
float top = bot + stp
float mid = (bot + top) / 2.0
bool above = mid > close
float ratio = wi / maxW
int blen = math.max(1, math.round(ratio * maxLen))
int transp = int(85.0 - 70.0 * ratio)
color c = color.new(above ? colShort : colLong, transp)
hmBoxes.push(box.new(bar_index + shift - blen, top, bar_index + shift, bot, border_color=na, bgcolor=c))
if above and wi > bestUpW
bestUpW := wi
bestUpMid := mid
bestUpLen := blen
if not above and wi > bestDnW
bestDnW := wi
bestDnMid := mid
bestDnLen := blen
if showMagnet
if not na(bestUpMid)
magUpLb := label.new(bar_index + shift - bestUpLen, bestUpMid, "🧲 +" + str.tostring((bestUpMid - close) / close * 100.0, "#.##") + "%", style=label.style_label_right, color=magColor, textcolor=color.black, size=size.small)
if not na(bestDnMid)
magDnLb := label.new(bar_index + shift - bestDnLen, bestDnMid, "🧲 -" + str.tostring((close - bestDnMid) / close * 100.0, "#.##") + "%", style=label.style_label_right, color=magColor, textcolor=color.black, size=size.small)
// Pull balance bar
if showTug
float tot = pullUp + pullDn
float upShare = tot > 0.0 ? pullUp / tot : 0.5
int upCells = int(math.round(upShare * NCELL))
int dnCells = NCELL - upCells
string bias = tot <= 0.0 ? "no levels yet" : upShare > 0.6 ? "upside dominates" : upShare < 0.4 ? "downside dominates" : "balanced"
string txt = tot <= 0.0 ? "—" : "▼ " + str.tostring(math.round((1.0 - upShare) * 100.0)) + "% · ▲ " + str.tostring(math.round(upShare * 100.0)) + "% → " + bias
tbl.cell(0, 0, "🧲 Pull Balance", text_color=color.white, text_size=size.small)
for i = 0 to NCELL - 1
color cc = tot <= 0.0 ? color.gray : (i < dnCells ? colLong : colShort)
tbl.cell(i, 1, "", width=1.0, height=1.0, bgcolor=cc)
tbl.cell(0, 2, txt, text_color=color.silver, text_size=size.small)
// ── Alerts ────────────────────────────────────────────────────
alertcondition(exLongNow, "Long liquidation zone exploded", "Price touched estimated long liquidation zones")
alertcondition(exShortNow, "Short liquidation zone exploded", "Price touched estimated short liquidation zones")