-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
executable file
·276 lines (255 loc) · 8.09 KB
/
search.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var request = require('request');
require('dotenv').config();
/* GET CHART DATA from vantage, where chart formats are pre made
* str : ticker symbol
* type : SMA, TIME_SERIES_DAILY
* length : 365, 90
* interval: daily, weekly, 60min, 15min,
* callback(err, res); */
function getVantageChart(str, type, length, interval, callback) {
if (!(length)) length = 254; //one year of buesiness days
if (!(interval)) interval = "daily";
if (!(type)) type = "TIME_SERIES_DAILY";
//https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=amzn&interval=daily&time_period=daily&series_type=close&outputsize=full&apikey=
var url = "https://www.alphavantage.co/query?function="+ type +"&symbol="+ str +"&interval="+interval+"&time_period="+ length +"&series_type=close&outputsize=full&apikey=" + process.env.VANTAGE_KEY;
request(url, function (err, resp, body) {
if (err) {
callback(err, null);
} else {
try {
body = JSON.parse(body);
} catch (e) {
callback("ERROR (getVantageChart) JSON.parse failed!", null);
}
var name = "";
switch (type.toUpperCase()) {
case 'SMA':
name = "Technical Analysis: SMA";
break;
case 'TIME_SERIES_DAILY':
name = "Time Series (Daily)";
break;
}
//remove number in front of open and close fields, also convert strings to numbers, and return only the data
var data = {};
var sub = {};
if (body[name]) {
var i = 1;
try {
for (var day in body[name]) {
if (i <= length) {
data[day] = {};
if (i < 64 && length == 254) sub[day] = {};
for (var row in body[name][day]) {
var index = row.replace(/[0-9]. /, "");
var dp = Number(body[name][day][row]);
data[day][index] = dp;
if (i < 64 && length == 254) { //if your make a 1 year than make a 3 month
sub[day][index] = dp;
}
}
i++;
} else {
throw "done";
}
}
callback("did not send a done message", data);
} catch (e) {
var year = name + "_year";
var month = name + "_3month";
callback(null, {year: data, month: sub});
}
} else {
callback(body, null);
}
}
});
}
// TEST STOCK CHART DATA
// getVantageChart("MMM", null, null, null, (err, res) => {
// var i = 1;
// for (var key in res.month) {
// console.log(i + " " + key);
// i++;
// }
// })
function getStock(str, callback) {
str = str.replace(/1$/g, "");
str = str.replace(/1$/g, "");
str = str.replace(/1$/g, "");
//Peer and finicials have been removed from teer
// https://cloud.iexapis.com/stable/stock/HD/batch?types=company,logo,quote,stats,news,earnings&token=
var url = 'https://cloud.iexapis.com/stable/stock/' + str + '/batch?types=company,logo,quote,stats,news,earnings&token=' + process.env.IEX_CLOUD_SECRET_TOKEN;
// https://sandbox.iexapis.com/stable/stock/amzn/batch?types=company,quote,logo,news,chart,balance-sheet,earnings,cash-flow&range=1m&last=10&token=Tsk_a0ec83cc7b884b0eb6a0facb3792ef2e
//var url = 'https://sandbox.iexapis.com/stable/stock/'+ str + '/batch?types=company,quote,logo,stats,news,balance-sheet,earnings,cash-flow&token=' + process.env.IEX_CLOUD_SANDBOX_SECRET_TOKEN;
request(url, function (err, resp, body) {
if (err) {
callback("ERROR (search->getStock) request returned a error " + err, null);
} else {
try {
body = JSON.parse(body);
body["url"] = url;
} catch (e) {
return callback("ERROR (search->getStock) JSON.parse failed! " + e, null);
}
callback(null, body);
}
});
}
/* GET MAJOR INDICES PRICE DATA in PARALLEL
* callback (err, res)
*/
function getIndices(callback) {
//https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=^IXIC&apikey=your_api_key&outputsize=compact
request("https://financialmodelingprep.com/api/v3/quote/%5EGSPC,%5EDJI,%5EIXIC?apikey=" + process.env.FIN_MODEL_PREP_API_KEY, function (err, resp, body) {
if (err) {
reject("ERROR (getIndices->getPrice->promise) an error occurred during request \n " + err);
} else {
try {
body = JSON.parse(body);
callback(null, body);
} catch (e) {
callback("ERROR (getIndices->promise->getData) JSON.parse failed!", null);
}
// for (var obj of body) {
// var name =
// }
// var name = body["Meta Data"]["2. Symbol"];
// var dateStr = body["Meta Data"]["3. Last Refreshed"];
// var data = body["Time Series (Daily)"];
// var open = "";
// var close = "";
// var high = "";
// var low = "";
// var volume = "";
// var lastClose = "";
// var i = 0;
// for (var line in data) {
// if (i == 0) {
// i++;
// open = data[line]["1. open"];
// close = data[line]["4. close"];
// high = data[line]["2. high"];
// low = data[line]["3. low"];
// volume = data[line]["5. volume"];
// } else if (i == 1) {
// lastClose = data[line]["4. close"];
// resolve({"name" : name, "dateStr" : dateStr, "open" : open, "low" : low, "high" : high, "close" : close, "volume" : volume, "lastClose" : lastClose});
// i++;
// }
}
// }
// }
});
}
// var par = []; //parrellel
// par.push(getData("DJI"));
// par.push(getData("GSPC"));
// par.push(getData("IXIC"));
//get
// Promise.all(par).then(function(values) {
// console.log(values);
// callback(null, values);
// }).catch(function(values) {
// callback(values);
// });
/*GET CHOSEN STOCK INDEX. series is the time series
callback(err, json)
*/
function getMarketData(symbol, callback) {
request("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" + '^' + symbol + "&apikey=" + process.env.VANTAGE_KEY + "&outputsize=compact", function (err, resp, body) {
if (err) {
callback(err, null);
} else if (body){
try {
body = JSON.parse(body);
} catch (e) {
callback("ERROR (getMarketData) JSON.parse failed");
}
var json = body["Time Series (Daily)"];
//console.log(body)
try {
for (var key in json) {
throw json[key];
}
} catch (e) {
var json = {}
for (var key in e) {
if(e[key].match(/[0-9]\.[0-9]/g)) {
e[key] = e[key].replace(/[0-9][0-9]$/g, "");
console.log(e[key]);
}
json[key.slice(3)] = e[key];
}
json["name"] = body["Meta Data"]["2. Symbol"];
//get a formatted date
getStock("AAPL", (err, stock) => {
if (err) {
callback(err, null);
} else {
json["dateStr"] = stock.quote.latestTime;
callback(null, json)
}
});
}
} else {
//console.log("ERROR (getMarketData) body is null from request");
callback("ERROR (getMarketData) body is null from request", null);
}
});
}
/* GET STOCK PEERS DATA
array: is a an array of tickers
callback(err, res) */
function getPeers(array, callback) {
var str = "";
for (var i in array) {
str = str + ',' + array[i];
}
if (str.length <= 0) {
callback("(search->getPeers) no tickers in the peer array");
return;
}
var url = 'https://api.iextrading.com/1.0/stock/market/batch?symbols=' + str + '&types=quote,logo';
request(url, function (err, resp, body) {
if (err) {
callback("ERROR (search->getPeers) request returned a error " + err, null);
} else {
try {
body = JSON.parse(body);
body["url"] = url;
} catch (e) {
return callback("ERROR (search->getPeers) JSON.parse failed!" + e, null);
}
callback(null, body);
}
});
}
/*GET MARKET NEWS and send back the data
TODO : append a source tag to the end*/
function getNews(callback) {
request("https://newsapi.org/v2/top-headlines?sources=business-insider&apiKey=" + process.env.NEWS, (err, body, resp) => {
if (err) {
callback(err, null);
} else if (body) {
var data = {};
try {
data = JSON.parse(body.body);
} catch (e) {
callback("ERROR (getNews) JSON.parse failed");
}
callback(null, data)
//console.log(JSON.stringify(data, null, 2));
} else {
callback("ERROR (getNews) body is null from get request");
}
});
}
module.exports = {
getStock : getStock,
getMarketData : getMarketData,
getVantageChart : getVantageChart,
getIndices : getIndices,
getPeers : getPeers,
getNews: getNews
}