Momentum divergence detector
Marks swings where price and its momentum input disagree; with the explainer's wiring it never fires.
The indicator looks for swing highs and lows, bars higher or lower than both neighbours, among the last divergence_window closes and the same bars of its momentum input. A bearish score (negative) means price made a higher swing high while momentum made a lower one; a bullish score (positive) is the mirror case at lows. Two weaker checks add ±1: price within 2% of its recent high (or low) while momentum sits more than 5% below its own high (or above its own low).
How to read it
- Markers sit on the candles where the score is not zero: negative is bearish, positive is bullish, and no marker means no divergence.
- A strong ±2 is drawn one bar after the swing it compares, because a swing needs the next bar to be confirmed.
- A weak ±1 is drawn on the bar it fires: price is near a recent swing extreme while momentum does not confirm it.
- On this explainer's chart no marker appears at any setting, because the momentum input is the close (see pitfalls).
What it outputs
- Divergence score
- Non-zero on bars with a divergence: -2 strong bearish, -1 weak bearish, +1 weak bullish, +2 strong bullish. Both checks can apply, so sums reach -3 or +3.
When it fires
Fires when the latest two swing highs or two swing lows disagree between price and momentum (strong ±2, one bar after the swing), or when price sits near a recent extreme while momentum does not confirm it (weak ±1, same bar). Negative is bearish, positive bullish.
Settings
- window
- Closes of history kept before the first score can appear, so raising it delays the start by that many bars. Below divergence_window it also caps how far back the swing search reaches; above it the search still reads only divergence_window bars.
- divergence_window
- How many recent closes are searched for swing highs and lows. Raising it puts more swings in view and compares older ones. Below 3 no swing fits in the search, so the indicator never fires.
Pitfalls
- The momentum input has no default, so the explainer wires it to the close. The test then compares the close with itself: zero events on the SPY sample and no marker at any setting.
- Each swing is re-added every bar it stays in the search, so the last-two test often pits a swing against itself. Synthetic test, default settings: ±2 on about 3% of bars, ±1 on about 36%.
- The docstring says -2 to +2, but both checks add up, so ±3 occurs. The try presets only change parameters, so none draws a marker on this chart.