-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdax.js
60 lines (44 loc) · 1.19 KB
/
gdax.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
var Gdax = require('gdax');
var util = require('../../core/util.js');
var _ = require('lodash');
var moment = require('moment');
var log = require('../../core/log');
var config = util.getConfig();
var dirs = util.dirs();
var Fetcher = require(dirs.exchanges + 'gdax');
util.makeEventEmitter(Fetcher);
var end = false;
var done = false;
var from = false;
var prevLastId = false;
var fetcher = new Fetcher(config.watch);
var fetch = () => {
fetcher.import = true;
fetcher.getTrades(from, handleFetch);
}
var handleFetch = (unk, trades) => {
var last = moment.unix(_.last(trades).date);
var lastId = _.last(trades).tid
if(last < from) {
log.debug('Skipping data, they are before from date', last.format());
return fetch();
}
if (last > end || lastId === prevLastId) {
fetcher.emit('done');
var endUnix = end.unix();
trades = _.filter(
trades,
t => t.date <= endUnix
)
}
prevLastId = lastId
fetcher.emit('trades', trades);
}
module.exports = function (daterange) {
from = daterange.from.clone();
end = daterange.to.clone();
return {
bus: fetcher,
fetch: fetch
}
}