# Anchored VWAP (AVWAP)

> Volume-weighted average price curves anchored automatically to the latest zigzag swing high, swing low, and highest-volume impulse candle.

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

`avwap` anchors cumulative volume-weighted average price curves (`curve` overlays) from the most recent major swing high, swing low, and highest-volume impulse bar, with optional volume-weighted `±1σ` standard-deviation bands.

## Parameters

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

## Algorithm & Nuances

1. **Anchor discovery (`maxStart = n - 4`)**: Anchors must have at least `4` bars to their right so curves carry meaningful sample depth.
   - **Swing High (`AVWAP H`)** and **Swing Low (`AVWAP L`)**: Latest high and latest low from `zigzag(h, l, atr, n, zigzagAtr)` (including the running extreme) with index `<= maxStart`.
   - **Impulse Volume (`AVWAP Vol`, when `includeImpulse = 1`)**: Scans `0..maxStart` for the highest-volume candle whose range satisfies `h[i] - l[i] >= 1.2 * atr[i]` (falling back to the highest-volume bar overall). Dropped if within `<= 3` bars of the Swing High or Swing Low anchor to prevent duplicate curves.
2. **West-Welford weighted math**: From each anchor index through `n - 1`, accumulates typical price `tp = (h + l + c) / 3` weighted by `v[i] > 0`:
   ```
   wSum2 = wSum + vol
   delta = tp - mean
   mean  = mean + (vol / wSum2) * delta
   m2    = m2 + vol * delta * (tp - mean)
   sd    = sqrt(m2 > 0 ? m2 / wSum : 0)
   ```
   If the anchor candle itself has zero volume, the reported anchor timestamp and bar count advance to the first positive-volume bar (`startIdx`).
3. **`±1σ` band clamping & decimation**:
   - When `showBands = 1`, upper (`mean + sd`) and lower (`mean - sd`) points clamp to `[spanLo, spanHi]` (the min low / max high across `start..n - 1`) so volatile outlier bands never breach chart bounds.
   - Curves exceeding `MAX_CURVE_POINTS = 400` points decimate uniformly while preserving exact first/last points and strictly increasing timestamps.
4. **Pinch / position state**: Compares `lastClose` against the end values of `AVWAP H` and `AVWAP L`: `'above_both'`, `'below_both'`, `'between'`, `'single'`, or `'none'`.

## Drawings & Facts

- **Drawings**: Up to `3` primary `curve` polylines — `AVWAP H` (solid `'down'`), `AVWAP L` (solid `'up'`), and `AVWAP Vol` (dashed `'accent'`). When `showBands = 1`, adds `2` faint dashed `±1σ` curves per anchor (up to `9` curves total). Rendered viewport-culled (`[-32, width + 32]` px).
- **Facts (`AvwapFacts`)**: `lastClose`, `anchors[]` (`kind`, `anchor`, `bars`, `priceAtEnd`, `stdAtEnd`, `distanceAtr`, `side`), and `position`.

## Activation

- **Studies menu**: `Anchored VWAP: swing highs, lows and volume` (`AVWAP`)
- **Slash command**: `/avwap`
- **Chat phrases**: `avwap`, `anchored`, `anchored vwap`
