OPEN-SOURCE SCRIPT

Micro and Macro RSI Divergence

4 013
█ OVERVIEW
Micro and Macro RSI Divergence is a multi-scale system for detecting divergences between price and RSI, designed for simultaneous analysis of short-term market structure (micro) and the broader trend context (macro).

The indicator does not limit itself to a simple price vs RSI comparison. It utilizes pivot structure, advanced signal quality filtering, and an additional layer of smoothed RSI, which allows it to effectively distinguish high-quality divergences from random market noise.

Each detected divergence is additionally verified in terms of structural integrity, distance between pivots, and momentum consistency. This enables significantly more selective and contextual market readings compared to classic divergence indicators.

█ CONCEPTS
In standard indicators, divergences are typically detected using a single, fixed pivot length. This causes signals to often be either delayed relative to the actual price movement or to react to very small, local fluctuations that have no greater structural significance.

This indicator was created precisely to solve this problem by introducing multi-scale pivot analysis. This makes it possible to simultaneously compare small, local divergences (micro) with larger movement structures (macro).

This allows for a better assessment of whether a local momentum change:
- is merely a short-term correction within an existing trend,
- or an element of a larger, structural change in market direction.
As a result, the user receives not only information about the divergence itself, but also an assessment of its real significance in the broader market context and the potential strength of the price direction change.

Divergence Types
- Regular Bullish - price forms a lower low, RSI forms a higher low
- Regular Bearish - price forms a higher high, RSI forms a lower high
- Hidden Bullish - higher low in price with weaker RSI (continuation of uptrend)
- Hidden Bearish - lower high in price with stronger RSI (continuation of downtrend)

Micro vs Macro Logic
Micro divergences are more reactive and generate a greater number of signals. In contrast, macro divergences filter the broader market structure and represent stronger, more significant turning points.
Both layers operate independently, but their confluence significantly enhances the quality of market interpretation.

█ FEATURES
RSI Engine
- RSI calculation based on the selected source (default close)
- Configurable RSI length
- Overbought/oversold levels (70/30)

Smoothed RSI Layer
- Optional smoothed RSI layer
- Available smoothing methods: SMA, EMA, RMA, WMA, HMA
- Serves as an additional confirmation layer and noise reduction

Divergence Engine (Micro + Macro)
- Detection system in two scales simultaneously
- Independent calculations for micro and macro
- Micro Pivot Length - for local structure analysis
- Macro Pivot Length - for higher-order structure analysis

Pivot Detection System
- Pivots based on High/Low or Close
- Configurable left/right pivot length
- Minimum and maximum distance restrictions between pivots
- Noise reduction and excessive signal count reduction

Divergence Types Detection
- Regular bullish and bearish divergences
- Hidden bullish and bearish divergences
- Separate logic for micro and macro scales

Structure Validation Engine
The indicator includes advanced structural integrity validation, known as line break check.
This validation checks whether the line connecting two consecutive pivots has not been broken by price movement (in High/Low mode) or by the RSI indicator itself (in oscillator mode). This allows rejection of divergences whose structure has been damaged by subsequent movement - which significantly improves signal quality.

Important limitation:
Line break validation operates exclusively in two cases:
- When pivot detection is based on High/Low (i.e., on wicks),
- When the indicator operates in oscillator mode (pivots calculated directly on RSI values).
In Close mode (when pivots are calculated on closing prices) line break validation is not applied, because the line connecting two consecutive closes does not have structural significance in this case and cannot be reliably broken.

Filtering System
- RSI alignment filter relative to the 50 level
- Smoothed RSI alignment filter relative to the 50 level
- RSI momentum direction filter
- Smoothed RSI momentum direction filter
- Minimum RSI value difference between pivots
- Pivot distance restrictions

Visualization System
The indicator offers a flexible visualization system that works in two modes:
- Overlay mode - draws divergence lines directly on the price chart
- Oscillator mode - draws divergence lines in the RSI panel

Additional cross-drawing options are available:
- When the indicator runs in the RSI panel, you can enable simultaneous drawing on the price chart
- When the indicator runs on the price chart, you can enable simultaneous drawing in the RSI panel

Available visual elements:
- Divergence lines between pivots
- Text labels (Bull / Bear / hBull / hBear and macro versions: BullM, BearM, hBullM, hBearM)
- Separation of micro and macro
- Shape markers indicating divergence detection points: circles (micro) and diamonds (macro)

Alert System
- Alerts for all types of divergences
- Separate alerts for micro and macro scales
- Separate alerts for regular and hidden divergences

█ APPLICATIONS
1. Trend Weakness Context
Regular divergences are not an entry signal, but information about the weakening of the current move.
Example:
You have an open long position and a bearish divergence appears - momentum is losing strength. You start paying more attention to potential resistance levels, profit-taking zones, and possible changes in market structure.

2. Trend Continuation Context
Hidden divergences may indicate trend continuation after a correction.
Example:
In an uptrend, a hidden bullish divergence appears - the correction may be exhausting. You do not enter automatically, but check support levels, trend structure, and additional momentum confirmations.

3. Support & Resistance Context
Divergences have the greatest value when they appear near key levels.
- Bearish divergence near resistance - greater risk of price rejection
- Bullish divergence near support - potential upward reaction
Important: Divergence by itself is not an entry signal - it provides context for analyzing levels and market structure.

4. Exit & Risk Management
Divergences can help with:
- Earlier position closing
- Profit protection
- Identification of the final phase of the impulse

█ NOTES
Divergences are detected with a delay resulting directly from the pivot length - the larger the pivot, the greater the detection delay (equal to its length).

The indicator is a contextual tool, not a signal tool. It should be treated as support for the decision-making process, not as a standalone system generating entry signals.

By default, divergence detection points are marked on the chart as follows:
- Micro divergences - circles
- Macro divergences - diamonds

The indicator also allows simultaneous display of divergence lines both on the price chart and in the RSI panel (cross-drawing function), which provides full flexibility depending on the preferred analysis style.

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.