Skip to content

Instantly share code, notes, and snippets.

@discountry
Created November 5, 2025 03:30
Show Gist options
  • Select an option

  • Save discountry/afb5f313f8cd0c383bfc5d1c50d9c934 to your computer and use it in GitHub Desktop.

Select an option

Save discountry/afb5f313f8cd0c383bfc5d1c50d9c934 to your computer and use it in GitHub Desktop.
//@version=6
indicator("BTC-ZEC RSI Div", overlay=true)
// === Inputs ===
symA = input.symbol("BITSTAMP:BTCUSD", "Symbol A")
symB = input.symbol("BITFINEX:ZECUSD", "Symbol B")
res = input.timeframe("", "Resolution (empty = chart)")
rsiLen = input.int(14, "RSI Length", minval=2)
len = input.int(20, "Z-Score Length", minval=10)
smooth = input.int(5, "EMA Smoothing", minval=1)
capZ = input.float(3.0, "Max Color Z", minval=0.5, step=0.5)
threshold = input.float(1.5, "Threshold |z|", minval=0.1, step=0.1)
// === Data (fill gaps to extend back in history) ===
useRes = (res == "") ? timeframe.period : res
cA = request.security(symA, useRes, close, gaps=barmerge.gaps_off)
cB = request.security(symB, useRes, close, gaps=barmerge.gaps_off)
// Validity guard
valid = not na(cA) and not na(cB)
// === RSI for both symbols ===
rsiA = valid ? ta.rsi(cA, rsiLen) : na
rsiB = valid ? ta.rsi(cB, rsiLen) : na
// === Divergence on RSI (z-score of RSI spread) ===
spread = rsiA - rsiB
spreadSm = ta.ema(spread, smooth)
sprStdev = ta.stdev(spreadSm, len)
zDiff = sprStdev != 0 ? (spreadSm - ta.sma(spreadSm, len)) / sprStdev : na
mag = math.abs(zDiff)
// === Thresholding & color depth mapping ===
overThreshold = mag > threshold
den = math.max(capZ - threshold, 0.000001)
norm = math.min((mag - threshold) / den, 1.0)
transp = 100 - math.round(norm * 100.0)
// Color: positive=lime, negative=red
col = zDiff >= 0 ? color.new(color.lime, transp) : color.new(color.red, transp)
showCondition = overThreshold and zDiff < 0
// === Background mark only when exceeding threshold ===
bgcolor(showCondition ? col : na, title="RSI Divergence Heat")
// === Optional alert (kept minimal) ===
alertcondition(overThreshold, title="RSI Divergence Exceeds Threshold",
message="BTC-ZEC RSI divergence exceeds threshold on {{ticker}} @ {{interval}}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment