本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// 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 · Spot vs Perp Flow Lite", shorttitle = "HT Flow Lite", overlay = false, max_labels_count = 5)
// Who is pushing the move: the spot market or the perp market?
// Cumulative volume delta (CVD) of Binance spot against Binance perpetual futures for the chart's coin.
// Each line is divided by its own average bar volume, so spot and perps share one scale.
// Delta is estimated from lower-timeframe candles (close position in the range × volume),
// not from exchange trade data.
// ───────────── Inputs ─────────────
grpD = "Data"
anchorIn = input.string("Auto", "CVD reset", options = ["Auto", "Day", "Week", "Rolling"], group = grpD, tooltip = "Auto: daily reset up to 1h charts, weekly up to 4h, rolling window above.")
rollLen = input.int(120, "Rolling window (bars)", minval = 10, maxval = 2000, group = grpD)
lb = input.int(12, "Read-out lookback (bars)", minval = 3, maxval = 200, group = grpD, tooltip = "Net buying of spot and perps over the last N bars, shown in the read-out.")
thr = input.float(3.0, "Flat below ± x % net of volume", minval = 0.5, maxval = 30, step = 0.5, group = grpD, tooltip = "Net buying = estimated buy volume minus sell volume, as % of volume. Inside ± this value a side counts as flat.")
grpV = "Display"
showTbl = input.bool(true, "Show read-out", group = grpV)
posIn = input.string("Top right", "Read-out position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grpV)
scheme = input.string("Black & White", "Color scheme", options = ["Black & White", "Classic (dark)", "Light", "Colorblind-safe", "Amber (dark)"], group = grpV, tooltip = "Light = for white chart backgrounds.")
color cSpot = switch scheme
"Classic (dark)" => #FFD600
"Light" => #E65100
"Colorblind-safe" => #E69F00
=> #FFB000
color cPerp = switch scheme
"Classic (dark)" => #E6E6E6
"Light" => #263238
"Colorblind-safe" => #56B4E9
"Amber (dark)" => #4C9AFF
=> #FFFFFF
color cBuy = switch scheme
"Classic (dark)" => #26A69A
"Light" => #089981
"Colorblind-safe" => #56B4E9
"Amber (dark)" => #FFB000
=> #FFFFFF
color cSell = switch scheme
"Classic (dark)" => #EF5350
"Light" => #F23645
"Colorblind-safe" => #D55E00
=> #9A9A9A
color cTxt = switch scheme
"Light" => #131722
=> #E0E0E0
color cMute = switch scheme
"Classic (dark)" => #6B7280
"Light" => #9598A1
=> #6E6E6E
color cZero = switch scheme
"Classic (dark)" => #3A4150
"Light" => #B2B5BE
=> #333333
// ───────────── Symbols ─────────────
string coin = syminfo.basecurrency != "" ? syminfo.basecurrency : syminfo.ticker
string spotSym = "BINANCE:" + coin + "USDT"
string perpSym = "BINANCE:" + coin + "USDT.P"
int tfs = timeframe.in_seconds()
string ltf = tfs < 60 ? timeframe.period : tfs <= 300 ? "1" : tfs <= 3600 ? "5" : tfs <= 14400 ? "15" : "60"
// ───────────── Delta ─────────────
// estimated delta of one candle: +volume when it closes at the high, −volume at the low
f_bar() =>
float r = high - low
r > 0 ? volume * (2 * close - high - low) / r : 0.0
f_delta(string sym) =>
array<float> a = request.security_lower_tf(sym, ltf, f_bar(), ignore_invalid_symbol = true)
float dC = request.security(sym, timeframe.period, f_bar(), ignore_invalid_symbol = true)
float v = request.security(sym, timeframe.period, volume, ignore_invalid_symbol = true)
[a.size() > 0 ? a.sum() : dC, v]
[dS, vS] = f_delta(spotSym)
[dP, vP] = f_delta(perpSym)
// ───────────── CVD (normalized) ─────────────
string anc = anchorIn == "Auto" ? (tfs <= 3600 ? "Day" : tfs <= 14400 ? "Week" : "Rolling") : anchorIn
bool newP = anc == "Day" ? timeframe.change("D") : anc == "Week" ? timeframe.change("W") : false
f_cvd(float d, float v) =>
var float c = 0.0
if newP
c := 0.0
c += nz(d)
float roll = math.sum(nz(d), rollLen)
float av = ta.sma(nz(v), 200)
float raw = anc == "Rolling" ? roll : c
av > 0 ? raw / av : na
float nS = f_cvd(dS, vS)
float nP = f_cvd(dP, vP)
plot(newP ? na : nS, "Spot CVD (Binance)", color = cSpot, linewidth = 2, style = plot.style_linebr)
plot(newP ? na : nP, "Perp CVD (Binance)", color = cPerp, linewidth = 1, style = plot.style_linebr)
hline(0, "Zero", color = cZero, linestyle = hline.style_dotted)
// ───────────── Read-out ─────────────
f_net(float d, float v) =>
float sv = math.sum(nz(v), lb)
float sd = math.sum(nz(d), lb)
sv > 0 ? sd / sv * 100 : na
float sNet = f_net(dS, vS)
float pNet = f_net(dP, vP)
bool sB = sNet > thr
bool sS = sNet < -thr
bool fB = pNet > thr
bool fS = pNet < -thr
string rd = sB and fB ? "BOTH BUYING" : sS and fS ? "BOTH SELLING" : sB and fS ? "SPOT BUYS · PERPS SELL" : sS and fB ? "SPOT SELLS · PERPS BUY" : sB ? "SPOT BUYING · perps flat" : sS ? "SPOT SELLING · perps flat" : fB ? "PERPS BUYING · spot flat" : fS ? "PERPS SELLING · spot flat" : "NO CLEAR FLOW"
color rc = sB and fB ? cBuy : sS and fS ? cSell : (sB and fS) or (sS and fB) ? cSpot : sB or fB ? cBuy : sS or fS ? cSell : cMute
f_sg(float v) =>
na(v) ? "–" : (v >= 0 ? "+" : "") + str.tostring(v, "0.0") + "%"
var table T = table.new(posIn == "Top left" ? position.top_left : posIn == "Bottom right" ? position.bottom_right : posIn == "Bottom left" ? position.bottom_left : position.top_right, 1, 3, bgcolor = color.new(#000000, 100), border_width = 0)
f_c(int r, string t, color c) =>
table.cell(T, 0, r, t, text_color = c, text_size = size.small, text_font_family = font.family_monospace, text_halign = text.align_left)
var array<label> LB = array.new<label>()
// tag above the line for the higher series, below for the lower one, so the tags never overlap
f_tag(float v, string t, color c, bool above) =>
if not na(v)
LB.push(label.new(bar_index + 2, v, t, style = above ? label.style_label_lower_left : label.style_label_upper_left, color = color.new(#000000, 100), textcolor = c, size = size.small, text_font_family = font.family_monospace))
if barstate.islast
for b in LB
b.delete()
LB.clear()
bool spotUp = nz(nS) >= nz(nP)
f_tag(nS, "spot", cSpot, spotUp)
f_tag(nP, "perp", cPerp, not spotUp)
if showTbl
bool noData = na(vS) and na(vP)
f_c(0, "HT FLOW LITE · " + coin + " · last " + str.tostring(lb) + " bars", cMute)
f_c(1, noData ? "No Binance spot / perp data for " + coin : "→ " + rd, noData ? cMute : rc)
f_c(2, noData ? "" : "spot " + f_sg(sNet) + " · perp " + f_sg(pNet) + " net", cTxt)