RSI Divergence Reversal

Detects RSI divergence patterns to identify potential trend reversals at overbought/oversold levels.

RSI divergence is one of the most discussed signals in technical analysis and one of the most difficult to trade mechanically. When price makes a new extreme but momentum does not confirm it, the theory says the move is losing conviction. The reality is that divergence can persist far longer than expected before it resolves. This strategy detects simplified divergence patterns with a defined entry, stop, and target — removing the discretion that makes divergence trading so inconsistent.

The Relative Strength Index (RSI) was developed by J. Welles Wilder Jr. and introduced in his 1978 book New Concepts in Technical Trading Systems. While the RSI is commonly used as a simple overbought/oversold oscillator, one of its most powerful applications is divergence analysis — a technique that can signal potential trend reversals before they happen.

RSI divergence occurs when price and the RSI indicator move in opposite directions. A bullish divergence forms when price makes a lower low but RSI makes a higher low, suggesting that bearish momentum is weakening despite new price lows. Conversely, a bearish divergence occurs when price makes a higher high but RSI makes a lower high, indicating fading bullish momentum.

Divergence signals are considered among the most reliable reversal indicators in technical analysis, though they require patience — divergences can persist for several bars before price actually reverses. This is why combining divergence with overbought/oversold levels (above 70 or below 30) significantly improves signal quality.

This strategy uses a simplified divergence detection method, comparing the current bar's price and RSI readings against the highest/lowest values over the previous 5 bars. While more sophisticated methods exist (such as pivot-based detection), this approach captures the core concept effectively.

//@version=6
strategy("RSI Reversal Strategy", overlay=false)

// Inputs
rsiLength = input.int(14, "RSI Length")
overbought = input.int(70, "Overbought Level")
oversold = input.int(30, "Oversold Level")

// Calculate RSI
rsiValue = ta.rsi(close, rsiLength)

// Divergence detection (simplified)
priceHigher = high > ta.highest(high, 5)[1]
rsiLower = rsiValue < ta.highest(rsiValue, 5)[1]
bearishDiv = priceHigher and rsiLower and rsiValue > overbought

priceLower = low < ta.lowest(low, 5)[1]
rsiHigher = rsiValue > ta.lowest(rsiValue, 5)[1]
bullishDiv = priceLower and rsiHigher and rsiValue < oversold
// Entries
if (bullishDiv)
    strategy.entry("RSI Long", strategy.long)
if (bearishDiv)
    strategy.entry("RSI Short", strategy.short)

// Plot RSI
plot(rsiValue, "RSI", color=color.purple, linewidth=2)
hline(overbought, "Overbought", color=color.red)
hline(oversold, "Oversold", color=color.green)
hline(50, "Middle", color=color.gray)

plotshape(bullishDiv, style=shape.triangleup, location=location.bottom, color=color.green, size=size.small, text="Bull Div")
plotshape(bearishDiv, style=shape.triangledown, location=location.top, color=color.red, size=size.small, text="Bear Div")