Skip to main content

Module kagi

Module kagi 

Source
Expand description

Kagi line construction (price → alternating up/down lines).

A Kagi chart discards time and draws a single connected line that keeps extending in the current direction while price advances, and reverses only when price retraces from the running extreme by at least the reversal amount H. Each completed line runs between two consecutive turning points (true price extrema — the tops/“shoulders” and bottoms/“waists”).

Reversal construction (threshold H, extrema as turning points) follows:

Bogomolov (2013), “Pairs trading based on statistical variability of the spread process”, Quantitative Finance 13(9): 1411–1430 — which formalises the Kagi (and Renko) constructions as model-free, time-independent samplers of a price path driven by a single threshold H: an up-move reverses at the first price <= running_max − H, a down-move at the first >= running_min + H.

The yin/yang thickness follows the classic charting convention (Nison, “Beyond Candlesticks”, 1994): the line turns yang (+1, thick/bullish) when price rises above the prior shoulder, and yin (-1, thin/bearish) when it falls below the prior waist; it holds its state in between. This is the machine-readable Kagi signal, kept separate from any visualization.

Two surfaces kept in parity (see the proptest):

  • streaming: KagiBuilder::next pushes one price, returns a line when the current direction reverses (else None);
  • batch: kagi_batch folds the builder over a price slice.

Structs§

KagiBuilder
Stateful Kagi builder with a fixed reversal amount H.
KagiLine
One completed Kagi line, running between two consecutive turning points. direction is +1 (up) / -1 (down); thickness is +1 (yang), -1 (yin), or 0 (undetermined — no prior shoulder/waist has been broken yet).

Functions§

kagi_atr_batch
ATR-reversal Kagi: reversal amount is multiplier * atr (a single representative ATR), matching the ATR box convention of the other bar types.
kagi_batch
Batch Kagi: fold KagiBuilder over a price slice, returning completed lines.