OPEN-SOURCE SCRIPT

🕴 Rajnikanth Divergence Tool

64
//version=5
indicator("🕴 Rajnikanth Divergence Tool", overlay=true)

// === INPUTS ===
useRSI = input.bool(true, "Enable RSI Divergence")
useMACD = input.bool(true, "Enable MACD Divergence")

rsiSource = input.source(close, "RSI Source")
rsiLength = input.int(14, "RSI Length")

macdSrc = input.source(close, "MACD Source")
macdFast = input.int(12, "MACD Fast Length")
macdSlow = input.int(26, "MACD Slow Length")
macdSig = input.int(9, "MACD Signal Smoothing")

// === INDICATOR CALCULATIONS ===
rsi = ta.rsi(rsiSource, rsiLength)
[macdLine, signalLine, _] = ta.macd(macdSrc, macdFast, macdSlow, macdSig)
macdHist = macdLine - signalLine

// === STORAGE ===
var float lastLowPrice = na
var float lastLowRSI = na
var float lastLowMACD = na

var float lastHighPrice = na
var float lastHighRSI = na
var float lastHighMACD = na

bullDivRSI = false
bearDivRSI = false
revBullDivRSI = false
revBearDivRSI = false

bullDivMACD = false
bearDivMACD = false
revBullDivMACD = false
revBearDivMACD = false

// === LINE STORAGE ===
var line[] lines = array.new_line()

// === DETECTION ===
// Detect Bottom (for bullish)
if ta.lowestbars(low, 3) == 1
if useRSI and not na(lastLowRSI)
bullDivRSI := (low < lastLowPrice) and (rsi > lastLowRSI)
revBullDivRSI := (low > lastLowPrice) and (rsi < lastLowRSI)
if useMACD and not na(lastLowMACD)
bullDivMACD := (low < lastLowPrice) and (macdHist > lastLowMACD)
revBullDivMACD := (low > lastLowPrice) and (macdHist < lastLowMACD)
lastLowPrice := low
lastLowRSI := rsi
lastLowMACD := macdHist

// Detect Top (for bearish)
if ta.highestbars(high, 3) == 1
if useRSI and not na(lastHighRSI)
bearDivRSI := (high > lastHighPrice) and (rsi < lastHighRSI)
revBearDivRSI := (high < lastHighPrice) and (rsi > lastHighRSI)
if useMACD and not na(lastHighMACD)
bearDivMACD := (high > lastHighPrice) and (macdHist < lastHighMACD)
revBearDivMACD := (high < lastHighPrice) and (macdHist > lastHighMACD)
lastHighPrice := high
lastHighRSI := rsi
lastHighMACD := macdHist

// === LABELS + HORIZONTAL LINES ===
plotLabel(cond, txt, loc, col) =>
if cond
label.new(bar_index, loc == location.abovebar ? high : low, txt,
style=label.style_label_left, textcolor=color.white, size=size.small, color=col)
line l = line.new(bar_index, loc == location.abovebar ? high : low, bar_index + 5, loc == location.abovebar ? high : low, color=col, width=1)
array.push(lines, l)

plotLabel(bullDivRSI, "RSI\nBull Div", location.belowbar, color.green)
plotLabel(bearDivRSI, "RSI\nBear Div", location.abovebar, color.red)
plotLabel(revBullDivRSI, "RSI\nRevBull", location.belowbar, color.lime)
plotLabel(revBearDivRSI, "RSI\nRevBear", location.abovebar, color.orange)

plotLabel(bullDivMACD, "MACD\nBull Div", location.belowbar, color.navy)
plotLabel(bearDivMACD, "MACD\nBear Div", location.abovebar, color.maroon)
plotLabel(revBullDivMACD, "MACD\nRevBull", location.belowbar, color.blue)
plotLabel(revBearDivMACD, "MACD\nRevBear", location.abovebar, color.purple)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.