RC Tools - Divergence DetectorRC Tools — Divergence Detector
────────────────────────────────────────────────────────────────────
█ OVERVIEW
Most divergence tools pattern-match swing highs and lows, which is finicky and often technically repaints — pivots can un-confirm as new bars form. This tool instead measures rolling correlation between price and a momentum oscillator of your choice. When price and momentum stop agreeing, that disagreement is the divergence — measured continuously, not detected as a one-off pattern.
█ WHAT IT DOES
Plots the rolling correlation between price and a selectable oscillator (RSI, MACD line, Rate of Change, or a custom source) on a -1 to +1 scale. Classifies each confirmed bar into one of three states — Confirmed Trend, Bearish Divergence, Bullish Divergence — colours the chart background accordingly, and shows a table with the current state, how long price has been in it, and historical base rates (average forward return and win rate) for each divergence state.
█ THE THEORY BEHIND IT
A genuine trend has price and momentum moving together — new highs accompanied by strengthening momentum, new lows by weakening momentum. When that relationship breaks down — price continues in one direction while the oscillator stops confirming it — that is a divergence. Rather than searching for specific swing-point patterns (which depend on exactly which pivots you pick and can shift as price continues), this tool asks the more direct statistical question: over the last N bars, how closely have price and the oscillator actually moved together? A strong positive correlation means they agree. A correlation that has dropped toward zero or negative means they have stopped agreeing, regardless of what any single pivot looks like.
█ HOW IT IS CALCULATED
1. Compute the selected oscillator: RSI, MACD line (fast EMA minus slow EMA), Rate of Change %, or a custom source you provide.
2. Compute the rolling Pearson correlation between price (close) and the oscillator over a configurable window (default 14 bars).
3. If that correlation falls below a threshold (default 0.0), price and momentum are no longer confirming each other — a divergence state.
4. The divergence is labelled Bearish if price has been rising over a short lookback (momentum failing to confirm continued strength) or Bullish if price has been falling (momentum failing to confirm continued weakness).
Classification occurs ONLY on confirmed bar close — the state and the displayed correlation are computed and committed together, so they can never disagree mid-bar or flip back and forth as the current bar forms.
█ SETTINGS & CONFIGURATION
• Oscillator (default RSI) — RSI / MACD Line / Rate of Change % / Custom Source
• RSI / MACD / Rate of Change lookbacks (defaults 14 / 12+26 / 20)
• Correlation Window (default 14 bars) — how far back the co-movement is measured
• Divergence Threshold (default 0.0) — the correlation level below which price and momentum are considered to have stopped agreeing
• Price Direction Lookback (default 5 bars) — used only to label a divergence bullish or bearish
• Forward Return Window (default 20 bars) — the horizon used for the base-rate table
• Paint Main Chart Background — toggle off if you only want the correlation pane
█ HOW TO USE IT
Use it as a warning flag on an existing trend read, not as a standalone entry signal. Example: if you're long into a rally and the background flags Bearish Divergence, that's a cue to tighten risk management or look for confirmation elsewhere before assuming the move continues unchecked — it is not, by itself, a sell signal. Check the base-rate table's sample count before treating any single divergence reading as meaningfully predictive.
Works on any asset and timeframe with sufficient history for the correlation window.
█ LIMITATIONS
• Divergence describes a PRESENT disagreement between price and momentum. It does not predict a reversal, and any use of it as a forecast is a misuse.
• Correlation is measured over a rolling window and is noisy by nature — expect it to cross the threshold repeatedly in choppy, range-bound conditions.
• The oscillator itself is not plotted, only its correlation with price — this keeps the pane on one consistent scale regardless of which oscillator is selected (RSI is bounded 0-100, MACD line is unbounded, etc.).
• The bullish/bearish label depends on a short price-direction lookback, which can flip near genuine turning points independently of the correlation reading itself.
• Historical base-rate stats need a meaningful sample count (check N) before being trusted, especially for less common states.
• This script does NOT repaint. All classification updates on confirmed bar close only.
█ DISCLAIMER
For educational and informational purposes only. Nothing here is financial advice. Past behaviour of any divergence state does not indicate future results. Trade at your own risk.
指標

