Whales vs Leverage (Bitfinex Flows + Open Interest)

BullBearSR · study · 54 行 · 点赞 3 · TradingView 原页

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

Pine Script

//@version=6
indicator("Whales vs Leverage (Bitfinex Flows + Open Interest)", shorttitle = "Whales vs Leverage", overlay = false, format = format.volume)
 
// ─── Inputs ─────────────────────────────────────────────────────────────────
mode    = input.string("Auto", "Coin", options = ["Auto", "Manual"], tooltip = "Auto: uses the base coin of the chart (BTC on BTCUSDT, ETH on ETHUSDT...).")
coinMan = input.string("BTC", "Coin (manual)")
len     = input.int(30, "Change measured over (bars)", minval = 1)
showTbl = input.bool(true, "Show current positions table")
colL    = input.color(color.green, "Longs", group = "Style")
colS    = input.color(color.red, "Shorts", group = "Style")
colNetU = input.color(color.new(color.teal, 40), "Net position rising", group = "Style")
colNetD = input.color(color.new(color.maroon, 40), "Net position falling", group = "Style")
showOI  = input.bool(true, "Open interest of the chart symbol", group = "Open interest", tooltip = "Change in open interest, in coins, of the futures / perpetual loaded on the chart (e.g. BINANCE:BTCUSDT.P). The line color tells who is behind the change.")
cNL     = input.color(color.blue, "Price up + OI up: new longs", group = "Open interest")
cSC     = input.color(color.aqua, "Price up + OI down: shorts closing", group = "Open interest")
cNS     = input.color(color.fuchsia, "Price down + OI up: new shorts", group = "Open interest")
cLC     = input.color(color.orange, "Price down + OI down: longs closing", group = "Open interest")
 
string coin = mode == "Auto" ? syminfo.basecurrency : coinMan
 
// Bitfinex margin positions, in coins
float L = request.security("BITFINEX:" + coin + "USDLONGS", timeframe.period, close, ignore_invalid_symbol = true)
float S = request.security("BITFINEX:" + coin + "USDSHORTS", timeframe.period, close, ignore_invalid_symbol = true)
 
// Change over the window, in coins: everything on the same scale
float dL   = L - L[len]
float dS   = S - S[len]
float dNet = dL - dS     // net position change (longs minus shorts)
 
hline(0, "Zero", color = color.gray, linestyle = hline.style_dashed)
plot(dNet, "Net position change", color = dNet >= 0 ? colNetU : colNetD, style = plot.style_columns)
plot(dL, "Longs change", color = colL, linewidth = 2)
plot(dS, "Shorts change", color = colS, linewidth = 2)
 
// Open interest of the chart symbol, change in coins (same scale as the Bitfinex positions)
float oi     = request.security(syminfo.tickerid + "_OI", timeframe.period, close, ignore_invalid_symbol = true)
float dOI    = oi - oi[len]
float dPrice = close - close[len]
color cOI    = dPrice >= 0 ? (dOI >= 0 ? cNL : cSC) : (dOI >= 0 ? cNS : cLC)
plot(showOI ? dOI : na, "Open interest change", color = cOI, linewidth = 2)
 
// Current positions
var table tbl = table.new(position.top_right, 2, 5, bgcolor = color.new(color.gray, 85), border_color = color.gray, border_width = 1)
if showTbl and barstate.islast
    tbl.cell(0, 0, "Bitfinex " + coin, text_size = size.small)
    tbl.cell(1, 0, na(L) ? "no data" : "last " + str.tostring(len) + " bars", text_size = size.small)
    tbl.cell(0, 1, "Longs", text_size = size.small, text_color = colL)
    tbl.cell(1, 1, str.tostring(L, format.volume) + "  (" + (dL >= 0 ? "+" : "") + str.tostring(dL, format.volume) + ")", text_size = size.small)
    tbl.cell(0, 2, "Shorts", text_size = size.small, text_color = colS)
    tbl.cell(1, 2, str.tostring(S, format.volume) + "  (" + (dS >= 0 ? "+" : "") + str.tostring(dS, format.volume) + ")", text_size = size.small)
    tbl.cell(0, 3, "Net", text_size = size.small)
    tbl.cell(1, 3, str.tostring(L - S, format.volume) + "  (" + (dNet >= 0 ? "+" : "") + str.tostring(dNet, format.volume) + ")", text_size = size.small)
    tbl.cell(0, 4, "Longs share", text_size = size.small)
    tbl.cell(1, 4, na(L) or na(S) ? "-" : str.tostring(100 * L / (L + S), "0.0") + " %", text_size = size.small)

← 返回列表