UNPKG

series-processing

Version:

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

121 lines (109 loc) 3.74 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var DataStream = function () { function DataStream(data, options) { _classCallCheck(this, DataStream); this.data = Object.assign([], data); this.length = this.data.length; this.options = Object.assign({ // Default setting timefield: 'timestamp', timeformat: function timeformat(v) { return new Date(v); }, timeblock: '1min', maxsize: 500 }, options); if (typeof this.options.timefield !== "string") { throw Error('options.timefield of DataStream must be a string.'); } if (typeof this.options.timeformat !== "function") { throw Error('options.timeformat of DataStream must be a function.'); } this.timeBlockInMs = 60000; switch (this.options.timeblock) { case '5min': this.timeBlockInMs *= 5; break; case '15min': this.timeBlockInMs *= 15; break; case '30min': this.timeBlockInMs *= 30; break; case '1hr': this.timeBlockInMs *= 60; break; case '2hr': this.timeBlockInMs *= 120; break; case '3hr': this.timeBlockInMs *= 180; break; case '12hr': this.timeBlockInMs *= 720; break; case '1d': this.timeBlockInMs *= 1440; break; } } _createClass(DataStream, [{ key: 'getData', value: function getData() { return this.data; } }, { key: 'setData', value: function setData(data) { this.data = Object.assign([], data); this.length = this.data.length; } }, { key: 'push', value: function push(item) { if (this.length >= this.options.maxsize) { this.data.shift(); } this.data.push(item); this.length = this.data.length; } }, { key: 'hasData', value: function hasData() { return this.length > 0; } }, { key: 'getLast', value: function getLast() { var num = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; return this.length >= num ? this.data[this.length - num] : null; } }, { key: 'getPrevious', value: function getPrevious() { return this.getLast(2); } }, { key: 'getLastSegment', value: function getLastSegment(length) { var segmentLength = Math.min(length, this.length); return this.data.slice(this.length - segmentLength); } }, { key: 'calcDistanceWithLast', value: function calcDistanceWithLast() { if (!this.getLast() || !this.getPrevious()) return 0; var lastTime = +this.options.timeformat(this.getLast()[this.options.timefield]); var prevTime = +this.options.timeformat(this.getPrevious()[this.options.timefield]); return Math.ceil((lastTime - prevTime) / this.timeBlockInMs); // deltaTime / timeBlock } }]); return DataStream; }(); exports.default = DataStream; module.exports = exports['default'];