TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published


A versatile, compact (807 bytes) Javascript implementation of Gerald Appel's MACD (Moving Average Convergence / Divergence) formula.

Obligatory BadgeFest:

git github npm terminal typescript

npm version latest tag npm total downloads watchers stars issues forks contributors branches releases commits last commit types install size known vulnerabilities tested with jest code style: prettier license


  • Self-contained: No run-time package dependencies!
  • Compact: Only 807 bytes!
  • Versatile: The Exponential Moving Average can be seeded in two ways

ema() : Exponential Moving Average

function ema(
	array: number[],
	period: number,
	usePeriodAsSeedLength: boolean
): number[] { ... }

See Gerald Appel's book 'Technical Analysis - Power Tools for Active Investors', chapter 6, pp. 134-137

const alpha = 2 / (period + 1); // The smoothing constant (Appel p. 134)

meanValue is the initial value which stabilizes the exponential average. It is the simple average of the first seedLength values in the array, after skipping any initial run of invalid values (e.g. NaN) See the section 'Stabilizing the Exponential Average' (Appel p. 136)

Developers' note: Do not try to replace this:

result = alpha * element + (1 - alpha) * result;

... with either of these:

result += alpha * (element - result);
result = alpha * (element - result) + result;

They are the same algebraically, but the latter two provide different results than the former due to limited floating-point precision.

macd() : Moving Average Convergence / Divergence

function macd(
	array: number[],
	fastPeriod = 12,
	slowPeriod = 26,
	signalPeriod = 9,
	usePeriodAsSeedLength = false
): number[][] { ... }

This function returns a two-element tuple consisting of [macdArray, signalArray].

  • When usePeriodAsSeedLength is falsy, this algorithm behaves like the npm package macd written by Kael Zhang; i.e. the EMA will be seeded with the first value in the array.
  • When usePeriodAsSeedLength is truthy, this algorithm behaves like indicatorMacd in the npm package @d3fc/d3fc-technical-indicator; i.e. the EMA will be seeded with the simple average (the mean) of the first n values in the array, where n is the EMA's period.

In general terms, macd is defined as this:

function macd(array, fastPeriod, slowPeriod) {
	return ema(array, fastPeriod) - ema(array, slowPeriod);

A signal(n) value is the EMA (with period = n) of consecutive MACD values.



Package Sidebar


npm i minimacd

Weekly Downloads






Unpacked Size

12.6 kB

Total Files


Last publish


  • tom-weatherhead