fintech-algorithms

Double Exponential Moving Average (DEMA)

Reduced Lag and Overshoot

Install and import

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

Signature

calculateDemaComponents(values, span)

Double exponential moving average: 2 × EMA − EMA(EMA). Subtracting the second smoothing pass cancels most of the lag a single EMA introduces, at the cost of overshooting sharp reversals.

Parameters

NameTypeNotes
values(number | null)[]Observation series in chronological order, oldest first.
nulls: propagate
spannumberSmoothing span used for both EMA passes; the decay factor is 2 / (span + 1).
min: 1 · integer: true

Returns

{ ema1, ema2, dema }[] · length same-as-input

One record per position carrying both intermediate EMAs alongside the result, so the cancellation can be checked rather than taken on trust.

Warm-up

The first 2 × (span − 1) positions are null. Both passes must fill before the difference is defined.

Errors

  • When span < 1 or is not an integer — 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
[10, 13, 12, 15, 14, 18]

Showing 6 of 8 elements.

span
3

Call

calculateDemaComponents(values, span)

Returns

array of 8 objects

[
  {
    "value": 10,
    "ema1": null,
    "ema2": null,
    "dema": null,
    "status": "warming_ema1"
  },
  {
    "value": 13,
    "ema1": null,
    "ema2": null,
    "dema": null,
    "status": "warming_ema1"
  },
  {
    "value": 12,
    "ema1": 11.666666666666666,
    "ema2": null,
    "dema": null,
    "status": "warming_ema2"
  }
]

Showing 3 of 8 elements.

Other exports

This module also exports calculateDema, demaSteadyStateWeights. Every module additionally exports run as an alias of its primary function, and a meta object carrying its catalog id, domain, family, shape and article URL.

Diagrams

Double Exponential Moving Average (DEMA) — dema components
Double Exponential Moving Average (DEMA) — dema path
Double Exponential Moving Average (DEMA) — dema signed weights

Calculation flow

DEMA calculation flow
flowchart LR
    A["Validate ordered finite value"] --> B["Update or seed EMA1"]
    B --> C{"EMA1 ready?"}
    C -- "No" --> D["Emit warming EMA1"]
    C -- "Yes" --> E["Feed EMA1 into EMA2"]
    E --> F{"EMA2 ready?"}
    F -- "No" --> G["Emit warming EMA2"]
    F -- "Yes" --> H["Calculate 2 × EMA1 − EMA2"]
    H --> I["Emit ready DEMA and component states"]

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

  • Smoothing Data With Faster Moving Averages — Patrick G. Mulloy; *Technical Analysis of Stocks & Commodities*
  • TA-Lib DEMA definition — TA-Lib project
  • Follow-up discussion of equivalent period — Letter published by *Technical Analysis of Stocks & Commodities*
  • EMA foundation package — The Fintech Builder
  • Evidence and design reconciliation
  • Historical-case publication boundary