fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

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#

verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

Input#

values
[10, 13, 12, 15, 14, 18]

Showing 6 of 8 elements.

span
3

Call#

calculateDemaComponents(values, span)

Returns#

object with 4 fields: 4, 5, 6, 7

{
  "4": {
    "dema": 14.444444444444445,
    "status": "ready"
  },
  "5": {
    "dema": 17.305555555555557,
    "status": "ready"
  },
  "6": {
    "dema": 17.444444444444443,
    "status": "ready"
  },
  "7": {
    "dema": 19.618055555555557,
    "status": "ready"
  }
}

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

The rest of the Trend Smoothing family#