OPEN-SOURCE SCRIPT

RSI Divergence (Regular + Hidden, @darshakssc)

185
This indicator detects regular and hidden divergence between price and RSI, using confirmed swing highs and swing lows (pivots) on both series. It is designed as a visual analysis tool, not as a signal generator or trading system.

The goal is to highlight moments where price action and RSI momentum move in different directions, which some traders study as potential early warnings of trend exhaustion or trend continuation. All divergence signals are only drawn after a pivot is fully confirmed, helping to avoid repainting.

The script supports four divergence types:
  • Regular Bullish Divergence
    *Regular Bearish Divergence
    *Hidden Bullish Divergence
    *Hidden Bearish Divergence

Each type is drawn with a different color and labeled clearly on the chart.

Core Concepts Used
1. RSI (Relative Strength Index)
  • The script uses standard RSI, calculated on a configurable input source (default: close) and length (default: 14).
  • RSI is treated purely as a momentum oscillator – the script does not enforce oversold/overbought interpretations.


2. Pivots / Swings
The indicator defines swing highs and swing lows using ta.pivothigh() and ta.pivotlow():
  • A swing high forms when a bar’s high is higher than a specified number of bars to the left and to the right.
  • A swing low forms when a bar’s low is lower than a specified number of bars to the left and to the right.
  • The same pivot logic is applied to both price and RSI.

Because pivots require “right side” bars to form, the indicator:
  • Waits for the full pivot to be confirmed (no forward-looking referencing beyond the rightBars parameter).
  • Only then considers that pivot for divergence detection.
  • This helps prevent repainting of divergence signals.


How Divergence Is Detected
The script always uses the two most recent confirmed pivots for both price and RSI. It tracks:
  • Last two swing lows in price and RSI
  • Last two swing highs in price and RSI
  • Their pivot bar indexes and values

A basic minimum distance filter between the pivots (in bars) is also applied to reduce noise.
1. Regular Bullish Divergence
Condition:
  • Price makes a lower low (LL) between the last two lows
  • RSI makes a higher low (HL) over the same two pivot lows
  • The RSI difference between the two lows is greater than or equal to the user-defined minimum (Min RSI Difference)
  • The two low pivots are separated by at least Min Bars Between Swings


Interpretation:
Some traders view this as bearish momentum weakening while price prints a new low. The script only marks this structure; it does not assume any outcome.

On the chart:
  • Drawn between the previous and current price swing lows
  • Labeled: “Regular Bullish”
  • Color: Green (by default in the script)


2. Regular Bearish Divergence
Condition:
  • Price makes a higher high (HH) between the last two highs
  • RSI makes a lower high (LH) over the same two pivot highs
  • RSI difference exceeds Min RSI Difference
  • Pivots are separated by at least Min Bars Between Swings


Interpretation:
Some traders see this as bullish momentum weakening while price prints a new high. Again, the indicator simply highlights this divergence.

On the chart:
  • Drawn between the previous and current price swing highs
  • Labeled: “Regular Bearish”
  • Color: Red


3. Hidden Bullish Divergence
Condition:
  • Price makes a higher low (HL) between the last two lows
  • RSI makes a lower low (LL) over the same two lows
  • RSI difference exceeds Min RSI Difference
  • Pivots meet the minimum distance requirement


Interpretation:
Some traders interpret hidden bullish divergence as a potential trend continuation signal within an existing uptrend. The indicator does not classify trends; it just tags the pattern when price and RSI pivots meet the conditions.

On the chart:
  • Drawn between the previous and current price swing lows
  • Labeled: “Hidden Bullish”
  • Color: Teal


4. Hidden Bearish Divergence
Condition:
  • Price makes a lower high (LH) between the last two highs
  • RSI makes a higher high (HH) over those highs
  • RSI difference exceeds Min RSI Difference
  • Pivots meet the minimum distance filter


Interpretation:
Some traders associate hidden bearish divergence with potential downtrend continuation, but again, this script only visualizes the structure.

On the chart:
  • Drawn between the previous and current price swing highs
  • Labeled: “Hidden Bearish”
  • Color: Orange


Inputs and Settings
1. RSI Settings
  • RSI Source – Price source for RSI (default: close).
  • RSI Length – Period for RSI calculation (default: 14).

These control the responsiveness of the RSI. Shorter lengths may show more frequent divergence; longer lengths smooth the signal.

2. Swing / Pivot Settings
  • Left Swing Bars (leftBars)
    *Right Swing Bars (rightBars)

These define how strict the pivot detection is:
  • Higher values → fewer, more significant swings
  • Lower values → more swings, more signals

Because the script uses ta.pivothigh / ta.pivotlow, a pivot is only confirmed once rightBars candles have closed after the candidate bar. This is an intentional design to reduce repainting and make pivots stable.

3. Divergence Filters
Min Bars Between Swings (Min Bars Between Swings)
  • Requires a minimum bar distance between the two pivots used to form divergence.
  • Helps avoid clutter from pivots that are too close to each other.


Min RSI Difference (Min RSI Difference)
  • Requires a minimum absolute difference between RSI values at the two pivots.
  • Filters out very minor changes in RSI that may not be meaningful.


4. Visibility Toggles
  • Show Regular Divergence
    *Show Hidden Divergence

You can choose to display:
  • Both regular and hidden divergence, or
  • Only regular divergence, or
  • Only hidden divergence


This is useful if you prefer to focus on one type of structure.

5. Alerts
Enable Alerts

    When enabled, the script exposes four alert conditions:
    • Regular Bullish Divergence Confirmed
    • Regular Bearish Divergence Confirmed
    • Hidden Bullish Divergence Confirmed
    • Hidden Bearish Divergence Confirmed


    Each alert fires after the corresponding divergence has been fully confirmed based on the pivot and bar confirmation logic. The script does not issue rapid or intrabar signals; it uses confirmed historical conditions.

    You can set these in the TradingView Alerts dialog by choosing this indicator and selecting the desired condition.

    Visual Elements
    On the main price chart, the indicator:
    • Draws a line between the two price pivots involved in the divergence.
    • Adds a small label at the latest pivot, describing the divergence type.
    • Colors are used to differentiate divergence categories (Green/Red/Teal/Orange).


    This makes it easy to visually scan the chart for zones where price and RSI have diverged.

    What to Look For (Analytical Use)
    • This indicator is intended as a visual helper, especially when:
    • You want to quickly see where price made new highs or lows while RSI did not confirm them in the same way.
    • You are studying momentum exhaustion, shifts, or continuation using RSI divergence as one of many tools.
    • You want to compare divergence occurrences across different timeframes or instruments.


    Important:
    • The indicator does not tell you when to enter or exit trades.
    • It does not rank or validate the “quality” of a divergence.
    • Divergence can persist or fail; it is not a guarantee of reversal or continuation.


    Many traders combine divergence analysis with:
    • Higher timeframe context
    • Trend filters (moving averages, structure)
    • Support/resistance zones or liquidity areas
    • Volume, structure breaks, or other confirmations


    Disclaimer
    This script is provided for educational and analytical purposes only.
    It does not constitute financial advice, trading advice, or investment recommendations.
    No part of this indicator is intended to suggest, encourage, or guarantee any specific trading outcome.
    Users are solely responsible for their own decisions and risk management.

    Disclaimer

    The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.