fintech-algorithms

Kaufman Adaptive Moving Average (KAMA)

Install and import

bash
npm install fintech-algorithms
ts
import { calculateKama } from "fintech-algorithms/technical-indicators/trend-smoothing/kama";

Signature

calculateKama(values, efficiencyPeriod, fastPeriod, slowPeriod)

Kaufman adaptive moving average. The smoothing constant moves between a fast and a slow bound according to an efficiency ratio — directional travel divided by total travel — so the average tightens in a trend and loosens in noise.

Parameters

NameTypeNotes
values(number | null)[]Observation series in chronological order, oldest first.
nulls: propagate
efficiencyPeriodnumberLookback over which the efficiency ratio is measured.
min: 1 · integer: true
fastPeriodnumberPeriod defining the fast bound of the smoothing constant; reached when the series is perfectly directional.
min: 1 · integer: true
slowPeriodnumberPeriod defining the slow bound; reached when the series is pure noise.
min: 1 · integer: true

Returns

Record<string, (number | null)[]> · length same-as-input

Parallel series: efficiencyRatio, blendedAlpha, smoothingConstant and kama, so the adaptation itself is inspectable rather than hidden inside the result.

Warm-up

The first efficiencyPeriod positions are null. The efficiency ratio needs a full lookback before the constant is defined.

Errors

  • When any period is < 1, is not an integer, or fastPeriod ≥ slowPeriod — throws RangeError

Complexity: time O(n), space O(n).

Worked example

executed Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

Input

values
[100, 101, 102, 103, 104, 103]

Showing 6 of 16 elements.

efficiencyPeriod
10
fastPeriod
2
slowPeriod
30

Call

calculateKama(values, efficiencyPeriod, fastPeriod, slowPeriod)

Returns

object with 4 fields: efficiencyRatio, blendedAlpha, smoothingConstant, kama

{
  "efficiencyRatio": [null, null, null, null, null, null],
  "blendedAlpha": [null, null, null, null, null, null],
  "smoothingConstant": [null, null, null, null, null, null],
  "kama": [null, null, null, null, null, null]
}

Diagrams

Kaufman Adaptive Moving Average (KAMA) — adaptive smoother choice
Kaufman Adaptive Moving Average (KAMA) — calculation pipeline
Kaufman Adaptive Moving Average (KAMA) — data integrity
Kaufman Adaptive Moving Average (KAMA) — path efficiency
Kaufman Adaptive Moving Average (KAMA) — regime dashboard
Kaufman Adaptive Moving Average (KAMA) — response curve
Kaufman Adaptive Moving Average (KAMA) — seed timeline

Calculation flow

KAMA calculation flow
flowchart LR
    A["Validated, ordered prices"] --> B{"At least E + 1 observations?"}
    B -- "No" --> C["Return not ready"]
    B -- "Yes" --> D["Change: absolute E-bar displacement"]
    D --> E["Path length: sum of absolute one-bar moves"]
    E --> F{"Path length is zero?"}
    F -- "Yes" --> G["Efficiency ratio = 1"]
    F -- "No" --> H["Efficiency ratio = change / path"]
    G --> I["Blend fast and slow alpha"]
    H --> I
    I --> J["Square alpha to obtain SC"]
    J --> K["Update from previous KAMA toward current price"]
    K --> L["Publish ER, SC, and KAMA"]

How it works

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References