Divergence Stack Scanner [AGPro Series]Divergence Stack Scanner
🔹 Overview
Divergence Stack Scanner scans five independent momentum and volume oscillators simultaneously — RSI, MACD Histogram, CCI, MFI, and OBV — and grades every confirmed pivot by how many of them show regular divergence at the same time. The result is a 0 to 5 STACK score that isolates rare, high-conviction reversal zones which single-divergence tools simply cannot surface.
Most divergence indicators track one oscillator at a time. Strong reversals, however, tend to leave fingerprints across momentum and volume at once. This tool quantifies that confluence in a single, objective number you can act on.
🔸 Unique Edge
What separates this script from the crowded divergence space:
1. Five-oscillator confluence in one engine — not one divergence, five divergences graded together.
2. Strict versus Window classification — a 5/5 STRICT stack (all five on the exact same pivot) is marked with a star flash, while a 5/5 WINDOW stack (all five within a small bar tolerance) is tagged separately. The distinction matters because strict stacks are statistically rarer and sharper.
3. Pressure Zones — a rectangular, SR-style zone is drawn forward from each qualifying pivot, so the reversal level stays visible long after the signal fires.
4. Rolling win-rate panel — the last N decided signals are evaluated N bars forward, and the win percentage is displayed live on the panel.
5. Per-oscillator live state — you see at a glance which oscillators are already in divergence and how long ago they triggered.
🔹 Methodology
Pivots are detected using the standard TradingView pivot method with a configurable lookback (default 5 bars each side, confirmed, non-repainting).
When a pivot is confirmed, the script compares the current pivot price and oscillator value to the previous same-direction pivot:
- Regular bullish divergence: price prints a lower low while the oscillator prints a higher low.
- Regular bearish divergence: price prints a higher high while the oscillator prints a lower high.
This comparison runs independently for RSI, MACD Histogram, CCI, MFI, and OBV. Each oscillator stores the bar index of its most recent divergence. The stack engine then counts how many oscillators have fired within a tolerance window of the current pivot:
- Strict stack (window = 0): all counted oscillators fired on the exact same pivot bar.
- Window stack (window = 1 to N bars): oscillators fired within N bars of each other.
Pressure zones are drawn only when the stack meets a configurable minimum (default 4/5). Zone height is ATR-scaled so it stays proportional across instruments and timeframes. A proximity filter prevents label clutter: once a label is drawn in a given direction, a new label in the same direction within a short window is only drawn if its stack level is strictly higher.
🔸 Signals & Alerts
On-chart signals:
- ★ N/5 STACK label (filled color) — strict same-bar stack at level N.
- N/5 NEAR label (lighter color) — window stack (near-miss of strict).
- Pressure zone rectangle — drawn forward from the pivot for qualifying stacks.
Alerts available:
- 5/5 Strict Stack (bull and bear, separate) — the rarest and sharpest signal.
- Minimum Stack threshold — fires whenever the stack reaches your configured minimum level.
All alerts use alert.freq_once_per_bar_close and include ticker plus timeframe in the message.
🔹 Key Inputs
Core Engine:
- Pivot Lookback — bars each side to confirm a pivot (default 5).
- RSI, MACD, CCI, MFI lengths — standard defaults, all configurable.
Stack Configuration:
- Minimum Stack Level — display threshold (default 3/5).
- Window Tolerance — bar tolerance for near-stacks (default 3).
- Win-Rate Lookback — number of recent signals used for rolling win rate (default 20).
- Win Evaluation Bars — forward bars to decide win or loss (default 10).
Pressure Zones:
- Minimum Stack for Zone (default 4/5) — keeps the chart premium and uncluttered.
- Zone Extend and Height (ATR%) — tune the visual footprint to your taste.
- Max Active Zones — oldest zones are automatically trimmed.
Panel and Theme:
- Location (6 anchors), Dark or Light theme, font size presets.
- Fully brand-consistent AGPro color palette built in.
🔸 How to Use
- Treat 5/5 STRICT stacks as the headline signal. They are rare by construction and typically appear at genuine inflection points.
- Use 4/5 stacks as early-warning context around support, resistance, or higher-timeframe levels.
- Read the per-oscillator live state on the panel. When RSI, MACD, CCI, MFI are all in the same direction and OBV is the last holdout, a full stack is often imminent.
- Pressure zones work well as re-entry or invalidation levels after the initial signal fires.
- The rolling win rate is a sanity check for the current asset and timeframe — if it degrades meaningfully, raise the minimum stack level or widen the pivot lookback.
🔹 Limitations and Transparency
- This is a confluence tool, not a standalone trading system. Divergence by nature can persist in strong trends before any reversal.
- The engine is pivot-based and therefore delayed by the pivot lookback. Labels appear on the bar the pivot is confirmed, not on the pivot itself.
- Win rate is computed on the last N decided signals on the current chart. It is not a backtest, it is an evolving statistic and it does not include slippage, spread, or position sizing.
- Window stacks are lower-confidence than strict stacks by design. The visual distinction is intentional.
- Session-based "Today's Max" uses calendar-day rollover.
🔸 Risk Disclosure
This indicator is a technical analysis tool for research and education. It is not financial advice, not a signal service, and not a trading strategy. Past patterns do not guarantee future behavior. You are solely responsible for your trading decisions, risk management, and position sizing. Always combine any indicator with independent analysis, higher-timeframe context, and strict risk controls.
🔹 Technical Notes
- Pine Script v6, overlay indicator.
- Fully non-repainting. All divergences are evaluated only on confirmed pivots.
- Drawing objects (labels, boxes) are capped to avoid resource overruns.
- MPL 2.0 licensed — open source.
Pine Script® Indikator






















