Rule Sentinel Crypto Liquidity

RuleSentinel · study · 403 行 · 点赞 4 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RuleSentinel
 
//@version=6
indicator("Rule Sentinel Crypto Liquidity", shorttitle="RS Crypto Liquidity", overlay=false, format=format.price, precision=2)
 
//──────────────────────────────
// Inputs
//──────────────────────────────
grpM       = "Markets"
stab_txt   = input.text_area("USDT, USDC, DAI", "Stablecoins counted (comma-separated, up to 5)", group = grpM, display = display.none, tooltip = "TradingView market-cap tickers (CRYPTOCAP:<ticker>). Their caps are added together for the Stablecoins row and the Backdrop.")
etf_txt    = input.text_area("NASDAQ:IBIT, CBOE:FBTC, AMEX:GBTC", "Spot Bitcoin ETFs (comma-separated, up to 3)", group = grpM, display = display.none, tooltip = "Full TradingView symbols. Their volumes are added together for the ETF row (activity, not net inflows).")
 
grpD        = "Display"
show_rollup = input.bool(true, "Show MTF rollup table", group = grpD, display = display.none, tooltip = "On: the main table plus the 1H / 4H / 1D rollup and TOTAL. Off: the main table only.")
show_hist   = input.bool(true, "Show score history in the pane", group = grpD, display = display.none, tooltip = "Columns showing the TOTAL score over time (or the main score when the rollup is off), in the lower part of the pane.")
hist_smooth = input.int(7, "History smoothing (candles)", minval = 1, maxval = 50, group = grpD, display = display.none, tooltip = "Average of the score over this many candles. 1 = raw score on every candle.")
show_age    = input.bool(true, "Show regime age", group = grpD, display = display.none, tooltip = "How long the Daily liquidity reading (inflow, mixed or outflow) has held, counted from the Daily close at which it last changed.")
show_pct    = input.bool(true, "Show % move in the Dir cells", group = grpD, display = display.none, tooltip = "The change of each market over the lookback, as a % of its own value.")
use_heat    = input.bool(false, "Heatmap background (old style)", group = grpD, display = display.none)
txt_color   = input.color(color.white, "Table text color", group = grpD, display = display.none)
tbl_size    = input.string("Normal", "Table size", options = ["Tiny", "Small", "Normal", "Large"], group = grpD, display = display.none, tooltip = "Text size of both tables. Normal is the standard size. A larger size makes the tables taller: if they reach the score history, make the pane taller or turn the history off. Display only: readings and alerts do not change.")
tbl_sz      = switch tbl_size
    "Tiny"  => size.tiny
    "Small" => size.small
    "Large" => size.large
    => size.normal
 
grpB         = "Backdrop (30 / 90 days)"
show_bd      = input.bool(true, "Show backdrop rows", group = grpB, display = display.none, tooltip = "Total stablecoin supply over 30 and 90 days: the slow-moving dry-powder picture.")
bd_thr       = input.float(1.0, "Growing / shrinking at (% over 30 days)", minval = 0.1, maxval = 20.0, step = 0.1, group = grpB, display = display.none, tooltip = "Supply up at least this much over 30 days reads Growing; down at least this much reads Shrinking; otherwise Flat.")
 
grpA       = "⚠ Advanced - do not change"
usdt_d_sym = input.string("CRYPTOCAP:USDT.D",  "USDT.D dominance symbol", group = grpA, display = display.none)
btc_sym    = input.string("BINANCE:BTCUSDT",   "BTC spot symbol",         group = grpA, display = display.none)
perp_sym   = input.string("BINANCE:BTCUSDT.P", "BTC perp symbol",         group = grpA, display = display.none)
dxy_sym    = input.string("TVC:DXY",           "DXY symbol",              group = grpA, display = display.none)
fast_tf    = input.timeframe("60",  "Fast TF (1H)", group = grpA, display = display.none)
mid_tf     = input.timeframe("240", "Mid TF (4H)",  group = grpA, display = display.none)
slow_tf    = input.timeframe("D",   "Main TF (1D) - the main table", group = grpA, display = display.none)
lookback   = input.int(10, "Lookback candles for UP / DOWN", minval = 2, maxval = 100, group = grpA, display = display.none, tooltip = "Each market is compared with its value this many candles ago, on that row's own timeframe.")
ema_len    = input.int(50, "EMA length for the trend", minval = 5, maxval = 200, group = grpA, display = display.none, tooltip = "UP needs the market above this EMA and rising over the lookback; DOWN needs it below and falling.")
 
