Kama

Incremental, causal technical analysis documentation

Summary

Kama is RTTA's streaming Kaufman Adaptive Moving Average. It speeds up for an efficient directional path and slows down when the same net movement contains more back-and-forth noise.

Update API

result = rtta.Kama(
    window=10, fast_ema=2, slow_ema=30, fillna=True
).update(close)

advance(...) consumes the same close without materializing a Python return value. batch(...) applies the identical state transition to an array.

Theory Of Operation

KAMA uses Kaufman's efficiency ratio (net displacement divided by path length) to interpolate between fast and slow EMA constants. Squaring the interpolated constant makes the average especially resistant to choppy price noise while remaining responsive to a clean trend.

Recurrence

For efficiency window \(n\), fast period \(f\), and slow period \(s\):

\[ER_t=\frac{|c_t-c_{t-n}|} {\sum_{i=0}^{n-1}|c_{t-i}-c_{t-i-1}|}\]
\[\alpha_f=\frac{2}{f+1},\qquad \alpha_s=\frac{2}{s+1}\]
\[SC_t=\left[ER_t(\alpha_f-\alpha_s)+\alpha_s\right]^2\]
\[KAMA_t=KAMA_{t-1}+SC_t(c_t-KAMA_{t-1}).\]

The calculation needs \(n+1\) closes to obtain \(n\) real one-step changes. Until then, price is retained as the KAMA seed. fillna=True returns that price during startup; fillna=False returns NaN. The first complete value is thus emitted on update \(n+1\), matching TA-Lib's canonical sequence.

Composed Primitives

EfficiencyRatio

Implementation Notes

The recurrence is implemented in src/rtta/indicator.cpp in class Kama. Startup changes are computed only between observed closes; no synthetic zero price is inserted into the path length.

Reference