-
Notifications
You must be signed in to change notification settings - Fork 35
/
quotes.js
132 lines (122 loc) · 3.71 KB
/
quotes.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var dates = require('./isodate.js');
var series = require("run-series");
var request = require('request');
var n = require('numbro');
module.exports = {
download: function(list, cb) {
historical_list = list.slice();
today_list = list.slice();
rt_list = list.slice();
series([download_historical, download_today, download_realtime], function(err, result) {
cb(err, result);
});
},
get: function(symbol) {
let q = quotes_historical[symbol];
if (q && q.length > 0) {
return (global.backtest_offset > 0) ?
q.slice(global.backtest_offset) :
[quotes_today[symbol], ...q];
}
return [];
}
}
var quotes_today = { };
var quotes_historical = { };
var historical_list;
var today_list;
var rt_list;
// historical from iextrading
function download_historical(cb) {
console.log("Downloading historical quotes...");
let url = "https://api.iextrading.com/1.0/stock/market/batch?symbols="+
historical_list.slice(0, 99).join(',')+"&types=chart&range=2y";
request(url, function(err, resp, body) {
if (err) {
console.error(err);
return setTimeout(function() {
download_historocal(cb);
}, 10000);
}
parse_historical(body);
if (historical_list.length > 0)
return download_historical(cb);
cb();
})
}
// today from iextrading
function download_today(cb) {
console.log("Downloading delayed today quotes...");
let url = "https://api.iextrading.com/1.0/stock/market/batch?symbols="+
today_list.slice(0, 99).join(',')+"&types=quote";
request(url, function(err, resp, body) {
if (err) {
console.error(err);
return setTimeout(function() {
download_today(cb);
}, 10000);
}
parse_today(body);
if (today_list.length > 0)
return download_today(cb);
cb();
})
}
// realtime from robinhood
function download_realtime(cb) {
if (global.Robinhood === undefined) {
return cb();
}
console.log("Downloading real-time quotes...");
global.Robinhood.quote_data(rt_list, function(err, resp, body) {
if (err) {
console.error(err);
return setTimeout(function() {
download_realtime(cb);
}, 10000);
}
parse_realtime(body);
cb();
});
}
// parse iextrading historical response
function parse_historical(jquotes) {
try {
var quotes = JSON.parse(jquotes);
} catch (error) {
console.log(error);
return;
}
for (symbol in quotes) {
let charts = quotes[symbol].chart.sort(function(a, b) {
return new Date(b.date) - new Date(a.date);
});
quotes_historical[symbol] = charts;
historical_list.splice(historical_list.indexOf(symbol), 1);
}
}
// parse iextrading today response
function parse_today(jquotes) {
try {
var quotes = JSON.parse(jquotes);
} catch (error) {
console.log(error);
return;
}
for (symbol in quotes) {
let quote = quotes[symbol].quote;
quote.volume = quote.latestVolume;
quotes_today[symbol] = quote;
today_list.splice(today_list.indexOf(symbol), 1);
}
}
// parse realtime robinhood response
function parse_realtime(body) {
body.results.forEach(function(item) {
// adjust today quotes with real-time data
var quote = quotes_today[item.symbol];
quote.close = n(item.last_trade_price).value();
quote.high = Math.max(quote.high, quote.close);
quote.low = Math.min(quote.low, quote.close);
});
}