'use strict'; import _ from 'underscore'; import NumberUtil from '../utils/number.js'; import ValidateMixin from '../mixin/validate.js'; import SetOptionsMixin from '../mixin/set-options.js'; import HandleGeneratorMixin from '../mixin/handle-generator.js'; /** * @param {object} [options] * @param {number} [optons.periods=14] * @param {number} [options.startIndex] * @param {number} [options.endIndex}] * @param {boolean} [options.sliceOffset=false] * @param {boolean} [options.lazyEvaluation=true] * @param {number} [option.maxTickDuration=10] */ function ROC(options = {}) { if (!new.target) throw new Error('ERROR: ROC() must be called with new'); this._options = { periods: 14, startIndex: null, endIndex: null, sliceOffset: false, lazyEvaluation: true, maxTickDuration: 10, }; let m = [SetOptionsMixin, ValidateMixin, HandleGeneratorMixin]; Object.assign(this, ...m); this.setOptions(options); this._collection = []; } ROC.prototype = { setValues(values) { if (!Array.isArray(values)) { throw new Error('ERROR: values param is not an array'); } this._collection = values; }, calculate() { this._validate(this._collection, this._options); return this._handleGenerator(this._compute()); }, _compute: function* () { let results = []; let { startIndex, endIndex, periods, lazyEvaluation, sliceOffset } = this._options; if (!NumberUtil.isNumeric(startIndex)) startIndex = 0; if (!NumberUtil.isNumeric(endIndex)) endIndex = this._collection.length - 1; let resultItem = null; for (let i = startIndex; i <= endIndex; i++) { let currentPrice = this._collection[i]; if (i <= startIndex + periods - 1) { if (!sliceOffset) { resultItem = { roc: 0, price: currentPrice }; results.push(resultItem); if (lazyEvaluation) yield resultItem; } continue; } let prevPrice = this._collection[i - periods]; let roc = ((currentPrice - prevPrice) / prevPrice) * 100; resultItem = { roc: NumberUtil.roundTo(roc, 2), price: currentPrice }; results.push(resultItem); if (lazyEvaluation) yield resultItem; }; return results; }, } export default ROC;