# Supply & Demand Zones

> Basing-candle impulse zone detection with volume boost, FVG confluence, penetration tracking, and broken-zone pruning.

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

`zones` finds institutional supply and demand bases (order blocks) that launched strong multi-candle ATR impulses, tracks retests and mitigation depth, and prunes invalidated zones once price closes through their far edge.

## Parameters

| Parameter | Default | Range | Type | Timeframe overrides |
| --- | --- | --- | --- | --- |
| `window` | `500` | `60..2000` | integer | — |
| `impulseAtr` | `2` | `0.8..8` | number | `2.5` on `<= 5m` |
| `maxBase` | `4` | `1..10` | integer | — |
| `baseBody` | `0.5` | `0.1..0.9` | number | — |
| `max` | `4` | `1..10` | integer | — |

## Algorithm & Nuances

1. **Impulse detection (`zoneScan`)**: Scans for `1–3` consecutive same-direction candles each having body ratio `|c - o| >= 0.5 * (h - l)` and achieving net move `dir * (c[k] - o[i]) >= impulseAtr * atr[i - 1]`.
2. **Base extraction**: Looks back up to `maxBase` basing candles with body ratio `|c - o| <= baseBody * (h - l)`:
   - **Demand zone**: `low = min(l)`, `high = max(o, c)` (upper body edge down to base low).
   - **Supply zone**: `high = max(h)`, `low = min(o, c)` (lower body edge up to base high).
   - **Minimum thickness**: If `high - low < 0.25 * atr[i - 1]`, widens to the full wick envelope `[min(l), max(h)]`.
3. **Confluence features**: Records `volRatio = clamp(max(v[imp..]) / medVol, 0.5, 3)` and sets `hasFvg = true` if any impulse candle left a 3-bar gap `>= 0.2 * atr[k]`.
4. **Testing & invalidation (`zoneTests`)**: Walks from `imp + len` to `n - 1`:
   - **Invalidation**: Any close breaking the far edge (`c < low` for demand, `c > high` for supply) removes the zone completely.
   - **Penetration & status**: Counts distinct re-entries (`tests`) and deepest penetration (`deepestPenetration` in `[0, 1]`). Status is `'fresh'` (`tests === 0`), `'mitigated'` (`deepestPenetration >= 0.5`), or `'tested'`.
   - Overlapping zones of the same kind replace the older zone with the newer one.
5. **Scoring & balanced selection**:
   ```
   statusMult = fresh ? 1.3 : tested ? 1.0 : 0.65
   score      = r2(impulseAtr * sqrt(volRatio) * (hasFvg ? 1.25 : 1) * statusMult
                   / (1 + distAtr / 4))
   ```
   Selects up to `ceil(max / 2)` zones below/at close and `ceil(max / 2)` above close, backfilling to `max`, sorted descending by `high`.

## Drawings & Facts

- **Drawings**: Up to `max` `zone` rectangles from `baseFrom` to the latest closed bar (`'up'` demand, `'down'` supply; `'faint'` when mitigated). Labels show `(Demand|Supply) · (fresh|N% filled|tested N×)` plus `· FVG` when paired with an imbalance.
- **Facts (`ZonesFacts`)**: `lastClose` and `zones[]` (`kind`, `low`, `high`, `baseFrom`, `baseTo`, `baseCandles`, `impulseTime`, `impulseCandles`, `impulseAtr`, `volRatio`, `hasFvg`, `tests`, `deepestPenetration`, `status`, `score`).

## Activation

- **Studies menu**: `Supply and demand zones` (`Zones`)
- **Slash command**: `/zones`
- **Chat phrases**: `zones`, `supply`, `demand`, `sd`, `s/d`, `order block`, `order blocks`
