Rule Sentinel Crypto Flow

RuleSentinel · study · 378 行 · 点赞 0 · 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 Flow", shorttitle="RS Crypto Flow", overlay=false, format=format.price, precision=3)
 
//──────────────────────────────
// Inputs
//──────────────────────────────
grpD        = "Display"
show_4h     = input.bool(true, "Show 4H column", group = grpD, display = display.none, tooltip = "The 4H column next to the 1D column: the fast view of the same rows.")
show_rot    = input.bool(true, "Show rotation", group = grpD, display = display.none, tooltip = "BTC, ETH, large alts and small caps, and the market leading the move (safest to riskiest).")
show_age    = input.bool(true, "Show regime age", group = grpD, display = display.none, tooltip = "How long the Daily Driver read has held, counted from the Daily close at which it last changed.")
show_pct    = input.bool(true, "Show the change in each cell", group = grpD, display = display.none, tooltip = "The move over the lookback, as a % of the market's own value.")
show_hist   = input.bool(true, "Show score history (Coinbase premium)", group = grpD, display = display.none, tooltip = "Columns of the Daily Coinbase premium over time: green above the flat band (US spot buyers paying up), red below it, orange inside.")
hist_smooth = input.int(3, "History smoothing (days)", minval = 1, maxval = 50, group = grpD, display = display.none, tooltip = "Average of the Daily premium over this many days, worked out on the Daily candles, so the history is the same on any chart. 1 = the raw Daily value.")
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 the table. Normal is the standard size. A larger size makes the table taller: if it reaches 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
 
grpA      = "⚠ Advanced - do not change"
spot_sym  = input.string("BINANCE:BTCUSDT",     "BTC spot",                    group = grpA, display = display.none)
perp_sym  = input.string("BINANCE:BTCUSDT.P",   "BTC perp",                    group = grpA, display = display.none)
oi_sym    = input.string("BINANCE:BTCUSDT.P_OI", "BTC perp open interest",     group = grpA, display = display.none)
fr_sym    = input.string("BINANCE:BTCUSDT.P_FR", "BTC perp funding rate",      group = grpA, display = display.none)
cb_sym    = input.string("COINBASE:BTCUSD",     "BTC on Coinbase (US spot)",   group = grpA, display = display.none)
usdt_sym  = input.string("COINBASE:USDTUSD",    "USDT to USD",                 group = grpA, display = display.none)
btc_cap   = input.string("CRYPTOCAP:BTC",       "Rotation 1: BTC market cap",      group = grpA, display = display.none)
eth_cap   = input.string("CRYPTOCAP:ETH",       "Rotation 2: ETH market cap",      group = grpA, display = display.none)
t3_cap    = input.string("CRYPTOCAP:TOTAL3",    "Rotation 3: large alts (TOTAL3)", group = grpA, display = display.none)
oth_cap   = input.string("CRYPTOCAP:OTHERS",    "Rotation 4: small caps (OTHERS)", group = grpA, display = display.none)
fast_tf   = input.timeframe("240", "Fast TF (4H)", group = grpA, display = display.none)
slow_tf   = input.timeframe("D",   "Main TF (1D)", group = grpA, display = display.none)
lookback  = input.int(10, "Lookback candles for UP / DOWN", minval = 2, maxval = 100, group = grpA, display = display.none)
ema_len   = input.int(50, "Average length for the trend", minval = 5, maxval = 200, group = grpA, display = display.none)
trend_len = input.int(200, "BTC long-term average (days)", minval = 50, maxval = 400, group = grpA, display = display.none)
avg_n     = input.int(3, "Funding and premium: average of the last N candles", minval = 1, maxval = 20, group = grpA, display = display.none)
fr_hot    = input.float(0.03, "Funding hot at (% per 8 hours)", step = 0.005, group = grpA, display = display.none)
fr_neg    = input.float(0.0, "Funding negative below (% per 8 hours)", step = 0.005, group = grpA, display = display.none)
prem_band = input.float(0.01, "Coinbase premium flat band (±%)", minval = 0.0, step = 0.005, group = grpA, display = display.none)
 
