Confirmed Divergence Engine v4.0 - Any Oscillator

511
CONFIRMED DIVERGENCE ENGINE v4.0 · Any Oscillator

WHAT THIS DOES

This is a divergence detector built around one rule: a signal prints exactly once, at the close of its confirmation bar, and can never move, repaint, or vanish. It finds regular (reversal) and hidden (continuation) divergences between price and virtually any oscillator, then forces every candidate through an eight-stage quality pipeline before anything reaches your chart. Most candidates die in the pipeline. What survives is scored 0–100, and only the best-scoring pair is drawn.

The indicator sits on the price chart (overlay). Divergence lines and labels are drawn on price; the oscillator itself is computed internally and is not plotted. If you want to see the oscillator, add it as its own pane — or run this engine in External mode directly against any oscillator already on your chart.

QUICK START


Add the indicator to any symbol and timeframe. Defaults = RSI(14), regular divergences only, adaptive zone filter.
Wait for a small triangle above or below a bar. That triangle is the signal bar — the bar where the divergence was confirmed on close.
Hover the R/H label at the pivot to see the full quality breakdown (score, oscillator delta, swing size, span, percentile rank).
Optional: create an alert on the indicator with trigger "Once Per Bar Close" (see ALERTS below).

THE NON-REPAINTING MODEL — READ THIS FIRST

A pivot only exists after "Pivot right bars" fully CLOSED bars have printed beyond it, and all detection runs strictly on the closing update of the bar. Nothing is evaluated intrabar, so nothing can flicker in and out, and a signal that appears is permanent.
The honest consequence: every signal carries a fixed structural delay of "Pivot right bars" bars (default 1). Zero-lag and non-repainting pivots cannot coexist in the same tool — any divergence indicator that shows you signals "at the pivot" in real time is repainting.
Each signal draws two things: – a dotted (regular) or dashed (hidden) line + label, placed back at the pivot for readability; – a small triangle on the bar where the signal actually FIRED (pivot bar + right bars).

Act on the triangle. Backtest on the triangle. When you scroll back through history the lines will look earlier than they were tradeable — that is cosmetic. The triangle is the truth.

REGULAR vs HIDDEN

Regular bullish: price makes a lower low, oscillator makes a higher low → potential reversal up.
Regular bearish: price makes a higher high, oscillator makes a lower high → potential reversal down.
Hidden bullish: price makes a higher low, oscillator makes a lower low → potential continuation up.
Hidden bearish: price makes a lower high, oscillator makes a higher high → potential continuation down.

Regular divergences are on by default; hidden are off. Hidden divergences bypass the overbought/oversold zone filter by design (they occur mid-trend, not at extremes) but still have to pass every other gate.

THE QUALITY PIPELINE

Every freshly confirmed pivot (P2) is tested against up to 20 stored earlier pivots (P1). A pair must pass ALL of the following before it can print:

  • Classification — strict price/oscillator inequalities for regular or hidden, bullish or bearish.
  • Span window — P1 and P2 must be between the min and max bar distance apart (default 5–60).
  • Oscillator magnitude — the divergence must be at least X% of the oscillator's own recent range (default 3% over a 200-bar lookback). Scale-free, so it behaves identically on RSI and on OBV.
  • Price separation (optional) — minimum distance between the two pivot prices, in ATR units. Off by default.
  • Zone filter (regular divs only) — P1 must sit in an oscillator extreme. – Adaptive percentile (default): P1 must rank in the outer 5% of the oscillator's recent values, P2 in the outer 25%. Works on any oscillator, bounded or not. – Fixed levels: classic thresholds (30/70 etc.) for 0–100 style oscillators. – Off.
  • Clean line-of-sight — no bar between the pivots may pierce the straight P1→P2 connecting line, on price OR on the oscillator. This is a true tangency test: "divergences" that price has already cut through are rejected.
  • Swing prominence — price must have bounced at least X·ATR away between the two pivots (default 0.3). This kills flat, drifting pivot pairs that only look like divergence on the oscillator.
  • Composite quality score (0–100) — weighted blend of divergence size, zone depth, swing quality, and price separation. Anything below "Min quality score" (default 55) is discarded. If several P1s survive, only the single best-scoring one is drawn (or the most recent valid one — your choice)
