-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphavantage.js
169 lines (136 loc) · 4.57 KB
/
alphavantage.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
"use strict";
let credentials = require("./alphavantage_credentials.json");
let https = require("https");
let URL_BASE = "https://www.alphavantage.co/query?";
let REQUEST_INTRADAY = {
"function": "TIME_SERIES_INTRADAY",
"interval": "30min",
"apikey": credentials["api-key"],
"symbol": null
};
//TODO use this...
let REQUEST_DAILY = {
"function": "TIME_SERIES_DAILY",
"apikey": credentials["api-key"],
"symbol": null
};
function getStockQuote(ticker, cb){
if ("function" !== typeof cb) throw ErrorType(ErrorType.INVALID_ARGUMENT);
let request = REQUEST_INTRADAY;
request.symbol = ticker.toLowerCase();
let urlParams = [];
for (let key in request){
if (!request.hasOwnProperty(key)) return;
urlParams.push([key, request[key]].join("="));
}
let url = URL_BASE+urlParams.join("&");
function handleHttpResponse(resp){
let data = [];
console.log("Got response: " + resp.statusCode);
if ("200" == resp.statusCode){
resp.on("readable", (stream) => {
let bb = resp.read();
data.push(bb);
});
resp.on("end", ()=>{
let msg = data.join("");
//console.debug(msg);
let stockData = {};
try {
stockData = JSON.parse(msg);
}
catch(err){
console.error("Failed to parse server response: "+err.message);
}
cb(stockData);
//processServerResponse(JSON.parse(msg));
});
}
}
/*
function processServerResponse(data){
if (!data) throw ErrorType(ErrorType.SERVER_RESPONSE);
let respKeys = Object.keys(data);
console.log(respKeys);
let series = respKeys
.filter(function(item){
if (0 === item.indexOf("Time Series")){
return true;
}
})
.reduce(function(acc, item){
return data[item];
}, {});
console.log(series);
let now = new Date();
let todayDateInfo = [now.getFullYear(), 1+now.getMonth(), now.getDate()];
console.log("Today is " + todayDateInfo);
let filteredSeries = Object.keys(series)
.filter(function(item){
console.log(item);
let dateInfo = item.split(" ")[0].split("-");
dateInfo = dateInfo.map( item => parseInt(item));
return (
(todayDateInfo[1] === dateInfo[1])
&& (todayDateInfo[2] === dateInfo[2]) );
})
.map( item => {
let t = new Date(item);
return {
timestamp: t.getTime(),
timeInfo: t.toTimeString(),
quote: series[item]
}
})
.sort( (a,b) => ((a.timestamp < b.timestamp)?-1:+1) );
console.log(filteredSeries);
cb(filteredSeries);
}
*/
https.get(url, handleHttpResponse);
}
function getStockDailyQuote(ticker, cb){
if ("function" !== typeof cb) throw ErrorType(ErrorType.INVALID_ARGUMENT);
let request = REQUEST_DAILY;
request.symbol = ticker.toLowerCase();
let urlParams = [];
for (let key in request){
if (!request.hasOwnProperty(key)) return;
urlParams.push([key, request[key]].join("="));
}
let url = URL_BASE+urlParams.join("&");
function handleHttpResponse(resp){
let data = [];
console.log("Got response: " + resp.statusCode);
if ("200" == resp.statusCode){
resp.on("readable", (stream) => {
let bb = resp.read();
data.push(bb);
});
resp.on("end", ()=>{
let msg = data.join("");
//console.debug(msg);
let stockData = {};
try {
stockData = JSON.parse(msg);
}
catch(err){
console.error("Failed to parse server response: "+err.message);
}
cb(stockData);
//processServerResponse(JSON.parse(msg));
});
}
}
https.get(url, handleHttpResponse);
}
function ErrorType(code){
return new Error(code);
}
ErrorType.SERVER_RESPONSE = 1;
ErrorType.INVALID_ARGUMENT = 2;
module.exports = {
ErrorType: ErrorType,
getStockQuote: getStockQuote,
getStockDailyQuote: getStockDailyQuote
};