# Fibonacci Retracements

> ATR-zigzag swing detection with Golden Pocket shading, OTE bounds, efficiency score, and optional extensions.

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

`fib` anchors Fibonacci retracement and extension levels on the most recent significant ATR-zigzag impulse leg, highlighting the `0.618–0.65` Golden Pocket and reporting premium/discount equilibrium state.

## Parameters

| Parameter | Default | Range | Type | Timeframe overrides |
| --- | --- | --- | --- | --- |
| `window` | `500` | `60..2000` | integer | — |
| `zigzagAtr` | `3` | `1..12` | number | — |
| `minLegAtr` | `5` | `1..40` | number | — |
| `extensions` | `1` | `0..1` | integer (`0` or `1`) | — |

## Algorithm & Nuances

1. **ATR zigzag (`zigzag`)**: Tracks running swing extremes and confirms a leg reversal whenever price retraces more than `atr[i] * zigzagAtr`. Tracks true opposite extremes continuously so shifting ATR cannot skip a wick extreme. Appends the active unconfirmed extreme as the latest leg.
2. **Minimum leg filter**: Walks backward from the newest leg to pick the first leg whose range satisfies `size >= minLegAtr * atr[n - 1]`. When the selected leg is unconfirmed, `facts.lastConfirmed` also exposes the most recent confirmed leg.
3. **Retracement & extension ratios**:
   - Retracements (`to + (from - to) * r`): `0.236`, `0.382`, `0.5`, `0.618`, `0.65`, `0.786`.
   - Extensions (`extensions = 1`): `-0.272` (labeled `1.272`) and `-0.618` (labeled `1.618`).
   - **Golden Pocket**: `[min(0.618, 0.65), max(0.618, 0.65)]`.
   - **OTE (Optimal Trade Entry)**: `[min(0.618, 0.786), max(0.618, 0.786)]`.
4. **Leg quality & zone state**:
   - `efficiency = min(1, size / sum(|c[j] - c[j - 1]|))` measures how straight the impulse moved (`1.0` = zero pullback noise).
   - `zone` classifies the current close as `'extended'` (`retracedNow < 0`), `'premium'` (`pos > 0.55`), `'discount'` (`pos < 0.45`), or `'equilibrium'`.

## Drawings & Facts

- **Drawings**: `0` or `1` `fib` overlay drawing the dashed swing leg, solid `0`/`1` bounds, shaded `0.618–0.65` Golden Pocket band, dashed retracement lines, and optional `1.272`/`1.618` extension lines.
- **Facts (`FibFacts`)**: `leg` (`dir`, `from`, `to`, `size`, `sizeAtr`, `bars`, `confirmed`, `efficiency`, `levels`, `extensions`, `goldenPocket`, `ote`, `retracedNow`, `zone`, `inGoldenPocket`, `inOte`) and `lastConfirmed`.

## Activation

- **Studies menu**: `Fibonacci on the last swing` (`Fib`)
- **Slash command**: `/fib`
- **Chat phrases**: `fib`, `fibs`, `fibo`, `fibonacci`, `retracement`, `retrace`
