series-processing
Version:
Time-series processing for forex, market analysis, including MA, EMA,...
66 lines (51 loc) • 1.33 kB
JavaScript
import DataStream from './data-stream';
export default class TimeSeries {
chain = [];
constructor(options) {
this.options = Object.assign({
// Default value
}, options);
this.dataStream = new DataStream();
}
getDataSeries = () => this.dataStream.getData();
getLatest = (num = 1) => this.dataStream.getLast(num);
initData = (data = [], skip = false) => {
if (skip) {
this.dataStream.setData(data);
} else {
// Append one-by-one
this.dataStream.setData(null);
data.map(record => this.appendData(record));
}
return this;
};
appendData = (record) => {
// TODO How about update last data?
this.dataStream.push(record);
for (let { type, func } of this.chain) {
if (type === 'map') {
Object.assign(record, func(record, this.dataStream));
}
}
return this;
};
map = (func) => {
if (Array.isArray(func)) {
// Recursive add to chain if array
func.map(f => this.map(f));
} else if (typeof func === 'function') {
this.chain.push({
type: 'map',
func,
});
// TODO Call func if data exists
if (this.dataStream.hasData()) {
}
}
return this;
};
// TODO Reduce function?
aggregate = (func) => {
return func(this.dataStream);
};
}