# Windowing & Scope

> How Fixed vs View scope selects which closed candles each study reads and why VPVR stays view-adaptive.

Canonical page: [https://docs.kami.trade/engine/windowing/](https://docs.kami.trade/engine/windowing/)

By default, Kami pins structural studies to a fixed trailing lookback window so zooming or panning the chart does not shift your support/resistance levels, trendlines, zones, or market structure. You can switch to `View` scope at any time from the chart legend.

## Constants & Bounds

| Constant | Value | Role |
| --- | --- | --- |
| `WINDOW` default | `500` | Default trailing closed candles in `fixed` scope (valid range `60..2000`) |
| `MIN_CANDLES` | `30` | Minimum closed candles required to run any study |
| `EXTEND_TO` | `60` | Minimum bar count `view` mode widens to when fewer than `30` bars are visible |
| `MAX_VIEW` | `2000` | Maximum closed candles scanned in `view` mode |
| `WARM_UP` | `50` | Extra bars prepended before `from` in `view` mode to seed Wilder's `ATR(14)` |

If fewer than `30` closed candles are loaded on the chart, studies throw a `StudyError` instead of drawing low-confidence noise.

## Fixed vs. View Scope

The scope setting is persisted per browser in `terminal.taScope` (`'fixed' | 'view'`, default `'fixed'`) and toggled directly from the **`Fixed` / `View`** button in the chart legend (`aria-label="Follow chart zoom"`).

The engine decides whether a study reads the visible viewport or a pinned trailing slice using:

```ts
useView =
  Boolean(input.view) &&
  given?.window === undefined &&
  (input.scope !== 'fixed' || VIEW_ADAPTIVE.has(name));
```

| Mode | Which studies use it | Candles read | Legend display |
| --- | --- | --- | --- |
| **`fixed` (default)** | All studies except `vpvr` | Last `min(window, closed)` closed candles (`from = closed - count`, `to = closed`, `warmUp = 0`) | `pinned 500` (or `pinned 500 (N closed)` if fewer than 500 loaded) |
| **`view` (or `vpvr`)** | `vpvr` always (unless explicit `window` passed); all studies when scope is `'view'` | Visible closed candles `[from, to)`, with `warmUp = min(50, from)` | `view N candles`, `extended to N`, or `capped at 2000` |

### Why `vpvr` stays View-Adaptive

Volume Profile Visible Range (`vpvr`) is explicitly defined over the range currently on screen (`VIEW_ADAPTIVE = new Set(['vpvr'])`). Even when chart scope is `Fixed`, panning or zooming recalculates POC, VAH, and VAL over the visible candles. Passing an explicit `window` parameter (for example via Kami AI tools) pins `vpvr` like any other study.

## View-Mode Edge Handling

When `useView` is `true`, `viewWindow(closed, from, to)` handles extreme zooms deterministically:

1. **Zoomed in (`visible < 30`)**: Widens the slice to `min(60, closed)` bars, extending left into older history first and right only if history runs out (`basis.window.fit = 'extended'`).
2. **Normal (`30 <= visible <= 2000`)**: Uses the exact visible closed bars (`basis.window.fit = 'view'`).
3. **Zoomed way out (`visible > 2000`)**: Caps to the newest `2000` closed bars inside the viewport (`basis.window.fit = 'capped'`).

In all view-mode cases, up to `50` prior candles are prepended strictly for `ATR(14)` warm-up and sliced away before any study executes, preventing wild ATR artifacts on the left edge of the screen.

## Explicit `window` Override

Passing a numeric `window` parameter (`60..2000`) always forces `pinned` mode for that call regardless of the UI's `Fixed` / `View` toggle. Conversely, when a study runs in `view` mode, `window` is omitted from `StudyResult.params` because it did not govern the bar slice.