[davidev] RSI with Divergences**RSI with Divergences - Optimized**
An enhanced Relative Strength Index (RSI) indicator designed to combine classic momentum analysis, automatic divergence detection, and an EMA-based momentum filter in a single oscillator.
The script automatically detects both **regular** and **hidden RSI divergences** by comparing confirmed RSI pivot highs/lows with the corresponding price structure.
### Features
* Standard configurable **RSI**
* Configurable **EMA of RSI**
* Dynamic EMA coloring based on RSI momentum
* Optional **DMI directional filter** for EMA coloring
* Automatic **Regular Bullish Divergence**
* Automatic **Regular Bearish Divergence**
* Automatic **Hidden Bullish Divergence**
* Automatic **Hidden Bearish Divergence**
* Configurable pivot sensitivity
* Optional divergence labels
* Custom colors and divergence line width
* Strict or inclusive high/low comparisons
* Automatic management of historical divergence drawings
* Built-in TradingView alerts for individual and combined divergence signals
### Divergence Logic
**Regular Bullish Divergence**
* Price forms a **lower low**
* RSI forms a **higher low**
This can indicate weakening bearish momentum and a potential bullish reversal.
**Regular Bearish Divergence**
* Price forms a **higher high**
* RSI forms a **lower high**
This can indicate weakening bullish momentum and a potential bearish reversal.
**Hidden Bullish Divergence**
* Price forms a **higher low**
* RSI forms a **lower low**
Hidden bullish divergences are generally associated with potential **bullish trend continuation**.
**Hidden Bearish Divergence**
* Price forms a **lower high**
* RSI forms a **higher high**
Hidden bearish divergences are generally associated with potential **bearish trend continuation**.
### EMA of RSI Momentum
The indicator also plots an EMA of the RSI to provide a smoother view of momentum.
By default, its color combines:
* The direction of the RSI EMA
* The relationship between **+DI and -DI**
The EMA is considered bullish when it is rising and +DI is greater than or equal to -DI, while bearish momentum is highlighted when the EMA is falling and -DI is greater than +DI.
The DMI filter can be disabled from the settings, in which case EMA coloring is determined exclusively by whether the RSI EMA is rising or falling.
### Pivot Confirmation
Divergences are calculated using confirmed RSI pivots.
The **Pivot Left** and **Pivot Right** settings control how many surrounding bars are required to validate a pivot.
Because a pivot requires future bars for confirmation, a divergence is only confirmed after the number of bars specified by **Pivot Right** has elapsed. The divergence is visually connected to the actual historical pivot points, while alerts trigger on the bar where the divergence becomes confirmed.
This avoids treating an unconfirmed pivot as a completed divergence.
### Strict Comparison
When **Strict HH/LL Comparison** is enabled, equal price or RSI highs/lows are not considered valid higher-high, lower-high, higher-low, or lower-low conditions.
Disabling it allows equal values to satisfy the relevant comparison.
### Alerts
The script includes alert conditions for:
* Regular Bullish RSI Divergence
* Hidden Bullish RSI Divergence
* Regular Bearish RSI Divergence
* Hidden Bearish RSI Divergence
* Any Bullish RSI Divergence
* Any Bearish RSI Divergence
* Any RSI Divergence
Alerts are generated when the corresponding pivot and divergence are **confirmed**, rather than on the historical pivot bar itself.
### Usage
RSI divergences can help identify changes in momentum, possible trend exhaustion, reversals, and continuation setups. They should not be treated as standalone buy or sell signals.
For better results, consider combining them with market structure, support and resistance, trend direction, volume, volatility, or other forms of confirmation.
The indicator is designed to provide a clean visual representation of RSI momentum while keeping divergence detection systematic, configurable, and easy to interpret.
指標

