RSI Divergence Pro — Momentum Drop
# RSI Divergence Pro — Momentum Drop
A clean Wilder's RSI with automatic regular bullish and bearish divergence detection. Pivots are scanned on price and confirmed against the RSI to flag exhaustion *before* the move actually reverses.
## What it does
- Plots Wilder's RSI with overbought (70), oversold (30), and a 50 midline.
- Detects **regular bullish divergence**: price prints a lower low while RSI prints a higher low.
- Detects **regular bearish divergence**: price prints a higher high while RSI prints a lower high.
- Marks each divergence with an up/down arrow on the RSI line at the actual pivot bar.
- Shades the overbought and oversold zones for quick visual context.
- Fires alerts on each new bullish or bearish divergence.
## Recommended settings
- RSI Length: **14** (Wilder's smoothing)
- Overbought: **70**
- Oversold: **30**
- Pivot Lookback: **5** bars (confirmation window — higher = fewer, cleaner signals)
- Price Source: **close**
## Ideal timeframe
Works on any timeframe; sweet spot is the 1H and 4H for swing setups, and 5m/15m for intraday reversal scalps.
## How to use
Wait for a divergence arrow inside or near the OB/OS zones — those are the highest-quality signals. Treat the divergence as a *warning*, not an entry. Combine it with a structure break (trendline, prior swing, or VWAP reclaim) for confirmation. Divergences confirm after the pivot completes (pivot lookback bars), so plan your trigger one or two bars after the arrow prints.
## thinkScript code
```
# ─────────────────────────────────────────────
# │ MarketFragments.com | DNA & Market │
# │ info@marketfragments.com │
# │ www.marketfragments.com │
# ─────────────────────────────────────────────
# Time →
# │
# █ █ █│ █
# █ █ █ │ █ █
# █ █ █ │ █ █ █ ╭─╮
# █ █ █ │ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ █ █╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ ╰─────╯
# ──────┴──────────────────────────────────────────────────────────────
# T1 T2 T3 T4 T5 T6
#
# RSI Divergence Pro
# Wilder's RSI with auto-detected regular bullish/bearish divergences.
# Free for non-commercial use. Tweaks and forks welcome.
declare lower;
input price = close;
input rsiLength = 14;
input overbought = 70;
input oversold = 30;
input pivotLookback = 5;
# Wilder's RSI
def NetChgAvg = MovingAverage(AverageType.WILDERS, price - price[1], rsiLength);
def TotChgAvg = MovingAverage(AverageType.WILDERS, AbsValue(price - price[1]), rsiLength);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
plot RSILine = RSI;
RSILine.SetDefaultColor(CreateColor(80, 160, 230));
RSILine.SetLineWeight(2);
plot OB = overbought;
plot OS = oversold;
plot Mid = 50;
OB.SetDefaultColor(Color.RED);
OB.SetStyle(Curve.SHORT_DASH);
OS.SetDefaultColor(Color.GREEN);
OS.SetStyle(Curve.SHORT_DASH);
Mid.SetDefaultColor(Color.GRAY);
Mid.SetStyle(Curve.SHORT_DASH);
AddCloud(100, OB, CreateColor(60, 0, 0));
AddCloud(OS, 0, CreateColor(0, 50, 0));
# Pivot detection (centered window, confirms pivotLookback bars later)
def pivotLowBar = low[pivotLookback] == Lowest(low, 2 * pivotLookback + 1);
def pivotHighBar = high[pivotLookback] == Highest(high, 2 * pivotLookback + 1);
# Track current and previous pivots
def lastPLPrice = if pivotLowBar then low[pivotLookback] else lastPLPrice[1];
def lastPLRsi = if pivotLowBar then RSI[pivotLookback] else lastPLRsi[1];
def prevPLPrice = if pivotLowBar then lastPLPrice[1] else prevPLPrice[1];
def prevPLRsi = if pivotLowBar then lastPLRsi[1] else prevPLRsi[1];
def lastPHPrice = if pivotHighBar then high[pivotLookback] else lastPHPrice[1];
def lastPHRsi = if pivotHighBar then RSI[pivotLookback] else lastPHRsi[1];
def prevPHPrice = if pivotHighBar then lastPHPrice[1] else prevPHPrice[1];
def prevPHRsi = if pivotHighBar then lastPHRsi[1] else prevPHRsi[1];
def bullDiv = pivotLowBar and !IsNaN(prevPLPrice) and low[pivotLookback] < prevPLPrice and RSI[pivotLookback] > prevPLRsi;
def bearDiv = pivotHighBar and !IsNaN(prevPHPrice) and high[pivotLookback] > prevPHPrice and RSI[pivotLookback] < prevPHRsi;
# Plot arrows at the actual pivot bar (forward-reference on historical data)
plot BullArrow = if bullDiv[-pivotLookback] then RSI else Double.NaN;
BullArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullArrow.SetDefaultColor(Color.GREEN);
BullArrow.SetLineWeight(3);
plot BearArrow = if bearDiv[-pivotLookback] then RSI else Double.NaN;
BearArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearArrow.SetDefaultColor(Color.RED);
BearArrow.SetLineWeight(3);
Alert(bullDiv, "RSI Bullish Divergence", Alert.BAR, Sound.Chimes);
Alert(bearDiv, "RSI Bearish Divergence", Alert.BAR, Sound.Bell);
```
Drop your tweaks, screenshots, and questions below — that's how the library gets better.

