本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("CTZ Whale vs Retail Flow - Multi Coin", "CTZ W/R Multi", overlay = true, max_labels_count = 500)
// ═════════ Coin detection ═════════
// Reads the coin on the chart (e.g. SOL from BINANCE:SOLUSDT or BINANCE:SOLUSDT.P)
// and builds every exchange / premium / OI symbol from it automatically.
string base = syminfo.basecurrency != "" ? syminfo.basecurrency : syminfo.ticker
// ═════════ Inputs ═════════
grpE = "Exchanges (spot, auto from chart coin)"
use1 = input.bool(true, "Binance", inline = "e1", group = grpE)
ov1 = input.string("", "", inline = "e1", group = grpE, tooltip = "Leave blank for auto. Or type EXCHANGE:TICKER to override.")
use2 = input.bool(true, "Coinbase", inline = "e2", group = grpE)
ov2 = input.string("", "", inline = "e2", group = grpE)
use3 = input.bool(true, "OKX", inline = "e3", group = grpE)
ov3 = input.string("", "", inline = "e3", group = grpE)
use4 = input.bool(true, "Bybit", inline = "e4", group = grpE)
ov4 = input.string("", "", inline = "e4", group = grpE)
grpD = "Whale Detection"
ltfIn = input.timeframe("", "Lower timeframe (blank = auto)", group = grpD)
largeMult = input.float(10.0, "Whale size (x avg intrabar volume)", minval = 1.0, step = 0.5, group = grpD)
baseLen = input.int(50, "Volume baseline length (bars)", minval = 5, group = grpD)
sumLen = input.int(20, "Flow window (bars)", minval = 2, group = grpD)
zLen = input.int(100, "Normalisation length (bars)", minval = 20, group = grpD)
zFlow = input.float(0.5, "Whale zone sensitivity (higher = fewer)", minval = 0.1, step = 0.1, group = grpD)
grpP = "Confirmation: Premium"
premMode = input.string("Auto", "Premium type", options = ["Auto", "Coinbase", "Gold", "Off"], group = grpP,
tooltip = "Auto = Gold premium for PAXG / XAUT, Coinbase premium for everything else.")
goldSym = input.string("OANDA:XAUUSD", "Spot gold symbol", group = grpP)
premLen = input.int(3, "Premium smoothing (bars)", minval = 1, group = grpP)
premAvgLen = input.int(50, "Gold premium average length (bars)", minval = 5, group = grpP)
premMin = input.float(0.0, "Min premium % for buy", step = 0.01, group = grpP)
grpO = "Confirmation: Open Interest Flush"
useOI = input.bool(true, "Use OI flush", group = grpO)
oiOv = input.string("", "OI symbol (blank = auto)", group = grpO, tooltip = "Auto uses BINANCE:<COIN>USDT.P_OI")
oiLen = input.int(10, "OI lookback (bars)", minval = 2, group = grpO)
oiDrop = input.float(3.0, "OI drop % from recent peak", minval = 0.1, step = 0.5, group = grpO)
grpS = "Signals"
minScore = input.int(2, "Min score to signal (out of 3)", minval = 1, maxval = 3, group = grpS)
needLow = input.bool(true, "Require price near recent low", group = grpS)
swingLen = input.int(50, "Price extreme lookback", minval = 5, group = grpS)
nearPct = input.float(3.0, "Within % of low / high", minval = 0.1, step = 0.5, group = grpS)
cooldown = input.int(10, "Cooldown bars between signals", minval = 0, group = grpS)
buysOnly = input.bool(false, "Buys only (hide all sell signals)", group = grpS)
showBg = input.bool(true, "Shade confirmed zones", group = grpS)
autoTune = input.bool(true, "Auto-tune on daily and above", group = grpS)
grpC = "Divergence Candles"
showDivC = input.bool(true, "Recolour divergence candles", group = grpC)
zBar = input.float(1.0, "Candle sensitivity (higher = fewer)", minval = 0.1, step = 0.1, group = grpC)
candleCnf = input.bool(true, "Candles need premium or OI confirmation", group = grpC)
bullCol = input.color(#00FF00, "Red candle, whales buying", group = grpC)
bearCol = input.color(#FF0000, "Green candle, whales selling", group = grpC)
grpL = "Low Levels (lines)"
showLvl1 = input.bool(true, "Chart timeframe levels", group = grpL)
showLvl2 = input.bool(true, "", inline = "t2", group = grpL)
lvlTf2 = input.timeframe("240", "Extra timeframe", inline = "t2", group = grpL)
showLvl3 = input.bool(true, "", inline = "t3", group = grpL)
lvlTf3 = input.timeframe("D", "Extra timeframe", inline = "t3", group = grpL)
pivLen = input.int(5, "Swing strength (bars each side)", minval = 2, group = grpL)
brkBuf = input.float(0.25, "Break buffer (x ATR below the low)", minval = 0.0, step = 0.05, group = grpL)
// ═════════ Symbols built from the chart coin ═════════
string sym1 = ov1 != "" ? ov1 : "BINANCE:" + base + "USDT"
string sym2 = ov2 != "" ? ov2 : "COINBASE:" + base + "USD"
string sym3 = ov3 != "" ? ov3 : "OKX:" + base + "USDT"
string sym4 = ov4 != "" ? ov4 : "BYBIT:" + base + "USDT"
string bnPrSym = "BINANCE:" + base + "USDT"
string cbPrSym = "COINBASE:" + base + "USD"
string oiSym = oiOv != "" ? oiOv : "BINANCE:" + base + "USDT.P_OI"
bool isGold = base == "PAXG" or base == "XAUT"
string pm = premMode == "Auto" ? (isGold ? "Gold" : "Coinbase") : premMode
// ═════════ Timeframe handling ═════════
autoLtf() =>
s = timeframe.in_seconds()
s <= 3600 ? "1" : s <= 14400 ? "5" : s <= 86400 ? "15" : "60"
ltf = ltfIn == "" ? autoLtf() : ltfIn
bool isHTF = autoTune and timeframe.in_seconds() >= 86400
float zBarE = isHTF ? zBar * 0.5 : zBar
float zFlowE = isHTF ? zFlow * 0.6 : zFlow
int zLenE = isHTF ? math.max(20, int(math.round(zLen * 0.6))) : zLen
int swingE = isHTF ? math.max(10, int(math.round(swingLen * 0.4))) : swingLen
float nearE = isHTF ? nearPct * 2 : nearPct
float oiDrpE = isHTF ? oiDrop * 2 : oiDrop
// ═════════ Per-exchange whale / retail split ═════════
f_flow(sym, tf, bLen, mult) =>
[h, l, c, v] = request.security_lower_tf(sym, tf, [high, low, close, volume], ignore_invalid_symbol = true)
// a coin not listed on an exchange returns na arrays, so treat it as "no data"
int sz = na(v) ? 0 : v.size()
float avgRaw = sz > 0 ? v.avg() : na
var float lastAvg = na
if not na(avgRaw)
lastAvg := avgRaw
float base_ = ta.sma(lastAvg, bLen)
float thr = nz(base_) * mult
float wB = 0.0
float wS = 0.0
float rB = 0.0
float rS = 0.0
if sz > 0 and thr > 0
for i = 0 to sz - 1
float vol = v.get(i)
float rng = h.get(i) - l.get(i)
float bp = rng > 0 ? (c.get(i) - l.get(i)) / rng : 0.5
float bv = vol * bp
float sv = vol - bv
if vol >= thr
wB += bv
wS += sv
else
rB += bv
rS += sv
[wB - wS, rB - rS, wB + wS + rB + rS]
[w1, r1, t1] = f_flow(sym1, ltf, baseLen, largeMult)
[w2, r2, t2] = f_flow(sym2, ltf, baseLen, largeMult)
[w3, r3, t3] = f_flow(sym3, ltf, baseLen, largeMult)
[w4, r4, t4] = f_flow(sym4, ltf, baseLen, largeMult)
float whaleNet = (use1 ? w1 : 0.0) + (use2 ? w2 : 0.0) + (use3 ? w3 : 0.0) + (use4 ? w4 : 0.0)
float retNet = (use1 ? r1 : 0.0) + (use2 ? r2 : 0.0) + (use3 ? r3 : 0.0) + (use4 ? r4 : 0.0)
float totVol = (use1 ? t1 : 0.0) + (use2 ? t2 : 0.0) + (use3 ? t3 : 0.0) + (use4 ? t4 : 0.0)
int exCount = (use1 and t1 > 0 ? 1 : 0) + (use2 and t2 > 0 ? 1 : 0) + (use3 and t3 > 0 ? 1 : 0) + (use4 and t4 > 0 ? 1 : 0)
// ═════════ Rolling whale flow ═════════
float whaleSum = math.sum(whaleNet, sumLen)
float retSum = math.sum(retNet, sumLen)
float sumTot = math.sum(totVol, sumLen)
float whalePct = sumTot > 0 ? whaleSum / sumTot * 100 : 0.0
float retPct = sumTot > 0 ? retSum / sumTot * 100 : 0.0
float sdBar = ta.stdev(whaleNet, zLenE)
float sdSum = ta.stdev(whaleSum, zLenE)
float whaleZ = nz(sdBar) > 0 ? whaleNet / sdBar : 0.0
float whaleSumZ = nz(sdSum) > 0 ? whaleSum / sdSum : 0.0
// ═════════ Premium (Coinbase or Gold) ═════════
float bnPx = request.security(bnPrSym, timeframe.period, close, ignore_invalid_symbol = true)
float cbPx = request.security(cbPrSym, timeframe.period, close, ignore_invalid_symbol = true)
float goldPx = request.security(goldSym, timeframe.period, close, gaps = barmerge.gaps_on, ignore_invalid_symbol = true)
// Coinbase premium: Coinbase USD vs Binance USDT (same as the BTC version)
float cbRaw = nz(bnPx) > 0 and not na(cbPx) ? (cbPx - bnPx) / bnPx * 100 : na
float cbPrem = ta.sma(cbRaw, premLen)
// Gold premium: coin vs spot gold, compared with its own average.
// Gold has no bars at weekends, so the last valid value is held.
float gNow = nz(goldPx) > 0 and not na(bnPx) ? (bnPx - goldPx) / goldPx * 100 : na
var float gHold = na
if not na(gNow)
gHold := gNow
float gPrem = ta.sma(gHold, premLen)
float gBase = ta.sma(gHold, premAvgLen)
float gDev = gPrem - gBase
bool usingCB = pm == "Coinbase"
bool usingG = pm == "Gold"
float premSig = usingCB ? cbPrem : usingG ? gDev : na
bool premBuy = not na(premSig) and premSig > premMin
bool premSell = not na(premSig) and premSig < -premMin
// ═════════ Open interest ═════════
float oi = request.security(oiSym, timeframe.period, close, ignore_invalid_symbol = true)
float oiPeak = ta.highest(oi, oiLen)
float oiTrough = ta.lowest(oi, oiLen)
float oiFromPk = nz(oiPeak) > 0 ? (oi - oiPeak) / oiPeak * 100 : na
float oiFromTr = nz(oiTrough) > 0 ? (oi - oiTrough) / oiTrough * 100 : na
bool oiFlush = useOI and not na(oiFromPk) and oiFromPk <= -oiDrpE // longs flushed
bool oiBuild = useOI and not na(oiFromTr) and oiFromTr >= oiDrpE // leverage piling in
// ═════════ Scores ═════════
bool nearLow = close <= ta.lowest(low, swingE) * (1 + nearE / 100)
bool nearHigh = close >= ta.highest(high, swingE) * (1 - nearE / 100)
bool whaleBuy = whaleSumZ > zFlowE and retSum < 0
bool whaleSell = whaleSumZ < -zFlowE and retSum > 0
int buyScore = (whaleBuy ? 1 : 0) + (premBuy ? 1 : 0) + (oiFlush ? 1 : 0)
int sellScore = (whaleSell ? 1 : 0) + (premSell ? 1 : 0) + (oiBuild ? 1 : 0)
bool buyZone = whaleBuy and buyScore >= minScore
bool sellZone = not buysOnly and whaleSell and sellScore >= minScore
bool buyCond = buyZone and (not needLow or nearLow)
bool sellCond = sellZone and (not needLow or nearHigh)
// ═════════ Signals with cooldown ═════════
var int lastBuy = -100000
var int lastSell = -100000
bool buySig = buyCond and not buyCond[1] and bar_index - lastBuy > cooldown
bool sellSig = sellCond and not sellCond[1] and bar_index - lastSell > cooldown
if buySig
lastBuy := bar_index
label.new(bar_index, low, "WHALE BUY\n" + str.tostring(buyScore) + "/3", yloc = yloc.belowbar,
style = label.style_label_up, color = color.new(color.green, 0), textcolor = color.white, size = size.small)
if sellSig
lastSell := bar_index
label.new(bar_index, high, "WHALE SELL\n" + str.tostring(sellScore) + "/3", yloc = yloc.abovebar,
style = label.style_label_down, color = color.new(color.red, 0), textcolor = color.white, size = size.small)
// ═════════ Divergence candles ═════════
bool hiddenBuy = close < open and whaleZ > zBarE and (not candleCnf or premBuy or oiFlush)
bool hiddenSell = not buysOnly and close > open and whaleZ < -zBarE and (not candleCnf or premSell or oiBuild)
color divCol = hiddenBuy ? bullCol : hiddenSell ? bearCol : na
bool divOn = showDivC and (hiddenBuy or hiddenSell)
plotcandle(divOn ? open : na, divOn ? high : na, divOn ? low : na, divOn ? close : na,
"Whale divergence candle", color = divCol, wickcolor = divCol, bordercolor = color.white)
// ═════════ Zone shading (darker = higher score) ═════════
color buyBg = buyScore == 3 ? color.new(color.green, 75) : color.new(color.green, 88)
color sellBg = sellScore == 3 ? color.new(color.red, 75) : color.new(color.red, 88)
bgcolor(showBg and buyZone ? buyBg : na, title = "Confirmed accumulation")
bgcolor(showBg and sellZone ? sellBg : na, title = "Confirmed distribution")
// ═════════ Low levels (expected low / break / squeeze target) ═════════
f_lows(int pLen, int sLen, float buf) =>
var array<float> piv = array.new<float>()
float pl = ta.pivotlow(low, pLen, pLen)
if not na(pl)
piv.push(pl)
if piv.size() > 40
piv.shift()
float atr = nz(ta.atr(14))
float fallback = ta.lowest(low, sLen)
int n = piv.size()
// expected low = highest swing low that price has NOT closed below its break buffer
float ex = na
if n > 0
for i = 0 to n - 1
float v = piv.get(i)
if v - atr * buf < close and (na(ex) or v > ex)
ex := v
if na(ex)
ex := fallback
float br = ex - atr * buf
float nx = na
if n > 0
for i = 0 to n - 1
float v = piv.get(i)
if v < br and (na(nx) or v > nx)
nx := v
if na(nx)
nx := br - atr
// status: 0 holding, 1 sweep, 3 testing, 2 broken
var float brokenLvl = na
float brPrev = br[1]
if not na(brPrev) and close < brPrev
brokenLvl := brPrev
if not na(brokenLvl) and close > brokenLvl
brokenLvl := na
int st = not na(brokenLvl) ? 2 : close < ex ? 3 : low < ex ? 1 : 0
[ex, br, nx, st, brokenLvl]
[exC, brC, nxC, stC, bkC] = f_lows(pivLen, swingLen, brkBuf)
[ex2, br2, nx2, st2, bk2] = request.security(syminfo.tickerid, lvlTf2, f_lows(pivLen, swingLen, brkBuf))
[ex3, br3, nx3, st3, bk3] = request.security(syminfo.tickerid, lvlTf3, f_lows(pivLen, swingLen, brkBuf))
f_tf(string tf) =>
int s = timeframe.in_seconds(tf)
s >= 86400 ? str.tostring(int(s / 86400)) + "D" : s >= 3600 ? str.tostring(int(s / 3600)) + "H" : str.tostring(int(s / 60)) + "m"
// price format follows the coin's tick size, so it works for BTC and tiny alts alike
f_px(float v) => na(v) ? "n/a" : str.tostring(v, format.mintick)
f_draw(array<line> L, array<label> B, float ex, float br, float nx, string pre) =>
if not na(ex)
L.push(line.new(bar_index - 40, ex, bar_index + 15, ex, color = color.lime, style = line.style_dashed, width = 1))
L.push(line.new(bar_index - 40, br, bar_index + 15, br, color = color.red, style = line.style_solid, width = 2))
L.push(line.new(bar_index - 40, nx, bar_index + 15, nx, color = color.orange, style = line.style_dotted, width = 1))
B.push(label.new(bar_index + 15, ex, pre + "Expected low " + f_px(ex), style = label.style_label_left,
color = color.new(color.black, 100), textcolor = color.lime, size = size.small))
B.push(label.new(bar_index + 15, br, pre + "Break below " + f_px(br), style = label.style_label_left,
color = color.new(color.black, 100), textcolor = color.red, size = size.small))
B.push(label.new(bar_index + 15, nx, pre + "Squeeze target " + f_px(nx), style = label.style_label_left,
color = color.new(color.black, 100), textcolor = color.orange, size = size.small))
var array<line> lvLines = array.new<line>()
var array<label> lvLabels = array.new<label>()
if barstate.islast
for ln in lvLines
line.delete(ln)
for lb in lvLabels
label.delete(lb)
lvLines.clear()
lvLabels.clear()
int chartSec = timeframe.in_seconds()
if showLvl1
f_draw(lvLines, lvLabels, exC, brC, nxC, f_tf(timeframe.period) + " ")
if showLvl2 and timeframe.in_seconds(lvlTf2) != chartSec
f_draw(lvLines, lvLabels, ex2, br2, nx2, f_tf(lvlTf2) + " ")
if showLvl3 and timeframe.in_seconds(lvlTf3) != chartSec
f_draw(lvLines, lvLabels, ex3, br3, nx3, f_tf(lvlTf3) + " ")
// ═════════ Dashboard ═════════
f_col(bool good, bool bad) => good ? color.lime : bad ? color.red : color.gray
string premLbl = usingCB ? "CB premium" : usingG ? "Gold premium" : "Premium"
string premTxt = usingCB ? (na(cbPrem) ? "n/a" : str.tostring(cbPrem, "0.000") + "%") :
usingG ? (na(gPrem) ? "n/a" : str.tostring(gPrem, "0.00") + "% (" + (gDev >= 0 ? "+" : "") + str.tostring(gDev, "0.00") + ")") : "off"
var table tb = table.new(position.top_right, 2, 8, bgcolor = color.new(color.black, 30), border_width = 1)
if barstate.islast
table.cell(tb, 0, 0, base + " LTF / Exch", text_color = color.white)
table.cell(tb, 1, 0, ltf + " / " + str.tostring(exCount), text_color = color.white)
table.cell(tb, 0, 1, "Whale flow", text_color = color.white)
table.cell(tb, 1, 1, str.tostring(whalePct, "0.0") + "%", text_color = f_col(whalePct > 0, whalePct < 0))
table.cell(tb, 0, 2, "Retail flow", text_color = color.white)
table.cell(tb, 1, 2, str.tostring(retPct, "0.0") + "%", text_color = f_col(retPct > 0, retPct < 0))
table.cell(tb, 0, 3, "Whale strength", text_color = color.white)
table.cell(tb, 1, 3, str.tostring(whaleSumZ, "0.00"), text_color = f_col(whaleBuy, whaleSell))
table.cell(tb, 0, 4, premLbl, text_color = color.white)
table.cell(tb, 1, 4, premTxt, text_color = f_col(premBuy, premSell))
table.cell(tb, 0, 5, "OI vs peak", text_color = color.white)
table.cell(tb, 1, 5, na(oiFromPk) ? "n/a" : str.tostring(oiFromPk, "0.0") + "%", text_color = f_col(oiFlush, oiBuild))
table.cell(tb, 0, 6, "Buy / Sell score", text_color = color.white)
table.cell(tb, 1, 6, str.tostring(buyScore) + "/3 | " + str.tostring(sellScore) + "/3",
text_color = buyScore >= minScore ? color.lime : sellScore >= minScore ? color.red : color.gray)
table.cell(tb, 0, 7, "State", text_color = color.white)
table.cell(tb, 1, 7, buyZone ? "ACCUMULATION" : sellZone ? "DISTRIBUTION" : "Neutral",
text_color = buyZone ? color.lime : sellZone ? color.red : color.gray)
// ═════════ Alerts ═════════
alertcondition(buySig, "Whale buy (confirmed)", "Confirmed whale buying near lows")
alertcondition(sellSig, "Whale sell (confirmed)", "Confirmed whale selling near highs")
alertcondition(hiddenBuy, "Red candle, whales buying", "Price dropping but whales are net buying")
alertcondition(hiddenSell, "Green candle, whales selling", "Price pumping but whales are net selling")
alertcondition(stC == 2 and stC[1] != 2, "Breaking low", "Price closed below the break level - long squeeze risk")
alertcondition(stC == 1 and stC[1] == 0, "Low swept", "Expected low wicked but closed back above")
alertcondition(stC != 2 and stC[1] == 2, "Broken low reclaimed", "Price closed back above the broken low")