//──────────────────────────────
// 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
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 ----
f_lvl() =>
    float e = ta.ema(close, ema_len)
    [close, close[lookback], e]
 
// BTC spot on the Daily also returns its long-term average
f_spot_d() =>
    float e = ta.ema(close, ema_len)
    [close, close[lookback], e, ta.ema(close, trend_len)]
 
// Funding feed: average of the last avg_n candles. The feed is a fraction per funding period (0.0001), shown as % (0.01%)
f_fr() =>
    ta.sma(close, avg_n) * 100.0
 
// Funding estimate when the feed is missing: (perp - spot) / spot / 3, a common 8-hour stand-in, averaged
f_basis(tf) =>
    float s = request.security(spot_sym, tf, close, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
    float b = na(s) or s == 0.0 ? na : (close / s - 1.0) * 100.0 / 3.0
    ta.sma(b, avg_n)
 
// Coinbase premium: Coinbase BTC-USD against Binance BTC-USDT (converted to USD), in %, averaged
f_prem(tf) =>
    float s = request.security(spot_sym, tf, close, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
    float u = request.security(usdt_sym, tf, close, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
    float uu = na(u) or u == 0.0 ? 1.0 : u
    float p = na(s) or s == 0.0 ? na : (close / (s * uu) - 1.0) * 100.0
    [ta.sma(p, avg_n), na(u) ? 0 : 1]
// The fast column needs the premium only
f_prem_f(tf) =>
    [a, ok] = f_prem(tf)
    a
// The main column also returns the score history: the premium averaged over hist_smooth Daily candles
f_prem_d(tf) =>
    [a, ok] = f_prem(tf)
    [a, ta.sma(a, hist_smooth), ok]
 
f_req(sym, tf) =>
    request.security(sym, tf, f_lvl(), 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
//──────────────────────────────
// Main TF (1D)
[sp_d, spl_d, spe_d, sp200] = request.security(spot_sym, slow_tf, f_spot_d(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
[oi_d, oil_d, oie_d] = f_req(oi_sym, slow_tf)
fr_d = request.security(fr_sym,   slow_tf, f_fr(),           gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
bs_d = request.security(perp_sym, slow_tf, f_basis(slow_tf), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
[pr_d, prh_d, uok] = request.security(cb_sym, slow_tf, f_prem_d(slow_tf), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
[b1_d, b1l_d, b1e_d] = f_req(btc_cap, slow_tf)
[e1_d, e1l_d, e1e_d] = f_req(eth_cap, slow_tf)
[t3_d, t3l_d, t3e_d] = f_req(t3_cap,  slow_tf)
[ot_d, otl_d, ote_d] = f_req(oth_cap, slow_tf)
// Fast TF (4H)
[sp_f, spl_f, spe_f] = f_req(spot_sym, fast_tf)
[oi_f, oil_f, oie_f] = f_req(oi_sym, fast_tf)
fr_f = request.security(fr_sym,   fast_tf, f_fr(),           gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
bs_f = request.security(perp_sym, fast_tf, f_basis(fast_tf), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
pr_f = request.security(cb_sym,   fast_tf, f_prem_f(fast_tf), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
[b1_f, b1l_f, b1e_f] = f_req(btc_cap, fast_tf)
[e1_f, e1l_f, e1e_f] = f_req(eth_cap, fast_tf)
[t3_f, t3l_f, t3e_f] = f_req(t3_cap,  fast_tf)
[ot_f, otl_f, ote_f] = f_req(oth_cap, fast_tf)
 
// Funding: the feed when it exists, otherwise the estimate
fr_feed = not na(fr_d)
fu_d  = fr_feed ? fr_d  : bs_d
fu_f  = fr_feed ? fr_f  : bs_f
 
//──────────────────────────────
// Reads
//──────────────────────────────
f_prem_state(p) =>
    na(p) ? 0 : p > prem_band ? 1 : p < -prem_band ? -1 : 0
f_fund_state(v) =>
    na(v) ? 0 : v >= fr_hot ? 1 : v < fr_neg ? -1 : 0
 
// Driver: price, open interest and the Coinbase premium read together
// 1 Spot-led rally, 2 Rally, spot and leverage, 3 Leverage-led rally, 4 Short squeeze, 5 Rally, no clear driver,
// 6 New shorts building, 7 Deleveraging, 8 Decline, no clear driver, 0 No driver (range)
f_driver(pd, od, ps) =>
    int c = 0
    if pd == 1
        c := od == 1 ? (ps == 1 ? 2 : 3) : od == -1 ? (ps == 1 ? 1 : 4) : (ps == 1 ? 1 : 5)
    else if pd == -1
        c := od == 1 ? 6 : od == -1 ? 7 : 8
    c
f_driver_name(c) =>
    c == 1 ? "Spot-led rally" : c == 2 ? "Rally, spot and leverage" : c == 3 ? "Leverage-led rally" : c == 4 ? "Short squeeze" : c == 5 ? "Rally, no clear driver" : c == 6 ? "New shorts building" : c == 7 ? "Deleveraging" : c == 8 ? "Decline, no clear driver" : "No driver (range)"
amber = color.rgb(201, 138, 27)
f_driver_col(c) =>
    c == 1 or c == 2 ? color.new(color.green, 10) : c == 3 or c == 4 or c == 7 ? amber : c == 6 or c == 8 ? color.new(color.red, 10) : color.new(color.orange, 10)
 
pd_d = f_dir(sp_d, spl_d, spe_d)
od_d = f_dir(oi_d, oil_d, oie_d)
ps_d = f_prem_state(pr_d)
drv_d = f_driver(pd_d, od_d, ps_d)
pd_f = f_dir(sp_f, spl_f, spe_f)
od_f = f_dir(oi_f, oil_f, oie_f)
ps_f = f_prem_state(pr_f)
drv_f = f_driver(pd_f, od_f, ps_f)
fs_d = f_fund_state(fu_d)
fs_f = f_fund_state(fu_f)
 
// Rotation: the market with the biggest move
f_leader(a, b, c, d) =>
    int  li = -1
    float lv = na
    if not na(a) and (na(lv) or a > lv)
        li := 0
        lv := a
    if not na(b) and (na(lv) or b > lv)
        li := 1
        lv := b
    if not na(c) and (na(lv) or c > lv)
        li := 2
        lv := c
    if not na(d) and (na(lv) or d > lv)
        li := 3
        lv := d
    [li, lv]
f_market(i) =>
    i == 0 ? "BTC" : i == 1 ? "ETH" : i == 2 ? "Large alts" : i == 3 ? "Small caps" : "n/a"
[ld_d, lv_d] = f_leader(f_pct(b1_d, b1l_d), f_pct(e1_d, e1l_d), f_pct(t3_d, t3l_d), f_pct(ot_d, otl_d))
[ld_f, lv_f] = f_leader(f_pct(b1_f, b1l_f), f_pct(e1_f, e1l_f), f_pct(t3_f, t3l_f), f_pct(ot_f, otl_f))
f_leader_txt(i, v) =>
    f_market(i) + (not na(v) and v < 0 ? " (falling least)" : "")
f_leader_col(i) =>
    i == 3 ? amber : color.rgb(10, 10, 10)
 
// BTC long-term trend (Daily)
trend_dist = f_pct(sp_d, sp200)
trend_up   = not na(trend_dist) and trend_dist > 0
 
//──────────────────────────────
// Cell text and colours
//──────────────────────────────
f_cell_col(d, isna) =>
    isna ? color.new(color.gray, 60) : d == 1 ? color.new(color.green, 10) : d == -1 ? color.new(color.red, 10) : color.new(color.orange, 10)
ctx_col = color.rgb(61, 90, 128)
f_word(d) =>
    d == 1 ? "UP" : d == -1 ? "DOWN" : "STABLE"
f_txt(d, x, xlb) =>
    string out = f_word(d)
    if na(x)
        out := "n/a"
    else if show_pct
        float p = f_pct(x, xlb)
        out := na(p) ? out : out + " " + (p >= 0 ? "+" : "") + str.tostring(p, "0.0") + "%"
    out
f_sgn3(x) =>
    (x >= 0 ? "+" : "") + str.tostring(x, "0.000") + "%"
f_fund_txt(v) =>
    na(v) ? "n/a" : f_sgn3(v) + " " + (f_fund_state(v) == 1 ? "Hot" : f_fund_state(v) == -1 ? "Negative" : "Normal")
f_fund_col(v) =>
    na(v) ? color.new(color.gray, 60) : f_fund_state(v) == 1 ? color.new(color.red, 10) : f_fund_state(v) == -1 ? amber : color.new(color.green, 10)
f_prem_txt(p) =>
    na(p) ? "n/a" : f_sgn3(p) + " " + (f_prem_state(p) == 1 ? "Positive" : f_prem_state(p) == -1 ? "Negative" : "Flat")
f_prem_col(p) =>
    na(p) ? color.new(color.gray, 60) : f_prem_state(p) == 1 ? color.new(color.green, 10) : f_prem_state(p) == -1 ? color.new(color.red, 10) : color.new(color.orange, 10)
 
//──────────────────────────────
// Score history: the Daily Coinbase premium, smoothed on the Daily candles (the same on any chart)
//──────────────────────────────
hist_val = prh_d
hist_col = na(hist_val) ? color(na) : hist_val > prem_band ? color.new(color.green, 15) : hist_val < -prem_band ? color.new(color.red, 15) : color.new(color.orange, 40)
plot(show_hist ? hist_val : na, "Coinbase premium history (%)", color = hist_col, style = plot.style_columns, linewidth = 1, display = display.all - display.status_line)
plot(show_hist ? 0.3 : na, "Scale top", color = color.new(color.black, 100), display = display.pane, editable = false)
plot(show_hist ? -0.1 : 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)
 
//──────────────────────────────
// Regime age: how long the Daily Driver read has held. Worked out on the Daily candles themselves, inside one data
// request (price, open interest and premium read together there), so it is the same on every chart.
//──────────────────────────────
f_drv_hist(tf) =>
    int off = time_close <= timenow ? 0 : 1
    float s  = request.security(spot_sym, tf, close, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
    float u  = request.security(usdt_sym, tf, close, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
    float oi = request.security(oi_sym,   tf, close, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
    float uu = na(u) or u == 0.0 ? 1.0 : u
    float p  = na(s) or s == 0.0 ? na : (close / (s * uu) - 1.0) * 100.0
    float pa = ta.sma(p, avg_n)
    int pdh = f_dir(s, s[lookback], ta.ema(s, ema_len))
    int odh = f_dir(oi, oi[lookback], ta.ema(oi, ema_len))
    int dv  = f_driver(pdh, odh, f_prem_state(pa))
    chg = ta.valuewhen(dv != dv[1], time_close, 0)
    [dv[off], chg[off]]
[drv_h, drv_chg] = request.security(cb_sym, slow_tf, f_drv_hist(slow_tf), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
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)
// The age row names the read at the last Daily close and how long it has held; when today's forming read (the Driver
// row) differs, it adds it, so the two rows never disagree without saying why.
age_base  = "1D driver: " + f_driver_name(drv_h) + (na(drv_chg) ? "" : " for " + f_age_text(math.max(0, nowT - drv_chg)))
age_today = drv_d != drv_h ? " · today so far: " + f_driver_name(drv_d) : ""
age_text  = na(sp_d) or na(drv_h) ? "No data yet" : timeframe.in_seconds() > timeframe.in_seconds(slow_tf) ? "Shown on 1D charts and lower" : age_base + age_today
 
//──────────────────────────────
// Counted
//──────────────────────────────
f_has(x) =>
    na(x) ? 0 : 1
f_miss(x, name) =>
    na(x) ? name + "  " : ""
// Coinbase BTC counts when its request returns data; USDT/USD is read inside it, so it is missing too when Coinbase is
cb_ok  = not na(uok)
usd_ok = cb_ok and uok == 1
n_have = f_has(sp_d) + f_has(bs_d) + f_has(oi_d) + f_has(fu_d) + (cb_ok ? 1 : 0) + (usd_ok ? 1 : 0) + f_has(b1_d) + f_has(e1_d) + f_has(t3_d) + f_has(ot_d)
miss   = f_miss(sp_d, "BTC spot") + f_miss(bs_d, "BTC perp") + f_miss(oi_d, "Open interest") + f_miss(fu_d, "Funding") + (cb_ok ? "" : "Coinbase BTC  USDT/USD (read with Coinbase BTC)  ") + (cb_ok and not usd_ok ? "USDT/USD (premium uses 1:1)  " : "") + f_miss(b1_d, "BTC cap") + f_miss(e1_d, "ETH cap") + f_miss(t3_d, "TOTAL3") + f_miss(ot_d, "OTHERS")
fund_src = fr_feed ? "funding from the Binance feed" : "funding estimated from perp vs spot"
 
//──────────────────────────────
// Table
//──────────────────────────────
var table tbl = table.new(position.top_left, 4, 22, 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 = "1"
bool tblBuild = tblKey != tblLayout
c4 = 1
cD = show_4h ? 2 : 1
cR = show_4h ? 3 : 2
dark  = color.rgb(10, 10, 10)
head  = color.rgb(20, 20, 20)
sec_c = color.rgb(159, 179, 200)
 
f_span(r, txt, bg, tc, al) =>
    table.cell(tbl, 0, r, txt, bgcolor = bg, text_color = tc, text_halign = al, text_size = tbl_sz)
    for c = 1 to cR
        table.cell(tbl, c, r, "", bgcolor = bg, text_size = tbl_sz)
    table.merge_cells(tbl, 0, r, cR, r)
 
f_label(r, item) =>
    table.cell(tbl, 0, r, item, bgcolor = head, text_color = txt_color, text_halign = text.align_left, text_size = tbl_sz)
f_note(r, note) =>
    table.cell(tbl, cR, r, note, bgcolor = dark, text_color = txt_color, text_halign = text.align_left, text_size = tbl_sz)
f_two(r, item, tf_txt, tf_col, d_txt, d_col, note) =>
    f_label(r, item)
    if show_4h
        table.cell(tbl, c4, r, tf_txt, bgcolor = tf_col, text_color = txt_color, text_size = tbl_sz)
    table.cell(tbl, cD, r, d_txt, bgcolor = d_col, text_color = txt_color, text_size = tbl_sz)
    f_note(r, note)
f_merged(r, item, txt, bg, note, tip) =>
    f_label(r, item)
    table.cell(tbl, 1, r, txt, bgcolor = bg, text_color = txt_color, tooltip = tip, text_size = tbl_sz)
    if show_4h
        table.cell(tbl, 2, r, "", bgcolor = bg, text_size = tbl_sz)
        table.merge_cells(tbl, 1, r, 2, r)
    f_note(r, note)
 
if barstate.islast
    if tblBuild
        table.clear(tbl, 0, 0, 3, 21)
    for c = 0 to cR
        table.cell(tbl, c, 0, " ", bgcolor = color.new(color.black, 100), text_color = color.new(color.black, 100), text_size = tbl_sz)
    f_span(1, "RS · Crypto Flow", color.rgb(12, 12, 12), color.white, text.align_center)
    table.cell(tbl, 0, 2, "Measure", bgcolor = head, text_color = txt_color, text_size = tbl_sz)
    if show_4h
        table.cell(tbl, c4, 2, f_tf_label(fast_tf) + " (fast)", bgcolor = head, text_color = txt_color, text_size = tbl_sz)
    table.cell(tbl, cD, 2, f_tf_label(slow_tf) + " (main)", bgcolor = head, text_color = txt_color, text_size = tbl_sz)
    table.cell(tbl, cR, 2, show_4h ? "What it means (both columns)" : "What it means", bgcolor = head, text_color = txt_color, text_size = tbl_sz)
 
    f_span(3, "POSITIONING · who is moving BTC", color.rgb(15, 15, 15), sec_c, text.align_left)
    f_two(4, "Open interest", f_txt(od_f, oi_f, oil_f), na(oi_f) ? color.new(color.gray, 60) : ctx_col, f_txt(od_d, oi_d, oil_d), na(oi_d) ? color.new(color.gray, 60) : ctx_col, "New leveraged money (context)")
    f_two(5, fr_feed ? "Funding" : "Funding (est.)", f_fund_txt(fu_f), f_fund_col(fu_f), f_fund_txt(fu_d), f_fund_col(fu_d), "Hot = longs crowded, squeeze risk")
    f_two(6, "Coinbase premium", f_prem_txt(pr_f), f_prem_col(pr_f), f_prem_txt(pr_d), f_prem_col(pr_d), "US spot buyers paying up")
    f_two(7, "Driver", f_driver_name(drv_f), f_driver_col(drv_f), f_driver_name(drv_d), f_driver_col(drv_d), "Price + OI + premium together")
 
    int r = 8
    if show_rot
        f_span(r, "ROTATION · safest to riskiest", color.rgb(15, 15, 15), sec_c, text.align_left)
        r += 1
        f_two(r, "BTC", f_txt(f_dir(b1_f, b1l_f, b1e_f), b1_f, b1l_f), f_cell_col(f_dir(b1_f, b1l_f, b1e_f), na(b1_f)), f_txt(f_dir(b1_d, b1l_d, b1e_d), b1_d, b1l_d), f_cell_col(f_dir(b1_d, b1l_d, b1e_d), na(b1_d)), "Safest: money goes here first")
        r += 1
        f_two(r, "ETH", f_txt(f_dir(e1_f, e1l_f, e1e_f), e1_f, e1l_f), f_cell_col(f_dir(e1_f, e1l_f, e1e_f), na(e1_f)), f_txt(f_dir(e1_d, e1l_d, e1e_d), e1_d, e1l_d), f_cell_col(f_dir(e1_d, e1l_d, e1e_d), na(e1_d)), "Next step out on risk")
        r += 1
        f_two(r, "Large alts", f_txt(f_dir(t3_f, t3l_f, t3e_f), t3_f, t3l_f), f_cell_col(f_dir(t3_f, t3l_f, t3e_f), na(t3_f)), f_txt(f_dir(t3_d, t3l_d, t3e_d), t3_d, t3l_d), f_cell_col(f_dir(t3_d, t3l_d, t3e_d), na(t3_d)), "Top alts (TOTAL3: all but BTC and ETH)")
        r += 1
        f_two(r, "Small caps", f_txt(f_dir(ot_f, otl_f, ote_f), ot_f, otl_f), f_cell_col(f_dir(ot_f, otl_f, ote_f), na(ot_f)), f_txt(f_dir(ot_d, otl_d, ote_d), ot_d, otl_d), f_cell_col(f_dir(ot_d, otl_d, ote_d), na(ot_d)), "Riskiest (OTHERS: outside the top 10)")
        r += 1
        f_two(r, "Money leading", f_leader_txt(ld_f, lv_f), f_leader_col(ld_f), f_leader_txt(ld_d, lv_d), f_leader_col(ld_d), "Strongest of the four; small caps = hot market")
        r += 1
 
    f_span(r, "CONTEXT", color.rgb(15, 15, 15), sec_c, text.align_left)
    r += 1
    t_txt = na(trend_dist) ? "n/a" : (trend_up ? "Above" : "Below") + " · " + (trend_dist >= 0 ? "+" : "") + str.tostring(trend_dist, "0.0") + "% from the " + str.tostring(trend_len) + "-day average"
    f_merged(r, "BTC " + str.tostring(trend_len) + "-day trend", t_txt, na(trend_dist) ? color.new(color.gray, 60) : trend_up ? color.new(color.green, 10) : color.new(color.red, 10), "The long-term filter, Daily only", "")
    r += 1
    if show_age
        table.cell(tbl, 0, r, "Regime age", bgcolor = head, text_color = txt_color, text_halign = text.align_left, text_size = tbl_sz)
        table.cell(tbl, 1, r, age_text, bgcolor = dark, text_color = txt_color, text_size = tbl_sz)
        for c = 2 to cR
            table.cell(tbl, c, r, "", bgcolor = dark, text_size = tbl_sz)
        table.merge_cells(tbl, 1, r, cR, r)
        r += 1
    table.cell(tbl, 0, r, "Counted", bgcolor = head, text_color = txt_color, text_halign = text.align_left, text_size = tbl_sz)
    table.cell(tbl, 1, r, str.tostring(n_have) + " of 10 · " + fund_src, bgcolor = dark, text_color = txt_color, tooltip = miss == "" ? "Every market has data." : "No data: " + miss, text_size = tbl_sz)
    for c = 2 to cR
        table.cell(tbl, c, r, "", bgcolor = dark, text_size = tbl_sz)
    table.merge_cells(tbl, 1, r, cR, r)
    tblKey := tblLayout
 
//──────────────────────────────
// Alerts (all fire once, on the change; Daily reads)
//──────────────────────────────
alertcondition(drv_d == 1 and drv_d[1] != 1,   "Driver: spot-led rally",      "Rule Sentinel · Crypto Flow: Daily driver turned to a spot-led rally")
alertcondition(drv_d == 3 and drv_d[1] != 3,   "Driver: leverage-led rally",  "Rule Sentinel · Crypto Flow: Daily driver turned to a leverage-led rally")
alertcondition(drv_d == 7 and drv_d[1] != 7,   "Driver: deleveraging",        "Rule Sentinel · Crypto Flow: Daily driver turned to deleveraging")
alertcondition(drv_d == 6 and drv_d[1] != 6,   "Driver: new shorts building", "Rule Sentinel · Crypto Flow: Daily driver turned to new shorts building")
alertcondition(ps_d == 1 and ps_d[1] != 1,     "Coinbase premium: turned positive", "Rule Sentinel · Crypto Flow: Daily Coinbase premium turned positive (US spot buyers paying up)")
alertcondition(ps_d == -1 and ps_d[1] != -1,   "Coinbase premium: turned negative", "Rule Sentinel · Crypto Flow: Daily Coinbase premium turned negative")
alertcondition(fs_d == 1 and fs_d[1] != 1,     "Funding: turned hot",          "Rule Sentinel · Crypto Flow: Daily funding turned hot (longs crowded)")
alertcondition(ld_d == 3 and ld_d[1] != 3 and ld_d[1] != -1, "Rotation: small caps leading", "Rule Sentinel · Crypto Flow: small caps lead the Daily rotation ladder")
alertcondition(ld_d == 0 and ld_d[1] != 0 and ld_d[1] != -1, "Rotation: BTC leading",        "Rule Sentinel · Crypto Flow: BTC leads the Daily rotation ladder")
alertcondition(trend_up and not trend_up[1] and not na(trend_dist[1]), "BTC: reclaimed the 200-day average", "Rule Sentinel · Crypto Flow: BTC closed above its long-term average")
alertcondition(not trend_up and trend_up[1] and not na(trend_dist), "BTC: lost the 200-day average", "Rule Sentinel · Crypto Flow: BTC fell below its long-term average")

← 返回列表