@ldrick/trade-indicators
Version:
Trade Indicators
24 lines (23 loc) • 1.12 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 getFactor = (period) => F.pipe(period, num.toBig, E.map((periodB) => new Big(1).div(periodB)));
/**
* SMMA without checks and conversion.
*
* @internal
*/
export const smmaC = (values, period) => F.pipe(period, getFactor, E.map((factorB) => dma(values, period, factorB)));
/**
* The Smoothed Moving Average (SMMA) is like the Exponential Moving Average (EMA),
* with just a “smoother” factor. It can be used to identify support and resistance levels.
* Also prices above the SMMA can indicate uptrends, prices below can indicate downtrends.
*
* @public
*/
export const smma = (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 }) => smmaC(valuesB, periodV)), E.map(arr.toNumber));