Liquidation Levels Lite [HansaTrades]

HansaTrades · study · 118 行 · 点赞 4 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HansaTrades
//@version=6
indicator("HansaTrades · Liquidation Levels Lite", shorttitle = "HT Liq Lite", overlay = true, max_lines_count = 500, max_labels_count = 10)
 
// Estimated liquidation levels of leveraged positions.
// Idea: many leveraged positions are opened on bars with unusually high volume.
// For each such bar the script projects where 25x / 50x / 100x longs (below) and
// shorts (above) would be liquidated. A level stays on the chart until price
// trades through it ("swept"). Overlapping levels form clusters that often act
// like magnets. Estimates only: no exchange order or position data is used.
 
// ───────────── Inputs ─────────────
grpL   = "Leverage"
use25  = input.bool(true, "25x levels", group = grpL)
use50  = input.bool(true, "50x levels", group = grpL)
use100 = input.bool(true, "100x levels", group = grpL)
mmrPct = input.float(0.5, "Maintenance margin (%)", minval = 0, maxval = 5, step = 0.1, group = grpL, tooltip = "Typical perp maintenance margin. Moves each level slightly toward the entry price.")
 
grpE    = "Entries"
volLen  = input.int(50, "Volume average (bars)", minval = 5, group = grpE)
volMult = input.float(2.0, "Only bars with volume > x average", minval = 1, step = 0.1, group = grpE)
maxLv   = input.int(150, "Max. active levels", minval = 10, maxval = 400, group = grpE)
 
grpD     = "Display"
showMag  = input.bool(true, "Label strongest cluster above / below", group = grpD)
clustPct = 0.3
scheme   = input.string("Black & White", "Color scheme", options = ["Black & White", "Classic (dark)", "Light", "Colorblind-safe", "Amber (dark)", "Custom colors"], group = grpD, tooltip = "Light = for white chart backgrounds. Custom colors uses the three color fields below.")
cLongIn  = input.color(#FFFFFF, "Custom: long liquidations (below price)", group = grpD)
cShortIn = input.color(#9E9E9E, "Custom: short liquidations (above price)", group = grpD)
cMagIn   = input.color(#FFB000, "Custom: cluster label", group = grpD)
// Scheme colors: long liquidations, short liquidations, cluster label, label text
color colLong  = switch scheme
    "Classic (dark)"  => #FF5252
    "Light"           => #263238
    "Colorblind-safe" => #E69F00
    "Amber (dark)"    => #FFB000
    "Custom colors"   => cLongIn
    => #FFFFFF
color colShort = switch scheme
    "Classic (dark)"  => #26A69A
    "Light"           => #78909C
    "Colorblind-safe" => #56B4E9
    "Amber (dark)"    => #4C9AFF
    "Custom colors"   => cShortIn
    => #9E9E9E
color colMag   = switch scheme
    "Classic (dark)"  => #FFEB3B
    "Light"           => #263238
    "Colorblind-safe" => #F0E442
    "Custom colors"   => cMagIn
    => #FFB000
color colMagTx = scheme == "Light" ? color.white : color.black
 
mmr = mmrPct / 100
 
// ───────────── Levels ─────────────
type Lvl
    line  ln
    float px
    float w
    bool  isLong
 
var array<Lvl> lv = array.new<Lvl>()
 
newLevel(float px, float w, bool isLong) =>
    tr = math.max(55, 88 - w * 6)
    c  = isLong ? colLong : colShort
    l  = line.new(bar_index, px, bar_index + 1, px, extend = extend.right, color = color.new(c, tr), width = w > 4 ? 2 : 1)
    lv.push(Lvl.new(l, px, w, isLong))
 
// Remove levels that price traded through on this bar
if lv.size() > 0
    for i = lv.size() - 1 to 0
        l = lv.get(i)
        if l.isLong ? low <= l.px : high >= l.px
            l.ln.delete()
            lv.remove(i)
 
// New levels from high-volume bars
volMa = ta.sma(volume, volLen)
ratio = volMa > 0 ? nz(volume) / volMa : 0.0
if ratio >= volMult
    for lev in array.from(25.0, 50.0, 100.0)
        on = lev == 25 ? use25 : lev == 50 ? use50 : use100
        if on
            newLevel(close * (1 - 1 / lev + mmr), ratio, true)
            newLevel(close * (1 + 1 / lev - mmr), ratio, false)
 
while lv.size() > maxLv
    lv.shift().ln.delete()
 
// ───────────── Strongest cluster per side ─────────────
var label lbUp = na
var label lbDn = na
if barstate.islast
    label.delete(lbUp)
    label.delete(lbDn)
    if showMag and lv.size() > 0
        float upW  = 0.0
        float upPx = na
        float dnW  = 0.0
        float dnPx = na
        for a in lv
            float cw = 0.0
            for b in lv
                if math.abs(b.px - a.px) <= a.px * clustPct / 100
                    cw += b.w
            if a.px > close and cw > upW
                upW := cw
                upPx := a.px
            if a.px < close and cw > dnW
                dnW := cw
                dnPx := a.px
        if not na(upPx)
            lbUp := label.new(bar_index + 3, upPx, "Short liqs " + str.tostring(upPx, format.mintick) + "  +" + str.tostring((upPx / close - 1) * 100, "#.##") + "%", style = label.style_label_left, color = color.new(colMag, 25), textcolor = colMagTx, size = size.small, text_font_family = font.family_monospace)
        if not na(dnPx)
            lbDn := label.new(bar_index + 3, dnPx, "Long liqs " + str.tostring(dnPx, format.mintick) + "  " + str.tostring((dnPx / close - 1) * 100, "#.##") + "%", style = label.style_label_left, color = color.new(colMag, 25), textcolor = colMagTx, size = size.small, text_font_family = font.family_monospace)

← 返回列表