MESA Adaptive Moving Average (MAMA)
Install and import
npm install fintech-algorithmsimport { 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
| Name | Type | Notes |
|---|---|---|
values | (number | null)[] | Observation series in chronological order, oldest first. nulls: propagate |
fastLimit | number | Upper bound on the adaptive smoothing factor. min: 0 · max: 1 optional |
slowLimit | number | Lower 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
[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
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.
References
- MESA Adaptive Moving Averages — John F. Ehlers; *Technical Analysis of Stocks &
- MAMA: The Mother of Adaptive Moving Averages — John F. Ehlers
- Pinned TA-Lib MAMA source of truth — TA-Lib project
- Official TA-Lib MAMA function page — TA-Lib project
- TA-Lib MAMA metadata — TA-Lib project
- Rocket Science for Traders — John F. Ehlers; John Wiley & Sons
- TA-Lib API: unstable periods — TA-Lib project
- QuantConnect MESA Adaptive Moving Average documentation — QuantConnect
- Canonical package calculations — The Fintech Builder topic package
- Source and derived boundary