# Market Structure

> Dual-scale internal and external swing labeling, causal BOS and CHoCH detection, and dealing range equilibrium.

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

`structure` tracks alternating swing highs and lows across both internal and external scales, marks causal Break of Structure (`BOS`) and Change of Character (`CHoCH`) events, and plots the active dealing range equilibrium (`EQ`).

## Parameters

| Parameter | Default | Range | Type | Timeframe overrides |
| --- | --- | --- | --- | --- |
| `window` | `500` | `60..2000` | integer | — |
| `pivotSpan` | `3` | `2..10` | integer | `4` on `<= 5m`, `2` on `>= 1d` |
| `maxLabels` | `8` | `0..30` | integer | — |
| `maxEvents` | `3` | `0..10` | integer | — |

## Algorithm & Nuances

1. **Dual-scale swings**:
   - **Internal structure**: `fractalPivots` at `pivotSpan`.
   - **External structure**: `fractalPivots` at `majorSpan = min(10, max(pivotSpan * 2, 5))`.
   - Both pass through `alternate()`, keeping the more extreme pivot on consecutive same-side swings (earlier on tie) and tagging each point `H`, `L`, `HH`, `LH`, `EH`, `HL`, `LL`, or `EL`.
2. **Causal `BOS` / `CHoCH` (`breakScan`)**:
   - A fractal swing at index `idx` becomes active only once `k >= idx + span` (after right-side confirmation bars close), preventing lookahead breaks.
   - A candle closing above the active unbroken swing high triggers an upward break (`dir = +1`); a close below the active unbroken swing low triggers a downward break (`dir = -1`).
   - Breaks opposing the prevailing direction label **`CHoCH`** (Change of Character); breaks continuing the direction label **`BOS`** (Break of Structure). Each event records `displacementAtr` and break candle `bodyRatio`.
3. **Trend & dealing range**:
   - `facts.trend` reflects external direction (falling back to internal if external is `'none'`); `facts.internalTrend` exposes internal direction separately.
   - **Dealing range**: Derived from the latest confirmed external swing high and swing low (falling back to internal if fewer than `2` external swings exist). `equilibrium = (high + low) / 2`; `zone` is `'premium'` (`pct > 0.55`), `'discount'` (`pct < 0.45`), or `'equilibrium'`.

## Drawings & Facts

- **Drawings**:
  - Up to `maxLabels` `mark` labels on recent internal swings (`HH`/`HL` `'up'`, `LH`/`LL` `'down'`, `H`/`L`/`EH`/`EL` `'neutral'`).
  - Up to `maxEvents` horizontal `trendline` segments from `swingTime` to `breakTime` (solid for `BOS`, dashed for `CHoCH`).
  - `1` neutral `level` line at dealing-range `EQ` (`EQ (premium|discount|equilibrium)`).
- **Facts (`StructureFacts`)**: `trend`, `internalTrend`, `swings[]`, `events[]`, and `dealingRange`.

## Activation

- **Studies menu**: `Market structure: swings, BOS, CHoCH` (`Structure`)
- **Slash command**: `/structure`
- **Chat phrases**: `structure`, `ms`, `bos`, `choch`, `swings`, `hh`, `hl`, `lh`, `ll`
