OPEN-SOURCE SCRIPT
🔁 Divergence Detector (RSI + MACD)

//version=5
indicator("🔁 Divergence Detector (RSI + MACD)", 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")
lookback = input.int(20, "Lookback Range")
// === INDICATOR CALCULATIONS ===
rsi = ta.rsi(rsiSource, rsiLength)
[macdLine, signalLine, _] = ta.macd(macdSrc, macdFast, macdSlow, macdSig)
macdHist = macdLine - signalLine
// === DIVERGENCE DETECTION ===
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
// Detect bottoms (for Bullish Divergences)
if ta.lowestbars(low, 3) == 1
// Store recent low
bullCondition = true
// RSI
if useRSI
if not na(lastLowRSI)
bullDivRSI := (low < lastLowPrice) and (rsi > lastLowRSI)
revBullDivRSI := (low > lastLowPrice) and (rsi < lastLowRSI)
lastLowPrice := low
lastLowRSI := rsi
// MACD
if useMACD
if not na(lastLowMACD)
bullDivMACD := (low < lastLowPrice) and (macdHist > lastLowMACD)
revBullDivMACD := (low > lastLowPrice) and (macdHist < lastLowMACD)
lastLowMACD := macdHist
// Detect tops (for Bearish Divergences)
if ta.highestbars(high, 3) == 1
// RSI
if useRSI
if not na(lastHighRSI)
bearDivRSI := (high > lastHighPrice) and (rsi < lastHighRSI)
revBearDivRSI := (high < lastHighPrice) and (rsi > lastHighRSI)
lastHighPrice := high
lastHighRSI := rsi
// MACD
if useMACD
if not na(lastHighMACD)
bearDivMACD := (high > lastHighPrice) and (macdHist < lastHighMACD)
revBearDivMACD := (high < lastHighPrice) and (macdHist > lastHighMACD)
lastHighMACD := macdHist
// === PLOTS ===
// RSI-based Divergences
plotshape(bullDivRSI, title="Bullish RSI Div", location=location.belowbar, color=color.green, style=shape.labelup, text="RSI\nBull Div")
plotshape(bearDivRSI, title="Bearish RSI Div", location=location.abovebar, color=color.red, style=shape.labeldown, text="RSI\nBear Div")
plotshape(revBullDivRSI, title="Rev Bull RSI", location=location.belowbar, color=color.lime, style=shape.triangleup, text="RSI\nRevBull")
plotshape(revBearDivRSI, title="Rev Bear RSI", location=location.abovebar, color=color.orange, style=shape.triangledown, text="RSI\nRevBear")
// MACD-based Divergences
plotshape(bullDivMACD, title="Bullish MACD Div", location=location.belowbar, color=color.navy, style=shape.labelup, text="MACD\nBull Div")
plotshape(bearDivMACD, title="Bearish MACD Div", location=location.abovebar, color=color.maroon, style=shape.labeldown, text="MACD\nBear Div")
plotshape(revBullDivMACD, title="Rev Bull MACD", location=location.belowbar, color=color.blue, style=shape.triangleup, text="MACD\nRevBull")
plotshape(revBearDivMACD, title="Rev Bear MACD", location=location.abovebar, color=color.purple, style=shape.triangledown, text="MACD\nRevBear")
indicator("🔁 Divergence Detector (RSI + MACD)", 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")
lookback = input.int(20, "Lookback Range")
// === INDICATOR CALCULATIONS ===
rsi = ta.rsi(rsiSource, rsiLength)
[macdLine, signalLine, _] = ta.macd(macdSrc, macdFast, macdSlow, macdSig)
macdHist = macdLine - signalLine
// === DIVERGENCE DETECTION ===
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
// Detect bottoms (for Bullish Divergences)
if ta.lowestbars(low, 3) == 1
// Store recent low
bullCondition = true
// RSI
if useRSI
if not na(lastLowRSI)
bullDivRSI := (low < lastLowPrice) and (rsi > lastLowRSI)
revBullDivRSI := (low > lastLowPrice) and (rsi < lastLowRSI)
lastLowPrice := low
lastLowRSI := rsi
// MACD
if useMACD
if not na(lastLowMACD)
bullDivMACD := (low < lastLowPrice) and (macdHist > lastLowMACD)
revBullDivMACD := (low > lastLowPrice) and (macdHist < lastLowMACD)
lastLowMACD := macdHist
// Detect tops (for Bearish Divergences)
if ta.highestbars(high, 3) == 1
// RSI
if useRSI
if not na(lastHighRSI)
bearDivRSI := (high > lastHighPrice) and (rsi < lastHighRSI)
revBearDivRSI := (high < lastHighPrice) and (rsi > lastHighRSI)
lastHighPrice := high
lastHighRSI := rsi
// MACD
if useMACD
if not na(lastHighMACD)
bearDivMACD := (high > lastHighPrice) and (macdHist < lastHighMACD)
revBearDivMACD := (high < lastHighPrice) and (macdHist > lastHighMACD)
lastHighMACD := macdHist
// === PLOTS ===
// RSI-based Divergences
plotshape(bullDivRSI, title="Bullish RSI Div", location=location.belowbar, color=color.green, style=shape.labelup, text="RSI\nBull Div")
plotshape(bearDivRSI, title="Bearish RSI Div", location=location.abovebar, color=color.red, style=shape.labeldown, text="RSI\nBear Div")
plotshape(revBullDivRSI, title="Rev Bull RSI", location=location.belowbar, color=color.lime, style=shape.triangleup, text="RSI\nRevBull")
plotshape(revBearDivRSI, title="Rev Bear RSI", location=location.abovebar, color=color.orange, style=shape.triangledown, text="RSI\nRevBear")
// MACD-based Divergences
plotshape(bullDivMACD, title="Bullish MACD Div", location=location.belowbar, color=color.navy, style=shape.labelup, text="MACD\nBull Div")
plotshape(bearDivMACD, title="Bearish MACD Div", location=location.abovebar, color=color.maroon, style=shape.labeldown, text="MACD\nBear Div")
plotshape(revBullDivMACD, title="Rev Bull MACD", location=location.belowbar, color=color.blue, style=shape.triangleup, text="MACD\nRevBull")
plotshape(revBearDivMACD, title="Rev Bear MACD", location=location.abovebar, color=color.purple, style=shape.triangledown, text="MACD\nRevBear")
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.