RSI Smart Divergence [josseliani]RSI Smart Divergence is an RSI divergence indicator designed to provide a clean and flexible way to work with confirmed bullish and bearish divergences.
There are many RSI divergence indicators available. I created this version because I wanted two different approaches to divergence detection in one simple tool: a selective Zone mode focused on overbought and oversold areas, and a separate Regular mode for more traditional divergence analysis.
One of the main visual features of RSI Smart Divergence is that confirmed divergences can be displayed simultaneously on the RSI and directly on the main price chart. The RSI line shows the momentum structure, while the price-chart line connects the corresponding local price extremes. This makes it possible to see the disagreement between price and RSI without manually matching oscillator pivots to candles.
ZONE MODE
Zone mode is the original logic of RSI Smart Divergence and is enabled by default.
Instead of evaluating every RSI pivot as the next divergence reference, this mode focuses specifically on divergence structures formed within the overbought and oversold zones.
For bullish divergence, the relevant RSI pivot points are evaluated within the oversold area.
For bearish divergence, the relevant RSI pivot points are evaluated within the overbought area.
Intermediate RSI pivots outside the relevant zone do not replace the previous zone reference point. This allows the indicator to compare significant momentum extremes inside the same zone even when other RSI swings occur between them.
As a result, Zone mode behaves differently from a standard adjacent-pivot divergence detector and provides a more selective view of divergence developing in already stretched momentum conditions.
REGULAR MODE
Regular mode is available as a separate optional mode.
It identifies classic regular bullish and bearish divergence using confirmed RSI pivots and corresponding local price extremes.
Additional structural filtering checks the price path between the selected endpoints and rejects structures where an intermediate price extreme invalidates the divergence being evaluated.
Regular mode is disabled by default.
The two modes operate independently and can identify different divergences. You can use Zone mode, Regular mode, or enable both at the same time.
CONFIRMED PIVOTS
Both modes use confirmed RSI pivots.
Pivot Left and Pivot Right determine how many bars are required around a potential RSI swing before it is considered confirmed.
The important setting for signal timing is Pivot Right.
For example:
Pivot Right = 3 — confirmation requires 3 bars to form to the right of the pivot.
Pivot Right = 10 — confirmation requires 10 bars to form to the right of the pivot.
Because of this confirmation process, a divergence becomes known only after the required right-side bars have formed.
The divergence lines are drawn back to the confirmed pivot locations, while the triangle marker and alert appear when the divergence is actually confirmed.
This distinction is important when reviewing historical charts: the lines identify the structure that produced the divergence, but the divergence was not known in real time until its confirmation bar.
PRICE AND RSI DISPLAY
Each confirmed divergence can be visualized in two places:
— on the RSI;
— directly on the corresponding price structure on the main chart.
The RSI line connects the confirmed momentum pivots used for the divergence.
The price-chart line connects the corresponding local price extremes.
This makes the relationship between price and oscillator structure immediately visible without manually matching RSI pivots to individual candles.
The RSI and price divergence lines can be enabled or disabled independently.
Small green and red triangles mark the confirmation of bullish and bearish divergence events in the RSI pane.
HOW I USE IT
The RSI period and pivot settings can be adjusted depending on the timeframe and the amount of market detail you want the indicator to capture.
For faster lower-timeframe trading, I may use a shorter RSI period such as 5 or 9 together with a smaller pivot setting around 3. This makes the indicator more responsive, but naturally includes more short-term market movement.
RSI 14 is a useful general starting point.
For higher timeframes, or when I want to focus on larger divergence structures, I normally keep RSI at 14 and increase the pivot setting.
For example, on a 15-minute chart I may use a pivot value around 10. This is one example configuration shown on the chart attached to this publication.
Smaller pivot settings produce faster and more frequent structures.
Larger pivot settings require more confirmation and tend to focus on broader swings.
WHAT MAKES THIS VERSION DIFFERENT
The main purpose of RSI Smart Divergence is not simply to mark every regular RSI divergence.
The original Zone mode maintains its own overbought and oversold divergence structure and ignores intermediate RSI pivots outside the relevant extreme zone when selecting the next comparison point.
Regular mode provides a separate approach for traders who also want traditional divergence detection.
Together with confirmed-pivot logic, corresponding price-extreme mapping, divergence lines on both RSI and price, adjustable structural distance and alerts, the two modes provide different ways to analyze divergence without requiring several separate indicators.
SETTINGS
RSI Length controls the RSI calculation period.
Overbought and Oversold Levels define the extreme RSI zones used by Zone mode.
Zone oversold / overbought enables the original Zone mode.
Regular enables regular divergence detection.
Pivot Left and Pivot Right control pivot confirmation.
Max bars between points limits the maximum distance between the two points of a divergence.
The RSI and price divergence lines can be shown or hidden independently.
Optional background highlighting can also be enabled for the overbought and oversold areas.
ALERTS
Alerts are available for confirmed bullish and bearish RSI divergences.
Alerts trigger when the divergence is confirmed, not retrospectively on the original pivot bar.
NOTES
RSI Smart Divergence is an analytical tool rather than an automatic trading system.
Divergence does not guarantee a market reversal. Different RSI periods and pivot settings can produce significantly different results, so settings should be selected according to the market, timeframe and trading approach being used.
The indicator should be used together with independent market analysis and appropriate risk management. 指標

指標
