forked from mlmurray/gekko
-
Notifications
You must be signed in to change notification settings - Fork 1
/
historicalCandleFetcher.js
110 lines (90 loc) · 2.86 KB
/
historicalCandleFetcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// var csv = require('csv');
var lineReader = require('line-reader');
var Util = require('util');
var EventEmitter = require('events').EventEmitter;
var _ = require('lodash');
var log = require('./log');
// `abstract` constructor
var CandleCalculator = function() {};
Util.inherits(CandleCalculator, EventEmitter);
CandleCalculator.prototype.set = function() {
this.candles = this.settings.candles;
this.from = this.settings.from || -Infinity;
this.to = this.settings.to || Infinity;
this.currentCandle = this.candles;
// candles are stored _chronologically_ (first is new, last is old)
this.candles = {
open: [],
high: [],
low: [],
close: []
}
lineReader.open(this.settings.candleFile, this.processFile);
}
CandleCalculator.prototype.processFile = function(reader) {
this.fetchingHistorical = true;
this.reader = reader;
this.reader.nextLine(_.bind(function(line) {
// first line has the column headers
this.emit('prepared');
}, this));
}
var loadCandle = function() {
}
CandleCalculator.prototype.getNewCandle = function() {
this.candles.open.shift();
this.candles.high.shift();
this.candles.low.shift();
this.candles.close.shift();
if(this.reader.hasNextLine())
this.reader.nextLine(this.addCandle);
else {
this.finish();
}
}
CandleCalculator.prototype.finish = function() {
this.endPrice = _.last(this.candles.close);
this.emit('finish', {
start: this.startPrice,
end: this.endPrice,
startTime: this.startTime,
endTime: this.currentTimestamp
});
}
CandleCalculator.prototype.getHistoricalCandles = function() {
this.reader.nextLine(this.addCandle);
}
CandleCalculator.prototype.addCandle = function(line) {
line = line.split(',');
this.currentTimestamp = parseInt( line[0] );
if(this.currentCandle > 0 && this.currentTimestamp < this.from)
// this candle happened before the `from`, skip this candle and try again
return this.getHistoricalCandles();
else if(this.currentCandle === 0 && this.currentTimestamp > this.to)
// this candle happened after the `to`, we are done!
return this.finish();
this.candles.open.push( parseFloat(line[1]) );
this.candles.high.push( parseFloat(line[2]) );
this.candles.low.push( parseFloat(line[3]) );
this.candles.close.push( parseFloat(line[4]) );
this.emit('calculated candle');
if(this.currentCandle > 0) {
this.currentCandle--;
this.getHistoricalCandles();
} else {
if(this.fetchingHistorical) {
this.startPrice = _.first(this.candles.open);
this.startTime = this.currentTimestamp;
this.fetchingHistorical = false;
log.info('calculated initial EMA, simulating remaining candles')
}
this.emit('calculated new candle');
}
}
// we need:
// getHistoricalCandles
// -> 'calculted candle'
// getNewCandle
// -> 'calculted candle'
// -> 'calculated new candle'
module.exports = CandleCalculator;