CTZ Hash Ribbons Pro

CryptoTradingZone · study · 225 行 · 点赞 17 · TradingView 原页

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

Pine Script

//@version=6
indicator("CTZ Hash Ribbons Pro", "CTZ Hash Ribbons", overlay = true, max_labels_count = 500)
 
// ═══════════════════════════════════════════════════════════════════
// CTZ HASH RIBBONS PRO
// Miner capitulation & recovery from the Bitcoin hash rate
// (30-day vs 60-day SMA), with a price-momentum buy filter.
// Upgrades over the classic version:
//  • Non-repainting: confirmed daily values only (no future leak)
//  • All maths on DAILY data, so signals are the same on any timeframe
//  • Hash rate source with automatic fallbacks (Quandl is retired)
//  • Noise filters: cross threshold % and minimum capitulation length
//  • One clean buy per recovery, with an expiry
//  • Valuation filter: no buys when overbought (Mayer Multiple,
//    distance from 1-year high, RSI) + overheated warning zone
// Everything is drawn on the price chart: background, signals, dashboard.
// ═══════════════════════════════════════════════════════════════════
 
// ═════════ Inputs ═════════
grpD = "Data"
hrSym    = input.string("INTOTHEBLOCK:BTC_HASHRATE", "Hash rate symbol", group = grpD, tooltip = "If the dashboard shows 'no data', search TradingView for 'hash rate' and type the exact symbol here.")
useFb    = input.bool(true, "Auto-fallback (BTC_HASHRATE, QUANDL:BCHAIN/HRATE)", group = grpD)
nonRep   = input.bool(true, "Non-repainting (confirmed daily values)", group = grpD, tooltip = "Uses yesterday's confirmed daily value. Signals appear one day later but never change afterwards.")
 
grpR = "Ribbons"
len_s    = input.int(30, "Short SMA (days)", minval = 2, group = grpR)
len_l    = input.int(60, "Long SMA (days)", minval = 5, group = grpR)
smoothR  = input.int(1, "Pre-smooth raw hash rate (days, 1 = off)", minval = 1, maxval = 14, group = grpR)
 
grpF = "Noise filters"
capTh    = input.float(2.0, "Capitulation start threshold %", minval = 0.0, step = 0.1, group = grpF, tooltip = "Short SMA must be this % BELOW the long SMA to start a capitulation. Deeper = fewer, more meaningful capitulations. 0 = classic.")
hyst     = input.float(0.5, "Recovery threshold %", minval = 0.0, step = 0.1, group = grpF, tooltip = "Short SMA must be this % ABOVE the long SMA to end the capitulation. 0 = classic.")
minCap   = input.int(10, "Confirm capitulation after (days)", minval = 0, group = grpF, tooltip = "A capitulation only counts once it has lasted this long. The start marker prints on the confirmation day; shorter dips are ignored completely (no marker, no recovery, no buy).")
shadePen = input.bool(false, "Lightly shade unconfirmed capitulations", group = grpF)
 
grpB = "Buy signal"
pFast    = input.int(10, "Price fast SMA (days)", minval = 2, group = grpB)
pSlow    = input.int(20, "Price slow SMA (days)", minval = 3, group = grpB)
buyWait  = input.int(60, "Buy must trigger within (days of recovery)", minval = 1, group = grpB)
 
grpO = "Overbought / oversold filter"
useMM    = input.bool(true, "Mayer Multiple filter (price / 200-day SMA)", group = grpO, tooltip = "Mayer Multiple below 1 = undervalued, above 2.4 = historically overheated. Cycle bottoms usually print below about 1.1.")
mmMax    = input.float(1.2, "   Max Mayer Multiple for a buy", minval = 0.5, step = 0.05, group = grpO)
useDD    = input.bool(true, "Drawdown filter (below 1-year high)", group = grpO)
ddMin    = input.float(20.0, "   Min % below 365-day high for a buy", minval = 0.0, step = 1.0, group = grpO)
useRSI   = input.bool(true, "RSI filter (daily RSI 14)", group = grpO)
rsiMax   = input.float(60.0, "   Max daily RSI for a buy", minval = 10.0, maxval = 100.0, step = 1.0, group = grpO)
keepArm  = input.bool(true, "If filtered, keep waiting until the buy window expires", group = grpO)
showFilt = input.bool(true, "Mark filtered (blocked) buys", group = grpO)
showHot  = input.bool(true, "Shade overheated zone (Mayer >= threshold)", group = grpO)
mmHot    = input.float(2.4, "   Overheated Mayer Multiple", minval = 1.0, step = 0.1, group = grpO)
 
