OPEN-SOURCE SCRIPT
RSI Predictive Divergence

RSI Predictive Divergence — Forming Divergence Detector
OVERVIEW
This indicator detects RSI divergences in two stages: a forming stage (early warning) and a confirmed stage (classic pivot-based). The forming stage is the unique component — it uses linear regression slope analysis to identify divergence conditions building up in real time, before a pivot is confirmed.
HOW IT WORKS
1. Slope Calculation
The indicator computes linear regression slopes for both RSI and price over a user-defined lookback period (default 5 bars). The price slope is ATR-normalized so that the comparison works consistently across different instruments and timeframes.
2. Slope Angles
Both slopes are converted to angles in degrees using the arctangent function. This gives a normalized way to compare RSI direction against price direction.
3. Opposition Counter
A counter tracks how many consecutive closed bars show opposing slopes (price down + RSI up = potential bullish divergence; price up + RSI down = potential bearish divergence). When the counter reaches the user-defined threshold (default 5 bars), a forming divergence is flagged.
4. Strength Score (0-100)
A composite score is calculated using three factors:
- Angle difference between RSI and price slopes (50% weight)
- Duration of slope opposition (30% weight)
- RSI zone context — oversold/overbought adds weight (20% weight)
Users can set a minimum strength threshold to filter low-quality signals.
5. Confirmed Divergence
Classic pivot-based divergence detection runs in parallel using RSI pivot highs and lows compared against price pivots. An anti-overlap gate prevents same-type labels from clustering too closely (default 8 bars minimum spacing).
NON-REPAINTING DESIGN
- All slope counters update only on confirmed bars (barstate.isconfirmed)
- Forming signals trigger only after bar close
- Confirmed divergence labels are drawn at the pivot bar, naturally
lagging by the pivot lookback period
- No lookahead_on, no future data access
INPUTS
- RSI Length, Source, Overbought/Oversold levels
- Slope lookback bars and minimum opposite-slope bars
- Minimum strength threshold for forming signals
- Pivot lookback for confirmed divergence
- Anti-overlap spacing between confirmed labels
- Label style: Compact, Icon Only, or Full
- Dashboard position, size, and compact/full mode
- Color customization for bullish, bearish, forming, and neutral states
DASHBOARD
A status table shows RSI value, RSI zone, slope angle difference, and live forming-divergence status with strength meters for both directions. Two display modes: Compact (7 rows) for minimal footprint or Full (10 rows) with detailed slope angles.
ALERTS
Four alert conditions available:
- Forming Bullish Divergence
- Forming Bearish Divergence
- Confirmed Bullish Divergence
- Confirmed Bearish Divergence
HOW TO USE
Forming divergences are early-warning signals — they indicate that divergence conditions are building. Wait for higher strength readings (60+) before acting, and combine with price-action context such as support/resistance, trend, and volume. Confirmed divergences provide
classical pivot-based signals after pivot formation. This indicator works across all timeframes.
NOTES
This is a technical analysis tool intended for educational and informational purposes. Past performance of any divergence signal does not guarantee future results. Always apply proper risk management and combine with your own trading methodology.
CREDITS
Original implementation. Uses standard RSI and pivot logic from TradingView's built-in functions combined with original slope-angle analysis methodology.
OVERVIEW
This indicator detects RSI divergences in two stages: a forming stage (early warning) and a confirmed stage (classic pivot-based). The forming stage is the unique component — it uses linear regression slope analysis to identify divergence conditions building up in real time, before a pivot is confirmed.
HOW IT WORKS
1. Slope Calculation
The indicator computes linear regression slopes for both RSI and price over a user-defined lookback period (default 5 bars). The price slope is ATR-normalized so that the comparison works consistently across different instruments and timeframes.
2. Slope Angles
Both slopes are converted to angles in degrees using the arctangent function. This gives a normalized way to compare RSI direction against price direction.
3. Opposition Counter
A counter tracks how many consecutive closed bars show opposing slopes (price down + RSI up = potential bullish divergence; price up + RSI down = potential bearish divergence). When the counter reaches the user-defined threshold (default 5 bars), a forming divergence is flagged.
4. Strength Score (0-100)
A composite score is calculated using three factors:
- Angle difference between RSI and price slopes (50% weight)
- Duration of slope opposition (30% weight)
- RSI zone context — oversold/overbought adds weight (20% weight)
Users can set a minimum strength threshold to filter low-quality signals.
5. Confirmed Divergence
Classic pivot-based divergence detection runs in parallel using RSI pivot highs and lows compared against price pivots. An anti-overlap gate prevents same-type labels from clustering too closely (default 8 bars minimum spacing).
NON-REPAINTING DESIGN
- All slope counters update only on confirmed bars (barstate.isconfirmed)
- Forming signals trigger only after bar close
- Confirmed divergence labels are drawn at the pivot bar, naturally
lagging by the pivot lookback period
- No lookahead_on, no future data access
INPUTS
- RSI Length, Source, Overbought/Oversold levels
- Slope lookback bars and minimum opposite-slope bars
- Minimum strength threshold for forming signals
- Pivot lookback for confirmed divergence
- Anti-overlap spacing between confirmed labels
- Label style: Compact, Icon Only, or Full
- Dashboard position, size, and compact/full mode
- Color customization for bullish, bearish, forming, and neutral states
DASHBOARD
A status table shows RSI value, RSI zone, slope angle difference, and live forming-divergence status with strength meters for both directions. Two display modes: Compact (7 rows) for minimal footprint or Full (10 rows) with detailed slope angles.
ALERTS
Four alert conditions available:
- Forming Bullish Divergence
- Forming Bearish Divergence
- Confirmed Bullish Divergence
- Confirmed Bearish Divergence
HOW TO USE
Forming divergences are early-warning signals — they indicate that divergence conditions are building. Wait for higher strength readings (60+) before acting, and combine with price-action context such as support/resistance, trend, and volume. Confirmed divergences provide
classical pivot-based signals after pivot formation. This indicator works across all timeframes.
NOTES
This is a technical analysis tool intended for educational and informational purposes. Past performance of any divergence signal does not guarantee future results. Always apply proper risk management and combine with your own trading methodology.
CREDITS
Original implementation. Uses standard RSI and pivot logic from TradingView's built-in functions combined with original slope-angle analysis methodology.
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。