JSPM

minimacd

0.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q37448F
  • License MIT

A versatile 807-byte Javascript implementation of the MACD formula.

Package Exports

  • minimacd
  • minimacd/dist/main.min.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (minimacd) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

minimacd

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

Features

  • 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.

License

MIT