fintech-algorithms

MESA Adaptive Moving Average (MAMA)

Install and import

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

Signature

mama(values, fastLimit, slowLimit)

MESA adaptive moving average. A Hilbert transform estimates the dominant cycle period of the series, and the smoothing rate follows the rate of phase change — so the average adapts to cycle length rather than to a fixed window.

Parameters

NameTypeNotes
values(number | null)[]Observation series in chronological order, oldest first.
nulls: propagate
fastLimitnumberUpper bound on the adaptive smoothing factor.
min: 0 · max: 1
optional
slowLimitnumberLower bound on the adaptive smoothing factor.
min: 0 · max: 1
optional

Returns

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

Every stage of the transform as a parallel series — smooth, detrender, the in-phase and quadrature components, period, phase, mama and fama — because the intermediate values are the only way to diagnose a suspicious result.

Warm-up

The first 6 positions are null. The Hilbert transform needs six bars of history before its output is meaningful.

Errors

  • When fastLimit or slowLimit falls outside 0…1, or slowLimit > fastLimit — 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, 100.517638, 101, 101.414214, 101.732051, 101.931852]

Showing 6 of 144 elements.

Call

mama(values, fastLimit, slowLimit)

Returns

object with 14 fields: smooth, detrender, i1, q1, i2, q2, re, im, …

{
  "smooth": [null, null, null, 100.96921320000001, 101.3688484, 101.67519890000001],
  "detrender": [null, null, null, null, null, null],
  "i1": [null, null, null, null, null, null],
  "q1": [null, null, null, null, null, null],
  "i2": [null, null, null, null, null, null],
  "q2": [null, null, null, null, null, null],
  "re": [null, null, null, null, null, null],
  "im": [null, null, null, null, null, null],
  "period": [null, null, null, null, null, null],
  "phase": [null, null, null, null, null, null],
  "deltaPhase": [null, null, null, null, null, null],
  "alpha": [null, null, null, null, null, null],
  "mama": [null, null, null, null, null, null],
  "fama": [null, null, null, null, null, null]
}

Diagrams

MESA Adaptive Moving Average (MAMA) — adaptive smoother choice
MESA Adaptive Moving Average (MAMA) — mama alpha transfer
MESA Adaptive Moving Average (MAMA) — mama phase wrap
MESA Adaptive Moving Average (MAMA) — mama ratchet trace
MESA Adaptive Moving Average (MAMA) — mama signal pipeline
MESA Adaptive Moving Average (MAMA) — mama warmup timeline

Calculation flow

MAMA state flow
flowchart LR
    P["Declared price source"] --> W["4-bar weighted smoother"]
    W --> H["Hilbert FIR states"]
    H --> IQ["I1 and Q1"]
    IQ --> PH["Signed phase change"]
    PH --> A["Bounded adaptive alpha"]
    P --> M["MAMA: alpha on price"]
    A --> M
    M --> F["FAMA: alpha / 2 on MAMA"]
    IQ --> D["I2/Q2 homodyne discriminator"]
    D --> R["Bounded period state"]
    R --> H

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