Liquidity Heater - TradingBox

Trading_Box · study · 190 行 · 点赞 244 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradingBox
 
//@version=6
indicator("Liquidity Heater - TradingBox", overlay = true, max_boxes_count = 500, max_lines_count = 100, max_labels_count = 100, max_bars_back = 1000)
 
// ───────────────────────────── Inputs ─────────────────────────────
G_H = "Heatmap"
pivLen   = input.int(3, "Swing sensitivity", minval = 1, maxval = 20, group = G_H, tooltip = "Lower = more, smaller swings")
lookback = input.int(300, "Lookback bars", minval = 50, maxval = 1000, group = G_H)
rowsN    = input.int(60, "Rows", minval = 10, maxval = 150, group = G_H)
decay    = input.int(200, "Fade (bars)", minval = 10, maxval = 2000, group = G_H, tooltip = "How fast old liquidity cools down")
volW     = input.bool(true, "Weight by volume", group = G_H)
minHeat  = input.float(8, "Hide rows below (% of max)", minval = 0, maxval = 90, group = G_H)
coldCol  = input.color(color.new(#1E3A8A, 90), "Cold", group = G_H, inline = "c")
warmCol  = input.color(color.new(#F59E0B, 65), "Warm", group = G_H, inline = "c")
hotCol   = input.color(color.new(#FF1744, 35), "Hot", group = G_H, inline = "c")
 
G_P = "Profile & magnets"
showProf = input.bool(true, "Heat profile on the right", group = G_P)
profW    = input.int(30, "Profile width (bars)", minval = 5, maxval = 150, group = G_P)
magnets  = input.int(3, "Magnet levels", minval = 0, maxval = 6, group = G_P)
showSw   = input.bool(true, "Mark sweeps of hot liquidity", group = G_P)
 
G_DB = "Dashboard"
showDash = input.bool(true, "Show dashboard", group = G_DB)
dashPos  = input.string("Top Right", "Position", options = ["Top Right", "Bottom Right", "Top Left", "Bottom Left"], group = G_DB)
 
// ───────────────────────────── Liquidity pools ─────────────────────────────
var array<float> pPrice = array.new<float>()
var array<float> pBase  = array.new<float>()
var array<int>   pBar   = array.new<int>()
var array<int>   pDir   = array.new<int>()
 
volAvg = ta.sma(nz(volume), 50)
 
// Remove liquidity that price has taken (closed candles only).
bool sweptUp   = false
bool sweptDn   = false
float sweptW   = 0.0
if barstate.isconfirmed and pPrice.size() > 0
    for i = pPrice.size() - 1 to 0
        float pr = pPrice.get(i)
        int   d  = pDir.get(i)
        if (d == 1 and high > pr) or (d == -1 and low < pr)
            float w = pBase.get(i) * math.exp(-(bar_index - pBar.get(i)) / decay)
            sweptW += w
            if d == 1
                sweptUp := true
            else
                sweptDn := true
            pPrice.remove(i)
            pBase.remove(i)
            pBar.remove(i)
            pDir.remove(i)
 
// Add new pools.
ph = ta.pivothigh(high, pivLen, pivLen)
pl = ta.pivotlow(low, pivLen, pivLen)
float baseW = volW and volAvg > 0 ? math.max(0.2, nz(volume[pivLen]) / volAvg) : 1.0
if not na(ph)
    pPrice.push(ph)
    pBase.push(baseW)
    pBar.push(bar_index - pivLen)
    pDir.push(1)
if not na(pl)
    pPrice.push(pl)
    pBase.push(baseW)
    pBar.push(bar_index - pivLen)
    pDir.push(-1)
while pPrice.size() > 600
    pPrice.shift()
    pBase.shift()
    pBar.shift()
    pDir.shift()
 
// Sweep of hot liquidity = swept weight well above a normal pool.
hotSweep = sweptW >= 1.5
plotshape(showSw and sweptUp and hotSweep, "Hot buy-side swept", shape.xcross, location.abovebar, hotCol, size = size.tiny)
plotshape(showSw and sweptDn and hotSweep, "Hot sell-side swept", shape.xcross, location.belowbar, hotCol, size = size.tiny)
 
// ───────────────────────────── Rendering ─────────────────────────────
rTop = ta.highest(high, lookback)
rBot = ta.lowest(low, lookback)
 
var array<box>   drawnBoxes  = array.new<box>()
var array<line>  drawnLines  = array.new<line>()
var array<label> drawnLabels = array.new<label>()
 
var float heatAbove = 0.0
var float heatBelow = 0.0
var float mag1      = na
 
f_heatCol(float t) =>
    t < 0.5 ? color.from_gradient(t, 0, 0.5, coldCol, warmCol) : color.from_gradient(t, 0.5, 1, warmCol, hotCol)
 
if barstate.islast
    for b in drawnBoxes
        b.delete()
    for l in drawnLines
        l.delete()
    for l in drawnLabels
        l.delete()
    drawnBoxes.clear()
    drawnLines.clear()
    drawnLabels.clear()
    heatAbove := 0.0
    heatBelow := 0.0
    mag1      := na
    float step = (rTop - rBot) / rowsN
    if step > 0 and pPrice.size() > 0
        array<float> rows = array.new<float>(rowsN, 0.0)
        for i = 0 to pPrice.size() - 1
            int   age = bar_index - pBar.get(i)
            float pr  = pPrice.get(i)
            if age <= lookback and pr <= rTop and pr >= rBot
                float w = pBase.get(i) * math.exp(-age / decay)
                int   r = math.min(rowsN - 1, int(math.floor((pr - rBot) / step)))
                rows.set(r, rows.get(r) + w)
                if r > 0
                    rows.set(r - 1, rows.get(r - 1) + w * 0.35)
                if r < rowsN - 1
                    rows.set(r + 1, rows.get(r + 1) + w * 0.35)
        float maxW = rows.max()
        if maxW > 0
            int x1   = bar_index - lookback + 1
            int x2   = bar_index + 3
            int pX   = bar_index + 6
            for r = 0 to rowsN - 1
                float w  = rows.get(r)
                float t  = w / maxW
                float y1 = rBot + step * (r + 1)
                float y0 = rBot + step * r
                if (y0 + y1) / 2 > close
                    heatAbove += w
                else
                    heatBelow += w
                if t * 100 >= minHeat
                    color c = f_heatCol(t)
                    drawnBoxes.push(box.new(x1, y1, x2, y0, border_color = color(na), bgcolor = c))
                    if showProf
                        int len = math.max(1, int(math.round(t * profW)))
                        drawnBoxes.push(box.new(pX, y1, pX + len, y0, border_color = color(na), bgcolor = color.new(c, math.max(0, color.t(c) - 25))))
            if magnets > 0
                array<int> rank = rows.sort_indices(order.descending)
                int shown = 0
                for k = 0 to rowsN - 1
                    if shown >= magnets
                        break
                    int   r  = rank.get(k)
                    float w  = rows.get(r)
                    if w <= 0
                        break
                    float y  = rBot + step * (r + 0.5)
                    float pc = (y - close) / close * 100
                    if shown == 0
                        mag1 := y
                    drawnLines.push(line.new(x1, y, pX + profW, y, color = color.new(hotCol, shown == 0 ? 0 : 40), width = shown == 0 ? 2 : 1, style = shown == 0 ? line.style_solid : line.style_dashed))
                    drawnLabels.push(label.new(pX + profW, y, "🧲 " + str.tostring(shown + 1) + "  " + str.tostring(y, format.mintick) + "  (" + str.tostring(pc, "+0.00;-0.00") + "%)", color = color.new(#131722, 20), textcolor = color.white, style = label.style_label_left, size = size.small))
                    shown += 1
 
// ───────────────────────────── Dashboard ─────────────────────────────
tPos = switch dashPos
    "Top Right"    => position.top_right
    "Bottom Right" => position.bottom_right
    "Top Left"     => position.top_left
    => position.bottom_left
 
var table dash = table.new(tPos, 2, 5, bgcolor = color.new(#0B0F19, 5), frame_color = color.new(#F59E0B, 40), frame_width = 1, border_color = color.new(#787B86, 85), border_width = 1)
 
if showDash and barstate.islast
    float tot   = heatAbove + heatBelow
    float upPct = tot > 0 ? heatAbove / tot * 100 : 50.0
    int   bars  = 10
    int   upN   = int(math.round(upPct / 100 * bars))
    string meter = str.repeat("█", upN) + str.repeat("░", bars - upN)
    dash.cell(0, 0, "🔥 TB HEAT", text_color = #F59E0B, text_size = size.small, text_halign = text.align_left)
    dash.cell(1, 0, timeframe.period, text_color = #787B86, text_size = size.small, text_halign = text.align_right)
    dash.cell(0, 1, "Liquidity above", text_color = #D1D4DC, text_size = size.small, text_halign = text.align_left)
    dash.cell(1, 1, str.tostring(upPct, "0") + "%", text_color = #FF1744, text_size = size.small, text_halign = text.align_right)
    dash.cell(0, 2, "Liquidity below", text_color = #D1D4DC, text_size = size.small, text_halign = text.align_left)
    dash.cell(1, 2, str.tostring(100 - upPct, "0") + "%", text_color = #00E676, text_size = size.small, text_halign = text.align_right)
    dash.cell(0, 3, "Draw on liquidity", text_color = #D1D4DC, text_size = size.small, text_halign = text.align_left)
    dash.cell(1, 3, (upPct >= 55 ? "▲ " : upPct <= 45 ? "▼ " : "◆ ") + meter, text_color = upPct >= 55 ? #00E676 : upPct <= 45 ? #FF1744 : #787B86, text_size = size.small, text_halign = text.align_right)
    dash.cell(0, 4, "Main magnet", text_color = #D1D4DC, text_size = size.small, text_halign = text.align_left)
    dash.cell(1, 4, na(mag1) ? "—" : str.tostring(mag1, format.mintick), text_color = #F59E0B, text_size = size.small, text_halign = text.align_right)
 
// ───────────────────────────── Alerts ─────────────────────────────
alertcondition(sweptUp and hotSweep, "Hot buy-side liquidity swept", "TB HEAT: hot buy-side liquidity swept on {{ticker}} ({{interval}})")
alertcondition(sweptDn and hotSweep, "Hot sell-side liquidity swept", "TB HEAT: hot sell-side liquidity swept on {{ticker}} ({{interval}})")

← 返回列表