ReversalIntermediateRSIPivot Points

RSI Divergence Strategy

RSI divergence occurs when price makes a new high (or low) that is not confirmed by RSI — a classic sign of weakening momentum and potential reversal. This strategy detects divergence using pivot highs and lows, then enters on the next bar with an ATR-based stop.

Best for: Ranging and topping/bottoming marketsTimeframes: 1H, 4H, Daily

Pine Script v6 — Complete Strategy Code

//@version=6
rsi-divergence-pine-script.pine
//@version=6
strategy("RSI Divergence", overlay=true,
     default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// ── Inputs ──
int   rsiLen   = input.int(defval=14, title="RSI Length",    minval=1, group="Settings")
int   pivLeft  = input.int(defval=5,  title="Pivot Left",    minval=1, group="Settings")
int   pivRight = input.int(defval=5,  title="Pivot Right",   minval=1, group="Settings")
float slMult   = input.float(defval=1.5, title="SL ATR Mult", minval=0.1, step=0.1, group="Risk")
float tpMult   = input.float(defval=3.0, title="TP ATR Mult", minval=0.1, step=0.1, group="Risk")

// ── Calculations ──
float rsi  = ta.rsi(close, rsiLen)
float atr  = ta.atr(14)

float phPrice = ta.pivothigh(high, pivLeft, pivRight)
float plPrice = ta.pivotlow(low,  pivLeft, pivRight)
float phRsi   = ta.pivothigh(rsi,  pivLeft, pivRight)
float plRsi   = ta.pivotlow(rsi,   pivLeft, pivRight)

var float lastPhPrice = na
var float lastPhRsi   = na
var float lastPlPrice = na
var float lastPlRsi   = na

bool bearDiv = false
bool bullDiv = false

if not na(phPrice)
    if not na(lastPhPrice)
        bearDiv := phPrice > lastPhPrice and phRsi < lastPhRsi
    lastPhPrice := phPrice
    lastPhRsi   := phRsi

if not na(plPrice)
    if not na(lastPlPrice)
        bullDiv := plPrice < lastPlPrice and plRsi > lastPlRsi
    lastPlPrice := plPrice
    lastPlRsi   := plRsi

// ── Visuals ──
plotshape(bearDiv, style=shape.triangledown, location=location.abovebar, color=color.new(color.red,   0), title="Bear Div", size=size.small)
plotshape(bullDiv, style=shape.triangleup,   location=location.belowbar, color=color.new(color.green, 0), title="Bull Div", size=size.small)

// ── Orders ──
if bullDiv
    strategy.entry("Long", strategy.long)
    strategy.exit("XL", from_entry="Long", stop=close - atr * slMult, limit=close + atr * tpMult)
if bearDiv
    strategy.entry("Short", strategy.short)
    strategy.exit("XS", from_entry="Short", stop=close + atr * slMult, limit=close - atr * tpMult)

// ── Alerts ──
alertcondition(bullDiv, title="Bullish Divergence", message="RSI bullish divergence — {{ticker}}")
alertcondition(bearDiv, title="Bearish Divergence", message="RSI bearish divergence — {{ticker}}")

Copy and paste into TradingView Pine Editor, or generate a custom variation with AI.

How it works

Strategy logic, step by step

  1. 1

    Pivot highs and lows are identified using ta.pivothigh() and ta.pivotlow().

  2. 2

    Bearish divergence: price pivot high is higher than the previous pivot high, but RSI pivot high is lower.

  3. 3

    Bullish divergence: price pivot low is lower than the previous pivot low, but RSI pivot low is higher.

  4. 4

    Entry triggers on the bar after confirmed divergence.

  5. 5

    Stop loss: ATR(14) × 1.5 beyond the entry bar.

Parameters

Configurable inputs

ParameterDefault
RSI Length14
Pivot Left5
Pivot Right5
SL ATR Mult1.5
TP ATR Mult3.0

Ask the TradePilot copilot to backtest this and sweep the inputs to find the best combination for your asset and timeframe.

FAQ

RSI Divergence Strategy questions

What is RSI divergence?

RSI divergence occurs when price makes a new extreme (higher high or lower low) but RSI does not confirm it — a sign that momentum is weakening. Bullish divergence (price lower low, RSI higher low) suggests a potential bottom. Bearish divergence (price higher high, RSI lower high) suggests a potential top.

Does RSI divergence work in trending markets?

RSI divergence is a reversal signal, so it works best at market turning points — not in strongly trending markets. Combine with a higher-timeframe trend filter to avoid fading strong trends.

More strategies

Other Pine Script strategies

Want a custom version of this strategy?

Describe your modifications in plain English — different indicators, different risk parameters, long-only, multi-timeframe filter — and TradePilot generates the full Pine Script v6 code in seconds.