BTC Aggregated Funding Rate

F_u_s_i_o_N · study · 82 行 · 点赞 8 · TradingView 原页

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

Pine Script

//@version=6
indicator("BTC Aggregated Funding Rate", shorttitle = "BTC FR", overlay = false, scale = scale.right, precision = 4)
import TradingView/Request/3 as req
 
// Funding across the same four exchanges LiqVP uses, converted to a common 8-hour basis
// (Hyperliquid pays hourly) and OI-weighted.
 
COINS = "Coins (BTC)"
USDU  = "USD"
 
grpX = "Exchanges  (on / perp / OI unit / funding hours)"
x1on = input.bool(true, "X1", inline = "x1", group = grpX), x1sym = input.symbol("BINANCE:BTCUSDT.P",     "", inline = "x1", group = grpX), x1u = input.string(COINS, "", options = [COINS, USDU], inline = "x1", group = grpX), x1h = input.int(8, "h", minval = 1, maxval = 24, inline = "x1", group = grpX)
x2on = input.bool(true, "X2", inline = "x2", group = grpX), x2sym = input.symbol("BYBIT:BTCUSDT.P",       "", inline = "x2", group = grpX), x2u = input.string(COINS, "", options = [COINS, USDU], inline = "x2", group = grpX), x2h = input.int(8, "h", minval = 1, maxval = 24, inline = "x2", group = grpX)
x3on = input.bool(true, "X3", inline = "x3", group = grpX), x3sym = input.symbol("OKX:BTCUSDT.P",         "", inline = "x3", group = grpX), x3u = input.string(USDU,  "", options = [COINS, USDU], inline = "x3", group = grpX), x3h = input.int(8, "h", minval = 1, maxval = 24, inline = "x3", group = grpX)
x4on = input.bool(true, "X4", inline = "x4", group = grpX), x4sym = input.symbol("HYPERLIQUID:BTCUSDC.P", "", inline = "x4", group = grpX), x4u = input.string(USDU,  "", options = [COINS, USDU], inline = "x4", group = grpX), x4h = input.int(1, "h", minval = 1, maxval = 24, inline = "x4", group = grpX)
fUnits = input.string("Auto", "Funding units in source data", options = ["Auto", "Fraction", "Percent"], group = grpX)
 
grpV     = "Display"
showAs   = input.string("Per 8h %", "Show funding as", options = ["Per 8h %", "Annualized %"], group = grpV)
weightM  = input.string("OI-weighted", "Average across exchanges", options = ["OI-weighted", "Simple"], group = grpV)
smooth   = input.int(1, "Smoothing (EMA bars, 1 = off)", minval = 1, group = grpV)
shade    = input.bool(true, "Shade to zero", group = grpV)
showBase = input.bool(true, "Show neutral baseline (0.01% per 8h)", group = grpV)
posCol   = input.color(color.new(color.lime, 0), "Positive (longs pay)", group = grpV)
negCol   = input.color(color.new(color.red, 0), "Negative (shorts pay)", group = grpV)
showTbl  = input.bool(true, "Show funding summary", group = grpV)
 
f_fund(bool on, string sym) =>
    float f = na
    if on
        f := req.cryptoDerivativeMetric(sym, "Funding Rate", timeframe.period, false, true)
    f
 
f_oi(bool on, string sym, string unit) =>
    float oi = request.security(sym + "_OI", timeframe.period, close, ignore_invalid_symbol = true)
    on ? (unit == USDU ? oi / close : oi) : na
 
f_w(float f, float oi) =>
    bool ok = not na(f) and not na(oi) and oi > 0
    [ok ? f * oi : 0.0, ok ? oi : 0.0]
 
// raw funding per exchange, unit-detected, converted to % per 8 hours
r1 = f_fund(x1on, x1sym)
r2 = f_fund(x2on, x2sym)
r3 = f_fund(x3on, x3sym)
r4 = f_fund(x4on, x4sym)
var float maxAbsRaw = 0.0
maxAbsRaw := math.max(maxAbsRaw, math.abs(nz(r1)), math.abs(nz(r2)), math.abs(nz(r3)), math.abs(nz(r4)))
bool  isFraction = fUnits == "Fraction" or (fUnits == "Auto" and maxAbsRaw > 0 and maxAbsRaw < 0.005)
float fMult = isFraction ? 100.0 : 1.0
f1 = r1 * fMult * 8.0 / x1h
f2 = r2 * fMult * 8.0 / x2h
f3 = r3 * fMult * 8.0 / x3h
f4 = r4 * fMult * 8.0 / x4h
 
// OI for weighting (BTC)
o1 = f_oi(x1on, x1sym, x1u)
o2 = f_oi(x2on, x2sym, x2u)
o3 = f_oi(x3on, x3sym, x3u)
o4 = f_oi(x4on, x4sym, x4u)
[a1, w1] = f_w(f1, o1)
[a2, w2] = f_w(f2, o2)
[a3, w3] = f_w(f3, o3)
[a4, w4] = f_w(f4, o4)
wSum    = w1 + w2 + w3 + w4
cntF    = (na(f1) ? 0 : 1) + (na(f2) ? 0 : 1) + (na(f3) ? 0 : 1) + (na(f4) ? 0 : 1)
simpleF = cntF > 0 ? (nz(f1) + nz(f2) + nz(f3) + nz(f4)) / cntF : na
fund8h  = weightM == "Simple" ? simpleF : (wSum > 0 ? (a1 + a2 + a3 + a4) / wSum : simpleF)
 
annual  = showAs == "Annualized %"
fDisp   = annual ? fund8h * 3 * 365 : fund8h
fS      = ta.ema(fDisp, smooth)
 
pF = plot(fS, "Funding", color = fS >= 0 ? posCol : negCol, linewidth = 2)
pZ = plot(0, "Zero", color = color.new(color.gray, 50), display = display.pane)
fill(pF, pZ, color = shade ? (fS >= 0 ? color.new(posCol, 85) : color.new(negCol, 85)) : na, title = "Shade")
plot(showBase ? (annual ? 10.95 : 0.01) : na, "Neutral baseline", color = color.new(color.gray, 30), linewidth = 1, display = display.pane)
 
var table tbl = table.new(position.top_right, 1, 1, bgcolor = color.new(color.black, 30))
if showTbl and barstate.islast
    string txt = na(fund8h) ? "Funding: no data" : "Funding " + (fund8h >= 0 ? "+" : "") + str.tostring(fund8h, "#.####") + "% / 8h · " + (fund8h >= 0 ? "+" : "") + str.tostring(fund8h * 1095, "#.#") + "% / yr"
    table.cell(tbl, 0, 0, txt, text_color = fund8h >= 0 ? posCol : negCol, text_size = size.small)

← 返回列表