@ldrick/trade-indicators
Version:
Trade Indicators
25 lines (24 loc) • 1.16 kB
JavaScript
import { Big } from 'big.js';
import { apply as AP, either as E, function as F } from 'fp-ts/lib';
import * as arr from '../utils/array.js';
import * as num from '../utils/number.js';
import { dma } from './dma.js';
const factor = (period) => F.pipe(period, num.toBig, E.map((periodB) => new Big(2).div(periodB.add(1))));
/**
* EMA without checks and conversion.
*
* @internal
*/
export const emaC = (values, period) => F.pipe(period, factor, E.map((factorB) => dma(values, period, factorB)));
/**
* The Exponential Moving Average (EMA) takes newer values weighted into account
* and reacts closer to the prices compared to the Simple Moving Average (SMA).
* It can be used to identify support and resistance levels.
* Also prices above the EMA can indicate uptrends, prices below can indicate downtrends.
*
* @public
*/
export const ema = (values, period = 20) => F.pipe(AP.sequenceS(E.Applicative)({
periodV: num.validatePositiveInteger(period),
valuesV: arr.validateRequiredSize(period)(values),
}), E.bind('valuesB', ({ valuesV }) => arr.toBig(valuesV)), E.chain(({ valuesB, periodV }) => emaC(valuesB, periodV)), E.map(arr.toNumber));