.

READING THE CHART

Dotted line = regular divergence. Dashed line = hidden.
Label at the pivot: "R" or "H" plus the quality score. Hover it for the full breakdown — score, oscillator delta as % of range, swing size in ATR, span in bars, and P1 percentile rank.
Triangle at the confirmation bar: green/red for regular bull/bear, teal/orange for hidden bull/bear. This is the actionable bar.

USING ANY OSCILLATOR (EXTERNAL MODE)

Eight engines are built in: RSI, Stochastic %K (smoothed), MACD histogram, CCI, MFI, Rate of Change, Williams %R, and OBV.

To run the engine on anything else:

Add your oscillator to the chart as its own indicator.
In this script's settings, set Oscillator = External.
Under "External oscillator plot", select the plot from your other indicator.

The engine treats it exactly like a native oscillator. For unbounded inputs (MACD histogram, OBV, volume deltas, custom composites), keep the zone filter on Adaptive percentile — fixed levels assume a 0–100 scale and will silently block signals otherwise.


SETTINGS GUIDE

1 · OSCILLATOR — engine selection. "Source" applies to RSI/ROC only; "Length" to everything except MACD and OBV; MACD has its own 12/26/9 inputs.

2 · PIVOT STRUCTURE — "Pivot right bars" is the single most important input in the script: it is both your pivot reliability and your confirmation delay. 1 = fastest signals, noisier pivots; 2–3 = sturdier pivots, later signals. Min/Max span controls how far apart the two pivots may be; max span also caps every historical lookup, which is what keeps the script runtime-safe on any symbol and timeframe.

3 · DIVERGENCE TYPES — toggle regular and hidden independently.

4 · QUALITY FILTERS — the pipeline gates described above: zone mode and percentiles, line-of-sight toggle, minimum oscillator divergence, minimum price separation, minimum swing, minimum score, best-vs-most-recent selection, and the normalisation/ATR lookbacks.

5 · DISPLAY — lines, labels, score-in-label, confirmation triangles, and the four signal colours.

ALERTS Create an alert on the chart → Condition = this indicator → choose one of:

  • Regular Bullish Divergence
  • Regular Bearish Divergence
  • Hidden Bullish Divergence
  • Hidden Bearish Divergence
  • Any Divergence


Set the trigger to "Once Per Bar Close". Detection is already gated on bar confirmation internally, so alerts can never fire on an unconfirmed intrabar state. There is also a dynamic alert() message (use "Any alert() function call") that includes the ticker, timeframe, divergence type, and score.

TUNING RECIPES
Too few signals → lower Min quality score to 40–45, widen "P1 in outer %" to 10–15, enable hidden divergences, or reduce Min swing to 0.1–0.2 ATR.
Too many / low quality → raise Min score to 65+, raise right bars to 2–3, tighten P1 percentile to 3, or raise the minimum oscillator divergence.
Higher timeframes / swing trading → widen Max span (100–150) and the normalisation lookback (300+) so "recent range" reflects the regime you trade.
MACD histogram, OBV, or any unbounded external plot → keep the zone filter on Adaptive.


WHAT THIS IS NOT
Divergence is exhaustion context, not a trading system. This tool tells you that momentum and price disagreed at a confirmed structural point, with a measurable quality score — it does not predict reversals, and no filter stack can make a counter-trend signal safe on its own. Combine it with market structure, levels, and higher-timeframe context, and size accordingly. The confirmation delay is deliberate: it is the price of signals you can actually trust in a backtest.

Pernyataan Penyangkalan

Informasi dan publikasi ini tidak dimaksudkan, dan bukan merupakan, saran atau rekomendasi keuangan, investasi, trading, atau jenis lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Ketentuan Penggunaan.