@ldrick/trade-indicators
Version:
Trade Indicators
16 lines (15 loc) • 1.12 kB
JavaScript
import { apply as AP, either as E, function as F, readonlyNonEmptyArray as RNEA } from 'fp-ts';
import * as array from '../utils/array.js';
import * as number_ from '../utils/number.js';
import { emaC } from './ema.js';
const calculate = (one, two, period) => F.pipe(two, RNEA.mapWithIndex((index, value) => one[index + period - 1].mul(2).sub(value).toNumber()));
/**
* The Double Exponential Moving Average (DEMA) uses two Exponential Moving Average (EMA)
* to reduce noise. It can be used to identify support and resistance levels.
* Also prices above the DEMA can indicate uptrends, prices below can indicate downtrends.
* @public
*/
export const dema = (values, period = 20) => F.pipe(AP.sequenceS(E.Applicative)({
periodV: number_.validatePositiveInteger(period),
valuesV: array.validateRequiredSize(2 * period - 1)(values),
}), E.bind('valuesB', ({ valuesV }) => array.toBig(valuesV)), E.bind('emaOne', ({ periodV, valuesB }) => emaC(valuesB, periodV)), E.bind('emaTwo', ({ emaOne, periodV }) => emaC(emaOne, periodV)), E.map(({ emaOne, emaTwo, periodV }) => calculate(emaOne, emaTwo, periodV)));