OPEN-SOURCE SCRIPT
🕴 Rajnikanth Divergence Tool

//version=5
indicator("🕴 Rajnikanth Divergence Tool", overlay=true)
// === INPUTS ===
useRSI = input.bool(true, "Enable RSI Divergence")
useMACD = input.bool(true, "Enable MACD Divergence")
showLabels = input.bool(true, "Show Divergence Labels")
showHLines = input.bool(true, "Show Horizontal Lines")
showConnLines = input.bool(true, "Show Connecting Lines")
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")
volFilter = input.bool(true, "Use Volume Filter")
volLen = input.int(20, "Volume MA Length")
// === CALCULATIONS ===
rsi = ta.rsi(rsiSource, rsiLength)
[macdLine, signalLine, _] = ta.macd(macdSrc, macdFast, macdSlow, macdSig)
macdHist = macdLine - signalLine
volMA = ta.sma(volume, volLen)
volOk = not volFilter or volume > volMA
// === TRACKING VARIABLES (Fixed typing)
var float lastLowPrice = na
var float lastLowRSI = na
var float lastLowMACD = na
var int lastLowBar = na
var float lastHighPrice = na
var float lastHighRSI = na
var float lastHighMACD = na
var int lastHighBar = na
// === DIVERGENCE FLAGS ===
bullDivRSI = false
bearDivRSI = false
revBullDivRSI = false
revBearDivRSI = false
bullDivMACD = false
bearDivMACD = false
revBullDivMACD = false
revBearDivMACD = false
// === FUNCTION TO DRAW DIVERGENCES ===
plotDivergence(cond, labelText, location, color_, price, fromBar) =>
if cond and volOk
if showLabels
label.new(bar_index, price, labelText, style=label.style_label_left, textcolor=color.white, size=size.small, color=color_)
if showHLines
line.new(bar_index, price, bar_index + 5, price, color=color_, width=1)
if showConnLines and not na(fromBar)
line.new(fromBar, price, bar_index, price, color=color_, width=1)
// === DETECT BOTTOMS ===
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
lastLowBar := bar_index
// === DETECT TOPS ===
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
lastHighBar := bar_index
// === PLOTS ===
plotDivergence(bullDivRSI, "RSI\nBull Div", location.belowbar, color.green, low, lastLowBar)
plotDivergence(bearDivRSI, "RSI\nBear Div", location.abovebar, color.red, high, lastHighBar)
plotDivergence(revBullDivRSI, "RSI\nRevBull", location.belowbar, color.lime, low, lastLowBar)
plotDivergence(revBearDivRSI, "RSI\nRevBear", location.abovebar, color.orange, high, lastHighBar)
plotDivergence(bullDivMACD, "MACD\nBull Div", location.belowbar, color.navy, low, lastLowBar)
plotDivergence(bearDivMACD, "MACD\nBear Div", location.abovebar, color.maroon, high, lastHighBar)
plotDivergence(revBullDivMACD, "MACD\nRevBull", location.belowbar, color.blue, low, lastLowBar)
plotDivergence(revBearDivMACD, "MACD\nRevBear", location.abovebar, color.purple, high, lastHighBar)
// === ALERT CONDITIONS ===
alertcondition(bullDivRSI, "RSI Bullish Divergence", "RSI Bullish Divergence detected")
alertcondition(bearDivRSI, "RSI Bearish Divergence", "RSI Bearish Divergence detected")
alertcondition(revBullDivRSI, "RSI Reverse Bullish", "RSI Reverse Bullish Divergence detected")
alertcondition(revBearDivRSI, "RSI Reverse Bearish", "RSI Reverse Bearish Divergence detected")
alertcondition(bullDivMACD, "MACD Bullish Divergence", "MACD Bullish Divergence detected")
alertcondition(bearDivMACD, "MACD Bearish Divergence", "MACD Bearish Divergence detected")
alertcondition(revBullDivMACD, "MACD Reverse Bullish", "MACD Reverse Bullish Divergence detected")
alertcondition(revBearDivMACD, "MACD Reverse Bearish", "MACD Reverse Bearish Divergence detected")
indicator("🕴 Rajnikanth Divergence Tool", overlay=true)
// === INPUTS ===
useRSI = input.bool(true, "Enable RSI Divergence")
useMACD = input.bool(true, "Enable MACD Divergence")
showLabels = input.bool(true, "Show Divergence Labels")
showHLines = input.bool(true, "Show Horizontal Lines")
showConnLines = input.bool(true, "Show Connecting Lines")
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")
volFilter = input.bool(true, "Use Volume Filter")
volLen = input.int(20, "Volume MA Length")
// === CALCULATIONS ===
rsi = ta.rsi(rsiSource, rsiLength)
[macdLine, signalLine, _] = ta.macd(macdSrc, macdFast, macdSlow, macdSig)
macdHist = macdLine - signalLine
volMA = ta.sma(volume, volLen)
volOk = not volFilter or volume > volMA
// === TRACKING VARIABLES (Fixed typing)
var float lastLowPrice = na
var float lastLowRSI = na
var float lastLowMACD = na
var int lastLowBar = na
var float lastHighPrice = na
var float lastHighRSI = na
var float lastHighMACD = na
var int lastHighBar = na
// === DIVERGENCE FLAGS ===
bullDivRSI = false
bearDivRSI = false
revBullDivRSI = false
revBearDivRSI = false
bullDivMACD = false
bearDivMACD = false
revBullDivMACD = false
revBearDivMACD = false
// === FUNCTION TO DRAW DIVERGENCES ===
plotDivergence(cond, labelText, location, color_, price, fromBar) =>
if cond and volOk
if showLabels
label.new(bar_index, price, labelText, style=label.style_label_left, textcolor=color.white, size=size.small, color=color_)
if showHLines
line.new(bar_index, price, bar_index + 5, price, color=color_, width=1)
if showConnLines and not na(fromBar)
line.new(fromBar, price, bar_index, price, color=color_, width=1)
// === DETECT BOTTOMS ===
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
lastLowBar := bar_index
// === DETECT TOPS ===
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
lastHighBar := bar_index
// === PLOTS ===
plotDivergence(bullDivRSI, "RSI\nBull Div", location.belowbar, color.green, low, lastLowBar)
plotDivergence(bearDivRSI, "RSI\nBear Div", location.abovebar, color.red, high, lastHighBar)
plotDivergence(revBullDivRSI, "RSI\nRevBull", location.belowbar, color.lime, low, lastLowBar)
plotDivergence(revBearDivRSI, "RSI\nRevBear", location.abovebar, color.orange, high, lastHighBar)
plotDivergence(bullDivMACD, "MACD\nBull Div", location.belowbar, color.navy, low, lastLowBar)
plotDivergence(bearDivMACD, "MACD\nBear Div", location.abovebar, color.maroon, high, lastHighBar)
plotDivergence(revBullDivMACD, "MACD\nRevBull", location.belowbar, color.blue, low, lastLowBar)
plotDivergence(revBearDivMACD, "MACD\nRevBear", location.abovebar, color.purple, high, lastHighBar)
// === ALERT CONDITIONS ===
alertcondition(bullDivRSI, "RSI Bullish Divergence", "RSI Bullish Divergence detected")
alertcondition(bearDivRSI, "RSI Bearish Divergence", "RSI Bearish Divergence detected")
alertcondition(revBullDivRSI, "RSI Reverse Bullish", "RSI Reverse Bullish Divergence detected")
alertcondition(revBearDivRSI, "RSI Reverse Bearish", "RSI Reverse Bearish Divergence detected")
alertcondition(bullDivMACD, "MACD Bullish Divergence", "MACD Bullish Divergence detected")
alertcondition(bearDivMACD, "MACD Bearish Divergence", "MACD Bearish Divergence detected")
alertcondition(revBullDivMACD, "MACD Reverse Bullish", "MACD Reverse Bullish Divergence detected")
alertcondition(revBearDivMACD, "MACD Reverse Bearish", "MACD Reverse Bearish Divergence detected")
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
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.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
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.