本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ⓒ RUpward
// BingX Liquidation & PnL Target Level Indicator (single-leverage / interactive entry + DCA simulation)
//@version=6
indicator("BingXLiqLevel V2", "BingXLiq", overlay=true, max_lines_count=50, max_labels_count=50)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Entry Settings (interactive line, no label)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var G_ENTRY = "Entry Settings"
i_entryPrice = input.price(0, "Entry Price (Click Chart)", confirm=true, group=G_ENTRY,
tooltip="Click the chart to set the entry price. You can drag the line afterwards to adjust it.")
i_isLong = input.bool(true, "Long (Uncheck = Short)", group=G_ENTRY,
tooltip="Checked : Long Position | Unchecked : Short Position")
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Leverage (single internal input field)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var G_LEV = "Leverage"
i_lev = input.float(50.0, "Leverage (x)", minval=1.0, maxval=200.0, step=1.0, group=G_LEV)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BingX Fee Settings
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var G_FEE = "BingX Fee Settings"
i_mmr = input.float(0.4, "Maintenance Margin Rate (%)", minval=0.01, maxval=5.0, step=0.01, group=G_FEE)
i_feeRate = input.float(0.05, "Taker Fee Rate (%)", minval=0.0, maxval=1.0, step=0.01, group=G_FEE)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// TP (Take Profit) Settings ── on/off
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var G_TP = "TP Settings"
i_showTP = input.bool(true, "Show TP Lines", group=G_TP, tooltip="Toggle all PnL +50% / +100% target lines")
i_show50 = input.bool(true, "PnL +50%", group=G_TP, inline="tp")
i_show100 = input.bool(true, "PnL +100%", group=G_TP, inline="tp")
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DCA Simulation
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var G_DCA = "DCA Simulation"
i_showDCA = input.bool(true, "Show DCA Simulation", group=G_DCA)
i_addPrice = input.price(0, "Add Price (Click Chart)", confirm=true, group=G_DCA,
tooltip="Click the chart to set the DCA (add) price. Drag the line to move it and the liquidation price is recalculated instantly.")
i_sizeUnit = input.string("Margin (USDT)", "Size Unit", options=["Margin (USDT)", "Qty (Coin)"], group=G_DCA,
tooltip="Margin : based on margin in USDT | Qty : based on coin quantity")
i_curSize = input.float(100.0, "Current Position Size", minval=0.0, step=1.0, group=G_DCA,
tooltip="Size of the position you currently hold, in the unit selected above. Also used for the PnL label and table.")
i_addSize = input.float(100.0, "Add Size", minval=0.0, step=1.0, group=G_DCA,
tooltip="Size to add at the DCA price, in the unit selected above")
i_showDCAInfo = input.bool(true, "Show DCA Info Label", group=G_DCA)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DCA Info Label (auto-fit label + leader line)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var G_INFO = "DCA Info Label"
i_lblBg = input.color(#FFEB3B, "Background", group=G_INFO, inline="c1")
i_lblTxt = input.color(#000000, "Text", group=G_INFO, inline="c1")
i_leaderCol = input.color(#FFEB3B, "Leader", group=G_INFO, inline="c2")
i_leaderW = input.int(2, "W", minval=1, maxval=5, group=G_INFO, inline="c2")
i_lblTxtSize = input.string("Small", "Text Size", options=["Tiny", "Small", "Normal", "Large"], group=G_INFO)
i_lblGapBars = input.int(8, "Horizontal Gap (Bars)", minval=0, maxval=200, group=G_INFO,
tooltip="Horizontal distance from the line end to the label")
i_lblGapPct = input.float(1.0, "Vertical Gap (%)", minval=0.0, step=0.1, group=G_INFO,
tooltip="Vertical distance from the new liquidation price to the label, as % of price. Long = below, Short = above")
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PnL Label (entry line end box)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var G_PNL = "PnL Label"
i_pnlOff = input.bool(false, "Hide PnL Label", group=G_PNL,
tooltip="Box label at the end of the entry line showing ROE and PnL in USDT. Calculated from entry price, current price, Current Position Size and leverage. ROE is return on margin.")
i_pnlProfit = input.color(#26A69A, "Profit", group=G_PNL, inline="p1")
i_pnlLoss = input.color(#EF5350, "Loss", group=G_PNL, inline="p1")
i_pnlTxtCol = input.color(#FFFFFF, "Text", group=G_PNL, inline="p1")
i_pnlTxtSize = input.string("Small", "Text Size", options=["Tiny", "Small", "Normal", "Large"], group=G_PNL)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Info Table (same info as labels)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var G_TBL = "Info Table"
i_tblOff = input.bool(false, "Hide Table", group=G_TBL,
tooltip="Shows the PnL label and DCA info label contents in a table.")
i_tblPos = input.string("Top Right", "Position",
options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], group=G_TBL)
i_tblTxtSize = input.string("Small", "Text Size", options=["Tiny", "Small", "Normal", "Large"], group=G_TBL)
i_tblBg = input.color(#1E222D, "Background", group=G_TBL, inline="tb1")
i_tblTxt = input.color(#D1D4DC, "Text", group=G_TBL, inline="tb1")
i_tblHdrBg = input.color(#2A2E39, "Header", group=G_TBL, inline="tb2")
i_tblHdrTxt = input.color(#FFFFFF, "Header Text", group=G_TBL, inline="tb2")
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Line Visual Settings (color / width / style)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var G_VIS = "Line Visual Settings"
// Entry
i_entryColor = input.color(#FF9800, "Entry", group=G_VIS, inline="e")
i_entryWidth = input.int(1, "W", minval=1, maxval=5, group=G_VIS, inline="e")
i_entryStyle = input.string("Dotted", "", options=["Solid", "Dashed", "Dotted"], group=G_VIS, inline="e")
// Liquidation
i_liqColor = input.color(#FF0000, "Liq", group=G_VIS, inline="l")
i_liqWidth = input.int(2, "W", minval=1, maxval=5, group=G_VIS, inline="l")
i_liqStyle = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], group=G_VIS, inline="l")
// TP
i_tpColor = input.color(#26A69A, "TP", group=G_VIS, inline="t")
i_tpWidth = input.int(1, "W", minval=1, maxval=5, group=G_VIS, inline="t")
i_tpStyle = input.string("Dashed", "", options=["Solid", "Dashed", "Dotted"], group=G_VIS, inline="t")
// DCA Add Point
i_addColor = input.color(#2196F3, "Add", group=G_VIS, inline="a")
i_addWidth = input.int(1, "W", minval=1, maxval=5, group=G_VIS, inline="a")
i_addStyle = input.string("Dotted", "", options=["Solid", "Dashed", "Dotted"], group=G_VIS, inline="a")
// New Average Entry
i_avgColor = input.color(#FFEB3B, "New Avg", group=G_VIS, inline="v")
i_avgWidth = input.int(1, "W", minval=1, maxval=5, group=G_VIS, inline="v")
i_avgStyle = input.string("Dashed", "", options=["Solid", "Dashed", "Dotted"], group=G_VIS, inline="v")
// New Liquidation
i_nLiqColor = input.color(#E040FB, "New Liq", group=G_VIS, inline="n")
i_nLiqWidth = input.int(2, "W", minval=1, maxval=5, group=G_VIS, inline="n")
i_nLiqStyle = input.string("Dashed", "", options=["Solid", "Dashed", "Dotted"], group=G_VIS, inline="n")
var G_LINE = "Line Length"
i_extL = input.int(30, "Extend Left (Bars)", minval=0, maxval=200, group=G_LINE)
i_extR = input.int(30, "Extend Right (Bars)", minval=0, maxval=200, group=G_LINE)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Helpers
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_lineStyle(string s) =>
s == "Solid" ? line.style_solid : s == "Dotted" ? line.style_dotted : line.style_dashed
f_txtSize(string s) =>
s == "Tiny" ? size.tiny : s == "Normal" ? size.normal : s == "Large" ? size.large : size.small
f_tblPos(string s) =>
switch s
"Top Left" => position.top_left
"Top Center" => position.top_center
"Middle Left" => position.middle_left
"Middle Right" => position.middle_right
"Bottom Left" => position.bottom_left
"Bottom Center" => position.bottom_center
"Bottom Right" => position.bottom_right
=> position.top_right
// BingX liquidation price
f_liq(float entry, float lev, float mmr, float fee, bool isLong) =>
float liqDist = 1.0 / lev - mmr - fee
isLong ? entry * (1.0 - liqDist) : entry * (1.0 + liqDist)
// PnL target price (pct = 0.5 → +50%, 1.0 → +100%)
f_pnlTarget(float entry, float lev, float pct, bool isLong) =>
float dist = pct / lev
isLong ? entry * (1.0 + dist) : entry * (1.0 - dist)
// input unit → coin quantity
f_toQty(float size, float price, float lev, bool isMargin) =>
isMargin ? size * lev / price : size
// Info label + leader line (Long: lower right / Short: upper right)
// Label auto-sizes to its text, so the text always fits regardless of zoom
f_infoLabel(int xAnchor, float yAnchor, bool isLong, string txt, color bg, color tc) =>
int lx = xAnchor + i_lblGapBars
float ly = isLong ? yAnchor * (1.0 - i_lblGapPct / 100.0) : yAnchor * (1.0 + i_lblGapPct / 100.0)
line ld = line.new(xAnchor, yAnchor, lx, ly, color=i_leaderCol, style=line.style_solid, width=i_leaderW)
label lb = label.new(lx, ly, txt,
style=isLong ? label.style_label_upper_left : label.style_label_lower_left,
color=bg, textcolor=tc, size=f_txtSize(i_lblTxtSize), textalign=text.align_left)
[ld, lb]
// Table header row (2 cells merged)
f_tblHeader(table tb, int row, string txt) =>
table.cell(tb, 0, row, txt, text_color=i_tblHdrTxt, bgcolor=i_tblHdrBg,
text_size=f_txtSize(i_tblTxtSize), text_halign=text.align_left)
table.merge_cells(tb, 0, row, 1, row)
// Table key/value row
f_tblRow(table tb, int row, string k, string v, color vCol) =>
table.cell(tb, 0, row, k, text_color=i_tblTxt, text_size=f_txtSize(i_tblTxtSize), text_halign=text.align_left)
table.cell(tb, 1, row, v, text_color=vCol, text_size=f_txtSize(i_tblTxtSize), text_halign=text.align_right)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Drawing Storage (single handles)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var line lnEntry = na
var line lnLiq = na
var line lnTP50 = na
var line lnTP100 = na
var line lnAdd = na
var line lnNewAvg = na
var line lnNewLiq = na
var line lnLeader = na
var label lbDCA = na
var label lbPnL = na
var table tbInfo = na
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Main Logic
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if barstate.islast and i_entryPrice > 0
// ── Clear previous ──
line.delete(lnEntry)
line.delete(lnLiq)
line.delete(lnTP50)
line.delete(lnTP100)
line.delete(lnAdd)
line.delete(lnNewAvg)
line.delete(lnNewLiq)
line.delete(lnLeader)
label.delete(lbDCA)
label.delete(lbPnL)
table.delete(tbInfo)
lnEntry := na
lnLiq := na
lnTP50 := na
lnTP100 := na
lnAdd := na
lnNewAvg := na
lnNewLiq := na
lnLeader := na
lbDCA := na
lbPnL := na
tbInfo := na
bool isLong = i_isLong
int x1 = bar_index - i_extL
int x2 = bar_index + i_extR
float mmr = i_mmr / 100.0
float fee = i_feeRate / 100.0
// ── Entry Line ──
lnEntry := line.new(x1, i_entryPrice, x2, i_entryPrice,
color=i_entryColor, style=f_lineStyle(i_entryStyle), width=i_entryWidth)
// ── Liquidation Line ──
float liqP = f_liq(i_entryPrice, i_lev, mmr, fee, isLong)
lnLiq := line.new(x1, liqP, x2, liqP,
color=i_liqColor, style=f_lineStyle(i_liqStyle), width=i_liqWidth)
// ── TP Lines (toggle) ──
if i_showTP
if i_show50
float p50 = f_pnlTarget(i_entryPrice, i_lev, 0.5, isLong)
lnTP50 := line.new(x1, p50, x2, p50,
color=i_tpColor, style=f_lineStyle(i_tpStyle), width=i_tpWidth)
if i_show100
float p100 = f_pnlTarget(i_entryPrice, i_lev, 1.0, isLong)
lnTP100 := line.new(x1, p100, x2, p100,
color=i_tpColor, style=f_lineStyle(i_tpStyle), width=i_tpWidth)
// ── Current Position PnL (entry · current price · size · leverage) ──
bool isMargin = i_sizeUnit == "Margin (USDT)"
float curQty = f_toQty(i_curSize, i_entryPrice, i_lev, isMargin)
float curMargin = curQty * i_entryPrice / i_lev
float pnlUsdt = (close - i_entryPrice) * curQty * (isLong ? 1.0 : -1.0)
float roePct = curMargin > 0 ? pnlUsdt / curMargin * 100.0 : 0.0
bool hasPos = curQty > 0
string roeTxt = hasPos ? str.tostring(roePct, "+#,##0.00;-#,##0.00") + "%" : "-"
string pnlTxt = hasPos ? str.tostring(pnlUsdt, "+#,##0.00;-#,##0.00") + " USDT" : "-"
color pnlCol = pnlUsdt >= 0 ? i_pnlProfit : i_pnlLoss
if not i_pnlOff and hasPos
lbPnL := label.new(x2, i_entryPrice, roeTxt + " " + pnlTxt,
style=label.style_label_left, color=pnlCol, textcolor=i_pnlTxtCol,
size=f_txtSize(i_pnlTxtSize), textalign=text.align_center)
// ── DCA Simulation ──
array<string> dcaKeys = array.new<string>()
array<string> dcaVals = array.new<string>()
string dcaWarn = ""
if i_showDCA and i_addPrice > 0
lnAdd := line.new(x1, i_addPrice, x2, i_addPrice,
color=i_addColor, style=f_lineStyle(i_addStyle), width=i_addWidth)
float addQty = f_toQty(i_addSize, i_addPrice, i_lev, isMargin)
float totQty = curQty + addQty
// Add price already beyond current liquidation → liquidated before reaching it
bool passedLiq = isLong ? i_addPrice <= liqP : i_addPrice >= liqP
if passedLiq
dcaWarn := "⚠ Add price is beyond the current liquidation\n(liquidated before reaching it)"
if i_showDCAInfo
[ld, lb] = f_infoLabel(x2, i_addPrice, isLong, dcaWarn, #FF1744, color.white)
lnLeader := ld
lbDCA := lb
else if totQty > 0
float newAvg = (curQty * i_entryPrice + addQty * i_addPrice) / totQty
float newLiq = f_liq(newAvg, i_lev, mmr, fee, isLong)
float totMargin = (curQty * i_entryPrice + addQty * i_addPrice) / i_lev
lnNewAvg := line.new(x1, newAvg, x2, newAvg,
color=i_avgColor, style=f_lineStyle(i_avgStyle), width=i_avgWidth)
lnNewLiq := line.new(x1, newLiq, x2, newLiq,
color=i_nLiqColor, style=f_lineStyle(i_nLiqStyle), width=i_nLiqWidth)
float liqShift = newLiq - liqP
float liqShiftPct = liqShift / liqP * 100.0
float bufPct = math.abs(i_addPrice - newLiq) / i_addPrice * 100.0
// Built in one place so the label and table show the same values
array.push(dcaKeys, "Avg Entry")
array.push(dcaVals, str.tostring(newAvg, format.mintick))
array.push(dcaKeys, "Liq Price")
array.push(dcaVals, str.tostring(newLiq, format.mintick))
array.push(dcaKeys, "Liq Shift")
array.push(dcaVals, str.tostring(liqShift, format.mintick) + " (" + str.tostring(liqShiftPct, "+#.##;-#.##") + "%)")
array.push(dcaKeys, "Add → Liq")
array.push(dcaVals, str.tostring(bufPct, "#.##") + "%")
array.push(dcaKeys, "Total Qty")
array.push(dcaVals, str.tostring(totQty, "#.####"))
array.push(dcaKeys, "Total Margin")
array.push(dcaVals, str.tostring(totMargin, "#.##") + " USDT")
if i_showDCAInfo
string txt = "After DCA"
for i = 0 to array.size(dcaKeys) - 1
txt += "\n" + array.get(dcaKeys, i) + ": " + array.get(dcaVals, i)
[ld, lb] = f_infoLabel(x2, newLiq, isLong, txt, i_lblBg, i_lblTxt)
lnLeader := ld
lbDCA := lb
// ── Info Table (same info as labels) ──
if not i_tblOff
int nDca = array.size(dcaKeys)
int nRows = 3 + (nDca > 0 ? nDca + 1 : 0) + (dcaWarn != "" ? 2 : 0)
tbInfo := table.new(f_tblPos(i_tblPos), 2, nRows, bgcolor=i_tblBg,
frame_color=i_tblHdrBg, frame_width=1, border_color=i_tblHdrBg, border_width=1)
int r = 0
f_tblHeader(tbInfo, r, "Position " + (isLong ? "Long" : "Short") + " " + str.tostring(i_lev, "#.#") + "x")
r += 1
f_tblRow(tbInfo, r, "ROE", roeTxt, hasPos ? pnlCol : i_tblTxt)
r += 1
f_tblRow(tbInfo, r, "PnL", pnlTxt, hasPos ? pnlCol : i_tblTxt)
r += 1
if nDca > 0
f_tblHeader(tbInfo, r, "After DCA")
r += 1
for i = 0 to nDca - 1
f_tblRow(tbInfo, r, array.get(dcaKeys, i), array.get(dcaVals, i), i_tblTxt)
r += 1
if dcaWarn != ""
f_tblHeader(tbInfo, r, "After DCA")
r += 1
table.cell(tbInfo, 0, r, dcaWarn, text_color=color.white, bgcolor=#FF1744,
text_size=f_txtSize(i_tblTxtSize), text_halign=text.align_left)
table.merge_cells(tbInfo, 0, r, 1, r)