M-Alpha-Leverage Guard - How Often Leverage Gets Wiped Out

MAlphaCore · study · 77 行 · 点赞 10 · 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/
// © MAlphaCore
 
//@version=6
// M·Alpha·Leverage Guard — draws, from the current price, how far the market has to move against a long or a short until
// the margin is gone at 5x, 10x, 20x and 50x leverage (simplified: 1 / leverage, no maintenance margin and no fees, so real
// liquidations come a bit earlier). Next to every line: how often THIS market actually moved that far against a position
// within your holding time, counted over the whole chart history. Past windows only, closed bars only, nothing repaints.
indicator("M·Alpha·Leverage Guard", shorttitle = "M·Alpha·Leverage", overlay = true, max_lines_count = 20, max_labels_count = 20)
 
// ───────────────────────────── Inputs ─────────────────────────────
grLev    = "Leverage"
lev1     = input.float(5, "Leverage 1", minval = 1, maxval = 200, group = grLev, display = display.none)
lev2     = input.float(10, "Leverage 2", minval = 1, maxval = 200, group = grLev, display = display.none)
lev3     = input.float(20, "Leverage 3", minval = 1, maxval = 200, group = grLev, display = display.none)
lev4     = input.float(50, "Leverage 4", minval = 1, maxval = 200, group = grLev, display = display.none)
hours    = input.float(24, "Holding time (hours)", minval = 0.25, maxval = 24 * 60, step = 1, group = grLev, display = display.none)
 
grShow   = "Display"
showLong  = input.bool(true, "Show long side (below price)", group = grShow, display = display.none)
showShort = input.bool(true, "Show short side (above price)", group = grShow, display = display.none)
lineBack  = input.int(30, "Line length to the left (bars)", minval = 1, maxval = 200, group = grShow, display = display.none)
 
grStyle  = "Style"
colLong  = input.color(#ffeb3b, "Long", group = grStyle, display = display.none)
colShort = input.color(#b2b5be, "Short", group = grStyle, display = display.none)
colText  = input.color(#e8eaf0, "Text", group = grStyle, display = display.none)
textSizeIn = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = grStyle, display = display.none)
 
// ───────────────────────────── Statistics ─────────────────────────────
// for the entry W bars ago: did the low (long) or high (short) of the following W bars reach 1 / leverage against it?
W = math.max(1, int(math.round(hours * 3600 / timeframe.in_seconds())))
lowNext  = ta.lowest(low, W)
highNext = ta.highest(high, W)
entryW   = close[W]
 
var int   nWin = 0
var array<int> hitL = array.from(0, 0, 0, 0)
var array<int> hitS = array.from(0, 0, 0, 0)
levs = array.from(lev1, lev2, lev3, lev4)
 
if barstate.isconfirmed and not na(entryW) and not na(lowNext)
    nWin += 1
    for i = 0 to 3
        d = 1.0 / levs.get(i)
        if lowNext <= entryW * (1 - d)
            hitL.set(i, hitL.get(i) + 1)
        if highNext >= entryW * (1 + d)
            hitS.set(i, hitS.get(i) + 1)
 
// ───────────────────────────── Drawing ─────────────────────────────
var array<line>  lns = array.new<line>()
var array<label> lbs = array.new<label>()
textSize = textSizeIn == "Tiny" ? size.tiny : textSizeIn == "Normal" ? size.normal : size.small
span = hours >= 48 ? str.tostring(hours / 24, "#.#") + " days" : str.tostring(hours, "#.##") + " h"
 
pct(int c) => nWin > 0 ? str.tostring(100.0 * c / nWin, "#.#") + " %" : "–"
 
level(float price, color col, string txt) =>
    lns.push(line.new(bar_index - lineBack, price, bar_index + 3, price, color = col, width = 1, style = line.style_dashed))
    lbs.push(label.new(bar_index + 3, price, txt, style = label.style_label_left, color = color.new(col, 85), textcolor = colText, size = textSize))
 
if barstate.islast
    for l in lns
        l.delete()
    for lb in lbs
        lb.delete()
    lns.clear()
    lbs.clear()
    for i = 0 to 3
        lv = levs.get(i)
        d  = 1.0 / lv
        lvTxt = str.tostring(lv, "#.#") + "x"
        if showLong
            level(close * (1 - d), colLong, lvTxt + " long · margin gone in " + span + ": " + pct(hitL.get(i)) + " of the time")
        if showShort
            level(close * (1 + d), colShort, lvTxt + " short · margin gone in " + span + ": " + pct(hitS.get(i)) + " of the time")

← 返回列表