UNPKG

series-processing

Version:

Time-series processing for forex, market analysis, including MA, EMA,...

28 lines (24 loc) 1.07 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * Exponential Moving Average * @see https://en.wikipedia.org/wiki/Exponential_smoothing * * <output series> = ema(<input series>, length) */ var EMA = exports.EMA = function EMA(outputKey, inputKey, length) { return function (lastPoint, dataStream) { if (typeof lastPoint === 'undefined' || typeof lastPoint[inputKey] === 'undefined') { return null; } var previousPoint = dataStream.getPrevious(); // Use the last data item as the first previous EMA value (if not previous point) var previousEma = previousPoint && previousPoint[inputKey] ? previousPoint[inputKey] : lastPoint[inputKey]; var K = 2 / (1 + length); var ema = lastPoint[inputKey] * K + previousEma * (1 - K); return _defineProperty({}, outputKey, ema); }; };