//──────────────────────────────
// Lists
//──────────────────────────────
var array<string> stabs = array.new<string>()
var array<string> etfs  = array.new<string>()
if barstate.isfirst
    for part in str.split(str.replace_all(str.replace_all(stab_txt, " ", ""), "\n", ""), ",")
        if str.length(part) > 0 and array.size(stabs) < 5
            array.push(stabs, str.upper(part))
    for part in str.split(str.replace_all(str.replace_all(etf_txt, " ", ""), "\n", ""), ",")
        if str.length(part) > 0 and array.size(etfs) < 3
            array.push(etfs, str.upper(part))
 
//──────────────────────────────
// Helpers
//──────────────────────────────
f_pct(cur, prev) =>
    na(cur) or na(prev) or prev == 0.0 ? na : (cur - prev) / prev * 100.0
 
// -1 DOWN, 0 STABLE, 1 UP. UP = above its EMA and above its value `lookback` candles ago; DOWN = the mirror.
f_dir(x, xlb, e) =>
    na(x) or na(xlb) or na(e) ? 0 : x > e and x > xlb ? 1 : x < e and x < xlb ? -1 : 0
 
// ---- evaluated inside the requests, on each timeframe's own candles ----
// Price-type market (caps, dominance, DXY): live values
f_lvl() =>
    int off = time_close <= timenow ? 0 : 1
    float e = ta.ema(close, ema_len)
    [close, close[lookback], e, close[30], close[90], close[off], close[off + lookback], e[off]]
 
// Volume window: one week on the Daily and above, one day (24 hours) on intraday timeframes
f_win(tf) =>
    int s = timeframe.in_seconds(tf)
    s >= 86400 ? 7 : math.max(1, int(math.round(86400 / s)))
 
// Volume-type market: the last CLOSED candle (the live candle is skipped until it closes)
f_vol(w) =>
    int off = time_close <= timenow ? 0 : 1
    float a = math.sum(volume, w)
    float e = ta.ema(a, ema_len)
    [a[off], a[off + w], e[off]]
 
// Perp: volume plus perp flow (volume signed by the candle's direction, accumulated), last closed candle
f_perp(w) =>
    int off = time_close <= timenow ? 0 : 1
    float a    = math.sum(volume, w)
    float ve   = ta.ema(a, ema_len)
    float flow = ta.cum(nz(math.sign(close - close[1])) * nz(volume))
    float fe   = ta.ema(flow, ema_len)
    [a[off], a[off + w], ve[off], flow[off], flow[off + w], fe[off]]
 
