@ldrick/trade-indicators
Version:
Trade Indicators
15 lines (14 loc) • 591 B
JavaScript
import { function as F, readonlyArray as RA, readonlyNonEmptyArray as RNEA } from 'fp-ts';
import { amean } from './amean.js';
/**
* Base implementation for the Exponential Moving Average (EMA) and
* the Smoothed Moving Average (SMMA) by providing a factor.
* @internal
*/
export const dma = (values, period, factor) => {
const [init, rest] = RNEA.splitAt(period)(values);
return F.pipe(rest, RA.reduce([amean(init)], (reduced, value) => {
const previous = RNEA.last(reduced);
return RA.append(value.sub(previous).mul(factor).add(previous))(reduced);
}));
};