@ldrick/trade-indicators
Version:
Trade Indicators
16 lines (15 loc) • 586 B
JavaScript
import { function as F, readonlyArray as RA, readonlyNonEmptyArray as RNEA } from 'fp-ts/lib';
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 prev = RNEA.last(reduced);
return RA.append(value.sub(prev).mul(factor).add(prev))(reduced);
}));
};