// Stablecoins on one timeframe: summed caps (now, lookback ago, EMA) and, for the backdrop, 30 and 90 candles ago
f_stables(tf) =>
    float sN = 0.0
    float sL = 0.0
    float sE = 0.0
    float bN = 0.0
    float b30 = 0.0
    float b90 = 0.0
    float cN = 0.0
    float cL = 0.0
    float cE = 0.0
    int   n  = 0
    int   nb = 0
    string miss = ""
    if array.size(stabs) > 0
        for i = 0 to array.size(stabs) - 1
            tk = array.get(stabs, i)
            [c, cl, e, c30, c90, cc, ccl, ce] = request.security("CRYPTOCAP:" + tk, tf, f_lvl(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
            if not na(c) and not na(cl) and not na(e)
                sN += c
                sL += cl
                sE += e
                cN += nz(cc)
                cL += nz(ccl)
                cE += nz(ce)
                n  += 1
            else
                miss += tk + " (no data)  "
            if not na(c) and not na(c30) and not na(c90)
                bN  += c
                b30 += c30
                b90 += c90
                nb  += 1
    [n > 0 ? sN : na, n > 0 ? sL : na, n > 0 ? sE : na, n, miss, nb > 0 ? bN : na, nb > 0 ? b30 : na, nb > 0 ? b90 : na, n > 0 ? cN : na, n > 0 ? cL : na, n > 0 ? cE : na]
 
// Spot ETFs (Daily only): summed volumes of the last closed session
f_etfs() =>
    float vN = 0.0
    float vL = 0.0
    float vE = 0.0
    int   n  = 0
    string miss = ""
    if array.size(etfs) > 0
        for i = 0 to array.size(etfs) - 1
            sym = array.get(etfs, i)
            [v, vl, e] = request.security(sym, slow_tf, f_vol(5), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
            if not na(v) and not na(vl) and not na(e)
                vN += v
                vL += vl
                vE += e
                n  += 1
            else
                miss += sym + " (no data)  "
    [n > 0 ? vN : na, n > 0 ? vL : na, n > 0 ? vE : na, n, miss]
 
f_req_lvl(sym, tf) =>
    request.security(sym, tf, f_lvl(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
f_req_vol(sym, tf) =>
    request.security(sym, tf, f_vol(f_win(tf)), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
f_req_perp(sym, tf) =>
    request.security(sym, tf, f_perp(f_win(tf)), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
 
f_tf_label(tf) =>
    int s = timeframe.in_seconds(tf)
    s < 3600 ? str.tostring(math.round(s / 60)) + "m" : s < 86400 ? str.tostring(math.round(s / 3600)) + "H" : s < 604800 ? str.tostring(math.round(s / 86400)) + "D" : str.tostring(math.round(s / 604800)) + "W"
 
//──────────────────────────────
// Data: the same 7 measures on each timeframe
//──────────────────────────────
// ETFs are read on the Daily for all three rows (they trade only in US market hours)
[etfN, etfL, etfE, etfCount, etfMiss] = f_etfs()
etfDir = f_dir(etfN, etfL, etfE)
etfPct = f_pct(etfN, etfL)
 
// Main TF (Daily)
[stN_s, stL_s, stE_s, stCount, stMiss, bdN, bd30, bd90, stCN, stCL, stCE] = f_stables(slow_tf)
[ud_s, udl_s, ude_s, ud30_s, ud90_s, udc_s, udcl_s, udce_s] = f_req_lvl(usdt_d_sym, slow_tf)
[bv_s, bvl_s, bve_s]                 = f_req_vol(btc_sym, slow_tf)
[pv_s, pvl_s, pve_s, pf_s, pfl_s, pfe_s] = f_req_perp(perp_sym, slow_tf)
[dx_s, dxl_s, dxe_s, dx30_s, dx90_s, dxc_s, dxcl_s, dxce_s] = f_req_lvl(dxy_sym, slow_tf)
 
st_d_s = f_dir(stN_s, stL_s, stE_s)
ud_d_s = f_dir(ud_s, udl_s, ude_s)
bv_d_s = f_dir(bv_s, bvl_s, bve_s)
pv_d_s = f_dir(pv_s, pvl_s, pve_s)
pf_d_s = f_dir(pf_s, pfl_s, pfe_s)
dx_d_s = f_dir(dx_s, dxl_s, dxe_s)
slowScore = st_d_s - ud_d_s + bv_d_s + etfDir + pv_d_s + pf_d_s - dx_d_s
// The same score on closed Daily values only (volumes are already closed): used for the regime age
slowClosed = f_dir(stCN, stCL, stCE) - f_dir(udc_s, udcl_s, udce_s) + bv_d_s + etfDir + pv_d_s + pf_d_s - f_dir(dxc_s, dxcl_s, dxce_s)
 
// Mid TF (4H)
[stN_m, stL_m, stE_m, stC_m, stMs_m, b1_m, b2_m, b3_m, c1_m, c2_m, c3_m] = f_stables(mid_tf)
[ud_m, udl_m, ude_m, ud30_m, ud90_m, udc_m, udcl_m, udce_m] = f_req_lvl(usdt_d_sym, mid_tf)
[bv_m, bvl_m, bve_m]                 = f_req_vol(btc_sym, mid_tf)
[pv_m, pvl_m, pve_m, pf_m, pfl_m, pfe_m] = f_req_perp(perp_sym, mid_tf)
[dx_m, dxl_m, dxe_m, dx30_m, dx90_m, dxc_m, dxcl_m, dxce_m] = f_req_lvl(dxy_sym, mid_tf)
midScore = f_dir(stN_m, stL_m, stE_m) - f_dir(ud_m, udl_m, ude_m) + f_dir(bv_m, bvl_m, bve_m) + etfDir + f_dir(pv_m, pvl_m, pve_m) + f_dir(pf_m, pfl_m, pfe_m) - f_dir(dx_m, dxl_m, dxe_m)
 
// Fast TF (1H)
[stN_f, stL_f, stE_f, stC_f, stMs_f, b1_f, b2_f, b3_f, c1_f, c2_f, c3_f] = f_stables(fast_tf)
[ud_f, udl_f, ude_f, ud30_f, ud90_f, udc_f, udcl_f, udce_f] = f_req_lvl(usdt_d_sym, fast_tf)
[bv_f, bvl_f, bve_f]                 = f_req_vol(btc_sym, fast_tf)
[pv_f, pvl_f, pve_f, pf_f, pfl_f, pfe_f] = f_req_perp(perp_sym, fast_tf)
[dx_f, dxl_f, dxe_f, dx30_f, dx90_f, dxc_f, dxcl_f, dxce_f] = f_req_lvl(dxy_sym, fast_tf)
fastScore = f_dir(stN_f, stL_f, stE_f) - f_dir(ud_f, udl_f, ude_f) + f_dir(bv_f, bvl_f, bve_f) + etfDir + f_dir(pv_f, pvl_f, pve_f) + f_dir(pf_f, pfl_f, pfe_f) - f_dir(dx_f, dxl_f, dxe_f)
 
// Weighted TOTAL: 1H x1, 4H x2, 1D x3 (range -42 to +42)
total    = fastScore * 1 + midScore * 2 + slowScore * 3
maxTotal = 7 * (1 + 2 + 3)
 
//──────────────────────────────
// Labels and colours
//──────────────────────────────
f_main_label(s) =>
    s >= 3 ? "Strong inflow" : s >= 1 ? "Inflow" : s == 0 ? "Mixed / neutral" : s >= -2 ? "Outflow" : "Strong outflow"
f_main_col(s) =>
    s >= 3 ? color.new(color.green, 0) : s >= 1 ? color.new(color.green, 25) : s == 0 ? color.new(color.orange, 30) : s >= -2 ? color.new(color.red, 25) : color.new(color.red, 0)
f_tf_word(s) =>
    s > 0 ? "Inflow" : s < 0 ? "Outflow" : "Mixed"
f_tf_col(s) =>
    s > 0 ? color.new(color.green, 15) : s < 0 ? color.new(color.red, 15) : color.new(color.orange, 30)
f_total_label(t) =>
    t >= 20 ? "Strong tailwind" : t >= 10 ? "Tailwind" : t >= 1 ? "Slight tailwind" : t == 0 ? "Neutral" : t > -10 ? "Slight headwind" : t > -20 ? "Headwind" : "Strong headwind"
f_total_col(t) =>
    t >= 20 ? color.new(color.green, 0) : t >= 10 ? color.new(color.green, 20) : t >= 1 ? color.new(color.lime, 40) : t == 0 ? color.new(color.orange, 30) : t > -10 ? color.new(color.orange, 10) : t > -20 ? color.new(color.red, 20) : color.new(color.red, 0)
 
// The Daily check: a positive TOTAL while the main TF shows outflow is flagged
slow_ok     = slowScore >= 0
slow_name   = timeframe.in_seconds(slow_tf) == 86400 ? "Daily" : f_tf_label(slow_tf)
unconfirmed = total > 0 and not slow_ok
total_label = unconfirmed ? "Short-term inflow, " + slow_name + " not confirming" : f_total_label(total)
total_col   = unconfirmed ? color.rgb(201, 138, 27) : f_total_col(total)
 
// Cells
f_cell_col(d, inv, isna) =>
    m = inv ? -d : d
    isna ? color.new(color.gray, 60) : m == 1 ? color.new(color.green, 10) : m == -1 ? color.new(color.red, 10) : color.new(color.orange, 10)
f_dir_word(d) =>
    d == 1 ? "UP" : d == -1 ? "DOWN" : "STABLE"
f_dir_text(d, isna, pct) =>
    string out = f_dir_word(d)
    if isna
        out := "n/a"
    else if show_pct and not na(pct)
        out := out + " " + (pct >= 0 ? "+" : "") + str.tostring(pct, "#.##") + "%"
    out
 
//──────────────────────────────
// Score history in the pane (optional old heatmap background)
//──────────────────────────────
hist_raw  = show_rollup ? float(total) : float(slowScore)
hist_val  = ta.sma(hist_raw, hist_smooth)
hist_max  = show_rollup ? 42.0 : 7.0
hist_norm = hist_val / (hist_max / 2.0)
hist_col  = hist_norm >= 0.5 ? color.new(color.green, 0) : hist_norm > 0.05 ? color.new(color.green, 45) : hist_norm > -0.05 ? color.new(color.orange, 40) : hist_norm > -0.5 ? color.new(color.red, 45) : color.new(color.red, 0)
plot(show_hist ? hist_val : na, "Score history", color = hist_col, style = plot.style_columns, linewidth = 1, display = display.all - display.status_line)
plot(show_hist ? hist_max * 3.0 : na, "Scale top", color = color.new(color.black, 100), display = display.pane, editable = false)
plot(show_hist ? -hist_max : na, "Scale bottom", color = color.new(color.black, 100), display = display.pane, editable = false)
hline(0, "Zero", color = color.new(color.gray, 60), linestyle = hline.style_dotted)
bgcolor(use_heat ? color.new(show_rollup ? total_col : f_main_col(slowScore), 85) : na)
 
//──────────────────────────────
// Regime age: time since the Daily reading (inflow / mixed / outflow) last changed
//──────────────────────────────
slow_state = slowClosed > 0 ? 1 : slowClosed < 0 ? -1 : 0
var float regStart  = na
var float firstTime = na
if barstate.isfirst
    firstTime := time
// Changes only at a Daily close. On a Daily chart in history that close is this candle's close; on the live candle,
// and on a lower chart, it is the open of the first candle after it. Same age on every chart, live or after a reload.
if bar_index > 0 and slow_state != slow_state[1]
    regStart := timeframe.in_seconds() >= timeframe.in_seconds(slow_tf) and not barstate.isrealtime ? time_close : time
f_age_text(ms) =>
    float hrs = ms / 3600000.0
    int h = int(math.floor(hrs))
    int d = int(math.floor(hrs / 24))
    hrs < 24 ? str.tostring(h) + (h == 1 ? " hour" : " hours") : str.tostring(d) + (d == 1 ? " day" : " days")
nowT = math.min(timenow, time_close)
above_main = timeframe.in_seconds() > timeframe.in_seconds(slow_tf)
age_text = na(ud_s) and na(bv_s) ? "No data yet" : above_main ? "Shown on " + f_tf_label(slow_tf) + " charts and lower" : na(regStart) ? "over " + f_age_text(nowT - firstTime) + " (before chart history)" : f_age_text(math.max(0, nowT - regStart))
 
//──────────────────────────────
// Backdrop: total stablecoin supply over 30 and 90 days (Daily)
//──────────────────────────────
bd30p = f_pct(bdN, bd30)
bd90p = f_pct(bdN, bd90)
bd_state = na(bd30p) ? 0 : bd30p >= bd_thr ? 1 : bd30p <= -bd_thr ? -1 : 0
f_sgn(x) =>
    na(x) ? "n/a" : (x >= 0 ? "+" : "") + str.tostring(x, "0.00") + "%"
 
//──────────────────────────────
// Main table (main TF)
//──────────────────────────────
var table tbl = table.new(position.top_left, 3, 16, border_width = 1)
// The table is cleared only when its layout changes. Clearing it on every live tick could leave old text showing under the new.
varip string tblKey = ""
string tblLayout = na(bd30p) ? "a" : "b"
bool tblBuild = tblKey != tblLayout
f_row(r, item, d, inv, isna, pct, note) =>
    table.cell(tbl, 0, r, item, bgcolor = color.rgb(10,10,10), text_color = txt_color, text_halign = text.align_left, text_size = tbl_sz)
    table.cell(tbl, 1, r, f_dir_text(d, isna, pct), bgcolor = f_cell_col(d, inv, isna), text_color = txt_color, text_size = tbl_sz)
    table.cell(tbl, 2, r, note, bgcolor = color.rgb(10,10,10), text_color = txt_color, text_size = tbl_sz)
 
if barstate.islast
    if tblBuild
        table.clear(tbl, 0, 0, 2, 15)
    table.cell(tbl, 0, 0, " ", bgcolor = color.new(color.black, 100), text_color = color.new(color.black, 100), text_size = tbl_sz)
    table.cell(tbl, 1, 0, " ", bgcolor = color.new(color.black, 100), text_color = color.new(color.black, 100), text_size = tbl_sz)
    table.cell(tbl, 2, 0, " ", bgcolor = color.new(color.black, 100), text_color = color.new(color.black, 100), text_size = tbl_sz)
    table.cell(tbl, 0, 1, "RS · Crypto Liquidity", bgcolor = color.rgb(12,12,12), text_color = color.white, text_halign = text.align_center, text_size = tbl_sz)
    table.cell(tbl, 1, 1, "", bgcolor = color.rgb(12,12,12), text_size = tbl_sz)
    table.cell(tbl, 2, 1, "", bgcolor = color.rgb(12,12,12), text_size = tbl_sz)
    table.merge_cells(tbl, 0, 1, 2, 1)
    table.cell(tbl, 0, 2, "Item",  bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
    table.cell(tbl, 1, 2, "Dir",   bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
    table.cell(tbl, 2, 2, "Notes", bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
 
    f_row(3, "Stablecoins",  st_d_s, false, na(stN_s), f_pct(stN_s, stL_s), "↑ = more dry powder")
    f_row(4, "USDT.D",       ud_d_s, true,  na(ud_s),  f_pct(ud_s, udl_s),  "↑ = risk-off in crypto")
    f_row(5, "BTC spot vol", bv_d_s, false, na(bv_s),  f_pct(bv_s, bvl_s),  "Spot activity, 7 days vs prior 7")
    f_row(6, "ETF vol",      etfDir, false, na(etfN),  etfPct,              "ETF activity, 5 sessions vs prior 5")
    f_row(7, "Perp vol",     pv_d_s, false, na(pv_s),  f_pct(pv_s, pvl_s),  "Leverage, 7 days vs prior 7")
    f_row(8, "Perp flow",    pf_d_s, false, na(pf_s),  float(na),           "Perp volume on up vs down days")
    f_row(9, "DXY",          dx_d_s, true,  na(dx_s),  f_pct(dx_s, dxl_s),  "↑ dollar = headwind")
 
    table.cell(tbl, 0, 10, "Liquidity (" + f_tf_label(slow_tf) + ")", bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
    table.cell(tbl, 1, 10, "Score " + str.tostring(slowScore), bgcolor = f_main_col(slowScore), text_color = txt_color, text_size = tbl_sz)
    table.cell(tbl, 2, 10, f_main_label(slowScore), bgcolor = f_main_col(slowScore), text_color = txt_color, text_size = tbl_sz)
 
    int r = 11
    if show_age
        table.cell(tbl, 0, r, "Regime age", bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl, 1, r, age_text, bgcolor = color.rgb(10,10,10), text_color = txt_color, text_size = tbl_sz)
        table.merge_cells(tbl, 1, r, 2, r)
        r += 1
 
    if show_bd
        table.cell(tbl, 0, r, "BACKDROP (30 / 90 DAYS)", bgcolor = color.rgb(12,12,12), text_color = color.rgb(159,179,200), text_halign = text.align_center, text_size = tbl_sz)
        table.cell(tbl, 1, r, "", bgcolor = color.rgb(12,12,12), text_size = tbl_sz)
        table.cell(tbl, 2, r, "", bgcolor = color.rgb(12,12,12), text_size = tbl_sz)
        table.merge_cells(tbl, 0, r, 2, r)
        r += 1
        table.cell(tbl, 0, r, "Stablecoin supply", bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
        if na(bd30p)
            table.cell(tbl, 1, r, "No data yet for a 30-day reading", bgcolor = color.new(color.gray, 60), text_color = txt_color, text_size = tbl_sz)
            table.merge_cells(tbl, 1, r, 2, r)
        else
            table.cell(tbl, 1, r, f_sgn(bd30p) + " (30d) · " + f_sgn(bd90p) + " (90d)", bgcolor = color.rgb(10,10,10), text_color = txt_color, text_size = tbl_sz)
            table.cell(tbl, 2, r, bd_state == 1 ? "Growing" : bd_state == -1 ? "Shrinking" : "Flat", bgcolor = f_cell_col(bd_state, false, false), text_color = txt_color, text_size = tbl_sz)
        r += 1
 
    table.cell(tbl, 0, r, "Counted", bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
    table.cell(tbl, 1, r, "Stables " + str.tostring(stCount) + " of " + str.tostring(array.size(stabs)) + " · ETFs " + str.tostring(etfCount) + " of " + str.tostring(array.size(etfs)), bgcolor = color.rgb(10,10,10), text_color = txt_color, tooltip = (stMiss + etfMiss) == "" ? "Every stablecoin and ETF in your lists has data." : "Not counted: " + stMiss + etfMiss + " Check the ticker in Settings, Markets.", text_size = tbl_sz)
    table.merge_cells(tbl, 1, r, 2, r)
    tblKey := tblLayout
 
//──────────────────────────────
// MTF rollup table
//──────────────────────────────
var table tbl_mtf = table.new(position.top_right, 3, 6, border_width = 1)
// The table is cleared only when its layout changes. Clearing it on every live tick could leave old text showing under the new.
varip string mtfKey = ""
string mtfLayout = "1"
bool mtfBuild = mtfKey != mtfLayout
if barstate.islast
    if mtfBuild
        table.clear(tbl_mtf, 0, 0, 2, 5)
    if show_rollup
        table.cell(tbl_mtf, 0, 0, " ", bgcolor = color.new(color.black, 100), text_color = color.new(color.black, 100), text_size = tbl_sz)
        table.cell(tbl_mtf, 1, 0, " ", bgcolor = color.new(color.black, 100), text_color = color.new(color.black, 100), text_size = tbl_sz)
        table.cell(tbl_mtf, 2, 0, " ", bgcolor = color.new(color.black, 100), text_color = color.new(color.black, 100), text_size = tbl_sz)
        table.cell(tbl_mtf, 0, 1, "TF",        bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 1, 1, "Liquidity", bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 2, 1, "Score",     bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 0, 2, f_tf_label(fast_tf), bgcolor = color.rgb(10,10,10), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 1, 2, f_tf_word(fastScore), bgcolor = f_tf_col(fastScore), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 2, 2, str.tostring(fastScore), bgcolor = color.rgb(10,10,10), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 0, 3, f_tf_label(mid_tf), bgcolor = color.rgb(10,10,10), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 1, 3, f_tf_word(midScore), bgcolor = f_tf_col(midScore), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 2, 3, str.tostring(midScore), bgcolor = color.rgb(10,10,10), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 0, 4, f_tf_label(slow_tf), bgcolor = color.rgb(10,10,10), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 1, 4, f_tf_word(slowScore), bgcolor = f_tf_col(slowScore), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 2, 4, str.tostring(slowScore), bgcolor = color.rgb(10,10,10), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 0, 5, "TOTAL", bgcolor = color.rgb(20,20,20), text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 1, 5, total_label, bgcolor = total_col, text_color = txt_color, text_size = tbl_sz)
        table.cell(tbl_mtf, 2, 5, str.tostring(total) + " / " + str.tostring(maxTotal), bgcolor = total_col, text_color = txt_color, text_size = tbl_sz)
    mtfKey := mtfLayout
 
//──────────────────────────────
// Alerts (all fire once, on the change)
//──────────────────────────────
// Main TF (Daily)
alertcondition(slowScore > 0 and slowScore[1] <= 0,   "Liquidity: turned inflow",   "Rule Sentinel · Crypto Liquidity: main TF score crossed above 0 (tailwind starts)")
alertcondition(slowScore < 0 and slowScore[1] >= 0,   "Liquidity: turned outflow",  "Rule Sentinel · Crypto Liquidity: main TF score crossed below 0 (headwind starts)")
alertcondition(slowScore >= 3 and slowScore[1] < 3,   "Liquidity: strong inflow",   "Rule Sentinel · Crypto Liquidity: main TF score reached +3 (strong tailwind)")
alertcondition(slowScore <= -3 and slowScore[1] > -3, "Liquidity: strong outflow",  "Rule Sentinel · Crypto Liquidity: main TF score reached -3 (strong headwind)")
alertcondition(pf_d_s == 1 and pf_d_s[1] != 1,        "Perp flow: turned up",       "Rule Sentinel · Crypto Liquidity: perp flow turned up (net buying on the perp)")
alertcondition(pf_d_s == -1 and pf_d_s[1] != -1,      "Perp flow: turned down",     "Rule Sentinel · Crypto Liquidity: perp flow turned down (net selling on the perp)")
// MTF TOTAL (the two tailwind alerts need the main TF not to be in outflow)
mtf_tw  = total > 0 and slow_ok
mtf_stw = total >= 20 and slow_ok
alertcondition(mtf_tw and not mtf_tw[1],              "MTF: tailwind",              "Rule Sentinel · Crypto Liquidity: TOTAL turned positive (main TF not in outflow)")
alertcondition(mtf_stw and not mtf_stw[1],            "MTF: strong tailwind",       "Rule Sentinel · Crypto Liquidity: TOTAL reached +20 (main TF not in outflow)")
alertcondition(total < 0 and total[1] >= 0,           "MTF: headwind",              "Rule Sentinel · Crypto Liquidity: TOTAL turned negative")
alertcondition(total <= -20 and total[1] > -20,       "MTF: strong headwind",       "Rule Sentinel · Crypto Liquidity: TOTAL reached -20")
// Backdrop
alertcondition(bd_state == 1 and bd_state[1] != 1,    "Backdrop: stablecoin supply growing", "Rule Sentinel · Crypto Liquidity: total stablecoin supply turned to growth over 30 days")

← 返回列表