# Divergences

> Regular and hidden bullish/bearish RSI and MACD histogram divergences with clean-path validation and dual-oscillator confluence.

Canonical page: [https://docs.kami.trade/studies/divergence/](https://docs.kami.trade/studies/divergence/)

`divergence` detects regular (reversal) and hidden (trend-continuation) divergences between price fractal pivots and both `RSI` and `MACD` histogram, boosting pairs confirmed by both oscillators simultaneously.

## Parameters

| Parameter | Default | Range | Type | Timeframe overrides |
| --- | --- | --- | --- | --- |
| `window` | `500` | `60..2000` | integer | — |
| `pivotSpan` | `3` | `2..10` | integer | `4` on `<= 5m`, `2` on `>= 1d` |
| `rsiPeriod` | `14` | `5..30` | integer | — |
| `includeHidden` | `1` | `0..1` | integer (`0` or `1`) | — |
| `max` | `4` | `1..8` | integer | — |

## Algorithm & Nuances

1. **Pivot pair window**: Computes `rsi(c, rsiPeriod)` and `macd(c, 12, 26, 9)` (histogram). Inspects the latest `18` swing lows and latest `18` swing highs, testing each pivot `b` against up to `4` preceding same-side pivots `a` within `5 <= b - a <= 80` bars.
2. **Clean-path check**: Rejects any pair `(a, b)` where an intervening candle `k in (a, b)` pierces past the pair's extreme by more than `0.25 * atr[k]`, ensuring diagonal divergence segments never cut across intermediate price action.
3. **RSI qualification**:
   - **`regular_bull`**: Lower low (`l[b] < l[a]`), higher RSI (`rb > ra + 2`), first pivot oversold (`ra <= 45`, extra `+0.6` boost if `ra <= 35`).
   - **`hidden_bull`**: Higher low (`l[b] > l[a]`), lower RSI (`rb < ra - 2`, `rb <= 48`).
   - **`regular_bear`**: Higher high (`h[b] > h[a]`), lower RSI (`rb < ra - 2`), first pivot overbought (`ra >= 55`, extra `+0.6` boost if `ra >= 65`).
   - **`hidden_bear`**: Lower high (`h[b] < h[a]`), higher RSI (`rb > ra + 2`, `rb >= 52`).
4. **MACD histogram qualification**:
   - **`regular_bull`**: Lower low, `ma < 0`, `mb > ma`.
   - **`hidden_bull`**: Higher low, `mb < ma`, `mb < 0`.
   - **`regular_bear`**: Higher high, `ma > 0`, `mb < ma`.
   - **`hidden_bear`**: Lower high, `mb > ma`, `mb > 0`.
5. **Dual confluence & scoring**: When both RSI and MACD confirm the same `(kind, a, b)`, they merge into one candidate with a `1.4×` boost:
   ```
   score = r2(strength * (dual ? 1.4 : 1) * (0.5 + 0.5 * (b / (n - 1)))
              * (regular ? 1.15 : 0.9))
   ```
   Deduplicates per `(direction, secondPivot)` keeping the highest score, then returns the `max` most recent divergences ordered chronologically.

## Drawings & Facts

- **Drawings**: For each divergence (up to `max`), `1` dashed segment `trendline` connecting the two price pivots plus `1` `mark` at the second pivot (`Bull Div`, `Bear Div`, `Hid Bull`, `Hid Bear` with `RSI`, `MACD`, or `RSI+MACD`).
- **Facts (`DivergenceFacts`)**: `lastClose` and `divergences[]` (`kind`, `sources`, `fromTime`, `toTime`, `fromPrice`, `toPrice`, `fromRsi`, `toRsi`, `spanBars`, `score`).

## Activation

- **Studies menu**: `Divergences: RSI and MACD` (`Div`)
- **Slash command**: `/divergence`
- **Chat phrases**: `divergence`, `divergences`, `div`, `divs`