grpV = "Display"
dashPos  = input.string("Bottom Left", "Dashboard position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = grpV)
showBg   = input.bool(true, "Shade capitulation on price chart", group = grpV)
showSig  = input.bool(true, "Signals on price chart", group = grpV)
showDash = input.bool(true, "Dashboard", group = grpV)
 
// ═════════ Daily calculations ═════════
int DAY = 86400000
int off = nonRep ? 1 : 0
 
f_hr(int o) =>
    float src  = ta.sma(close, smoothR)
    float s    = ta.sma(src, len_s)
    float l    = ta.sma(src, len_l)
    bool  rise = s > s[1] and s[1] > s[2] and s[2] > s[3]
    [s[o], l[o], src[o], rise[o]]
 
f_px(int o) =>
    float mm = close / ta.sma(close, 200)
    float hi = ta.highest(high, 365)
    float dd = hi > 0 ? (hi - close) / hi * 100 : na
    [ta.sma(close, pFast)[o], ta.sma(close, pSlow)[o], mm[o], dd[o], ta.rsi(close, 14)[o]]
 
la = nonRep ? barmerge.lookahead_on : barmerge.lookahead_off
 
[s1, l1, r1, u1] = request.security(hrSym, "D", f_hr(off), lookahead = la, ignore_invalid_symbol = true)
[s2, l2, r2, u2] = request.security("BTC_HASHRATE", "D", f_hr(off), lookahead = la, ignore_invalid_symbol = true)
[s3, l3, r3, u3] = request.security("QUANDL:BCHAIN/HRATE", "D", f_hr(off), lookahead = la, ignore_invalid_symbol = true)
[pf, ps, mmV, ddV, rsiV] = request.security(syminfo.tickerid, "D", f_px(off), lookahead = la)
 
bool ok1 = not na(s1) and not na(l1)
bool ok2 = useFb and not na(s2) and not na(l2)
bool ok3 = useFb and not na(s3) and not na(l3)
 
float  hrS  = ok1 ? s1 : ok2 ? s2 : ok3 ? s3 : na
float  hrL  = ok1 ? l1 : ok2 ? l2 : ok3 ? l3 : na
float  hrR  = ok1 ? r1 : ok2 ? r2 : ok3 ? r3 : na
bool   hrUp = ok1 ? u1 : ok2 ? u2 : ok3 ? u3 : false
string srcN = ok1 ? hrSym : ok2 ? "BTC_HASHRATE" : ok3 ? "QUANDL:BCHAIN/HRATE" : "no data"
 
float diff = not na(hrS) and not na(hrL) and hrL != 0 ? (hrS - hrL) / hrL * 100 : na
 
// ═════════ State machine ═════════
var bool  inCap    = false
var bool  capConf  = false
var int   capT     = na
var bool  armed    = false
var int   recT     = na
var int   lastBuyT = na
var int   lastCapT = na
 
bool capStart = false   // confirmed capitulation (marker / alert)
bool recovery = false
bool noiseCap = false
bool buy      = false
bool filtered = false
var bool filtShown = false
 
// valuation check (each enabled filter must pass; missing data = pass)
bool mmOk  = not useMM  or na(mmV)  or mmV <= mmMax
bool ddOk  = not useDD  or na(ddV)  or ddV >= ddMin
bool rsiOk = not useRSI or na(rsiV) or rsiV <= rsiMax
bool valOk = mmOk and ddOk and rsiOk
bool hot   = not na(mmV) and mmV >= mmHot
 
if not na(diff)
    if not inCap and diff < -capTh
        // possible capitulation – not confirmed yet
        inCap   := true
        capConf := false
        capT    := time
    else if inCap and diff > hyst
        inCap := false
        if capConf
            recovery  := true
            recT      := time
            armed     := true
            filtShown := false
        else
            noiseCap := true
        capConf := false
    // confirm once it has lasted long enough
    if inCap and not capConf and int((time - capT) / DAY) >= minCap
        capConf  := true
        capStart := true
        lastCapT := capT
        armed    := false
 
bool recovering      = inCap and capConf and hrUp
bool recoveringStart = recovering and not recovering[1]
 
// one buy per recovery: price momentum up (fast > slow) while armed, before expiry
if armed and not na(pf) and not na(ps)
    bool expired = (time - recT) / DAY > buyWait
    if pf > ps and valOk
        buy      := true
        armed    := false
        lastBuyT := time
    else if pf > ps and not valOk
        // momentum is up but the market is overbought: block the buy
        if not filtShown
            filtered  := true
            filtShown := true
        if not keepArm or expired
            armed := false
    else if expired
        armed := false
 
int capDays = inCap and not na(capT) ? int((time - capT) / DAY) : na
 
// ═════════ Colours ═════════
color cCap  = color.new(#9ca3af, 0)
color cRec  = color.new(#22c55e, 0)
color cNorm = color.new(#38bdf8, 0)
 
// hash rate values in the Data Window only (they would wreck the price scale)
plot(hrS, "Hash rate short SMA", display = display.data_window)
plot(hrL, "Hash rate long SMA", display = display.data_window)
plot(diff, "Short vs long %", display = display.data_window)
 
// ═════════ Price chart ═════════
bgcolor(showBg and inCap and capConf ? (recovering ? color.new(cRec, 90) : color.new(cCap, 88)) : showBg and shadePen and inCap ? color.new(cCap, 95) : na, title = "Capitulation background")
bgcolor(showHot and hot and not (inCap and capConf) ? color.new(color.red, 90) : na, title = "Overheated zone")
 
plotshape(showSig and capStart, "Capitulation confirmed", shape.circle, location.belowbar, color.new(cCap, 0), size = size.tiny)
plotshape(showSig and recovery, "Recovery", shape.triangleup, location.belowbar, color.new(cRec, 0), size = size.tiny)
plotshape(showSig and showFilt and filtered, "Filtered buy (overbought)", shape.xcross, location.belowbar, color.new(color.orange, 0), text = "filtered", textcolor = color.orange, size = size.tiny)
plotshape(showSig and buy, "Hash Ribbon BUY", shape.labelup, location.belowbar, color.new(#2563eb, 0), text = "HASH\nBUY", textcolor = color.white, size = size.small)
 
// ═════════ Dashboard ═════════
f_d(int t) => na(t) ? "–" : str.format_time(t, "dd MMM yyyy", "UTC")
 
f_pos(string p) =>
    switch p
        "Top Right"    => position.top_right
        "Top Left"     => position.top_left
        "Bottom Right" => position.bottom_right
        => position.bottom_left
 
var table dash = table.new(f_pos(dashPos), 2, 10, bgcolor = color.new(color.black, 20), border_width = 1, border_color = color.new(color.white, 80))
if showDash and barstate.islast
    color lc = color.new(color.white, 25)
    string st = na(diff) ? "NO DATA" : recovering ? "RECOVERING (day " + str.tostring(capDays) + ")" : inCap and capConf ? "CAPITULATION (day " + str.tostring(capDays) + ")" : inCap ? "POSSIBLE CAPITULATION (day " + str.tostring(capDays) + " of " + str.tostring(minCap) + ")" : armed ? "RECOVERED – waiting for momentum" : "NORMAL"
    color  sc = na(diff) ? color.orange : recovering ? cRec : inCap and capConf ? cCap : inCap ? color.new(cCap, 40) : armed ? color.yellow : cNorm
    table.cell(dash, 0, 0, "CTZ HASH RIBBONS", text_color = color.white, bgcolor = color.new(#1e293b, 0), text_size = size.small)
    table.cell(dash, 1, 0, nonRep ? "confirmed daily" : "live daily", text_color = color.new(color.white, 40), bgcolor = color.new(#1e293b, 0), text_size = size.small)
    table.cell(dash, 0, 1, "Source", text_color = lc, text_size = size.small)
    table.cell(dash, 1, 1, srcN, text_color = srcN == "no data" ? color.orange : color.white, text_size = size.small)
    table.cell(dash, 0, 2, "Status", text_color = lc, text_size = size.small)
    table.cell(dash, 1, 2, st, text_color = sc, text_size = size.small)
    table.cell(dash, 0, 3, str.tostring(len_s) + "d vs " + str.tostring(len_l) + "d", text_color = lc, text_size = size.small)
    table.cell(dash, 1, 3, na(diff) ? "–" : (diff >= 0 ? "+" : "") + str.tostring(diff, "0.00") + "%", text_color = na(diff) ? lc : diff >= 0 ? cNorm : cCap, text_size = size.small)
    table.cell(dash, 0, 4, "Price momentum", text_color = lc, text_size = size.small)
    table.cell(dash, 1, 4, na(pf) ? "–" : pf > ps ? "UP (" + str.tostring(pFast) + " > " + str.tostring(pSlow) + ")" : "DOWN", text_color = not na(pf) and pf > ps ? cRec : color.red, text_size = size.small)
    table.cell(dash, 0, 5, "Last capitulation", text_color = lc, text_size = size.small)
    table.cell(dash, 1, 5, f_d(lastCapT), text_color = color.white, text_size = size.small)
    table.cell(dash, 0, 6, "Last recovery", text_color = lc, text_size = size.small)
    table.cell(dash, 1, 6, f_d(recT), text_color = cRec, text_size = size.small)
    table.cell(dash, 0, 7, "Last buy", text_color = lc, text_size = size.small)
    table.cell(dash, 1, 7, f_d(lastBuyT), text_color = color.new(#60a5fa, 0), text_size = size.small)
    string valTxt = "Mayer " + (na(mmV) ? "–" : str.tostring(mmV, "0.00")) + " · DD " + (na(ddV) ? "–" : str.tostring(ddV, "0") + "%") + " · RSI " + (na(rsiV) ? "–" : str.tostring(rsiV, "0"))
    table.cell(dash, 0, 8, "Valuation", text_color = lc, text_size = size.small)
    table.cell(dash, 1, 8, valTxt, text_color = color.white, text_size = size.small)
    table.cell(dash, 0, 9, "Buy filter", text_color = lc, text_size = size.small)
    table.cell(dash, 1, 9, hot ? "OVERHEATED – no buys" : valOk ? "PASS – buys allowed" : "BLOCKED – overbought", text_color = hot ? color.red : valOk ? cRec : color.orange, text_size = size.small)
 
// ═════════ Alerts ═════════
alertcondition(capStart, "Miner capitulation confirmed", "⚠️ Hash Ribbons: miner capitulation confirmed — {{ticker}}")
alertcondition(recoveringStart, "Hash rate recovering", "Hash Ribbons: hash rate recovering inside capitulation — {{ticker}}")
alertcondition(recovery, "Miner capitulation ended (recovery)", "Hash Ribbons: capitulation over, hash rate recovered — {{ticker}}")
alertcondition(filtered, "Hash Ribbon buy filtered", "Hash Ribbons: buy blocked by overbought filter — {{ticker}}")
alertcondition(hot and not hot[1], "Overheated zone entered", "Hash Ribbons: Mayer Multiple in overheated zone — {{ticker}}")
alertcondition(buy, "Hash Ribbon BUY", "✅ Hash Ribbons BUY signal — {{ticker}} @ {{close}}")

← 返回列表