UNPKG

@4ex/indicators

Version:

Technical indicators for ohlc charts written in TypeScript

96 lines (95 loc) 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MA = void 0; const fp_1 = require("lodash/fp"); const technicalindicators_1 = require("technicalindicators"); const types_1 = require("../types"); const defaultInput = { source: types_1.OHLCEnum.CLOSE, method: types_1.MovingAverageEnum.SMA, }; /** * In statistics, a moving average is a calculation used to analyze data points * by creating a series of averages of different subsets of the full data set. * In finance, a moving average (MA) is a stock indicator that is commonly used * in technical analysis. The reason for calculating the moving average of a * stock is to help smooth out the price data by creating a constantly updated * average price. * [Investopedia](https://www.investopedia.com/terms/m/movingaverage.asp) */ class MA { /** * Create a new instance for Moving Average * @param {number} period number of period to calculate moving average * @param {OHLC[]} series series of candle * @param {OHLCEnum} source source of calculation * @param {MovingAverageEnum} method type of moving average calculation */ constructor(period, series = [], source = defaultInput.source, method = defaultInput.method) { // // super(); let indicator; const values = fp_1.map(source)(series); switch (method) { case types_1.MovingAverageEnum.SMA: indicator = new technicalindicators_1.SMA({ values, period }); break; case types_1.MovingAverageEnum.EMA: indicator = new technicalindicators_1.EMA({ values, period }); break; case types_1.MovingAverageEnum.WMA: indicator = new technicalindicators_1.WMA({ values, period }); break; case types_1.MovingAverageEnum.WEMA: indicator = new technicalindicators_1.WEMA({ values, period }); } this.indicator = indicator; this.method = method; this.source = source; } /** * Calculate Moving Average from object * @param {MovingAverageInput} input input data for calculation * @return {number[]} array of MAs based on input */ static calculate(input) { const { series, source, method, period } = Object.assign(Object.assign({}, defaultInput), input); const values = fp_1.map(source)(series); switch (method) { case types_1.MovingAverageEnum.SMA: return technicalindicators_1.SMA.calculate({ values, period }); case types_1.MovingAverageEnum.EMA: return technicalindicators_1.EMA.calculate({ values, period }); case types_1.MovingAverageEnum.WMA: return technicalindicators_1.WMA.calculate({ values, period }); case types_1.MovingAverageEnum.WEMA: return technicalindicators_1.WEMA.calculate({ values, period }); default: throw new Error('[MA] unrecognized MA method for calculation!'); } } /** * Generate instance of MA indicator * @param {MovingAverageInput} input object with input for MA * @return {MA} instance of indicator */ static generator({ series, source, method, period, }) { return new MA(period, series, source, method); } /** * Calculate moving average for next tick * @param {OHLC} candle price candle * @return {number} next ma value or undefined if period is greater * than actual series length */ next(candle) { return this.indicator.nextValue(candle[this.source]); } /** * Same as calculate bat for instance data * @return {number[]} moving average. */ getResults() { return this.indicator.getResult(); } } exports.MA = MA;