-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgetStockData.js
More file actions
35 lines (32 loc) · 1.19 KB
/
Copy pathgetStockData.js
File metadata and controls
35 lines (32 loc) · 1.19 KB
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
async function getData(stockSymbol, limit){
if(!stockSymbol){
alert('Please enter in a value');
return;
}
var flag = true;
if(!limit){
flag = false;
}
var rawData = await (await window.fetch('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&outputsize=full&symbol='+encodeURIComponent(stockSymbol)+'&apikey=VFKNX13I1KQF3WRZ')).json();
var errMsg = rawData['Error Message'];
if(errMsg){
if(errMsg.includes('Invalid API call')){
alert('Stock symbol not found');
}else if(errMsg.includes('if you would like to target a higher API call frequency')){
alert('Oops, looks like you spammed too much. Wait a couple minutes before trying again');
}else{
alert('Weird error from alpha vantage:'+errMsg);
}
return;
}
var labels = [];
var prices = [];
var count = 0;
for(var label in rawData['Time Series (Daily)']){
if(flag && count >= limit) break;
labels.push(label);
prices.push(parseFloat(rawData['Time Series (Daily)'][label]['4. close']));
count++;
}
return [labels.reverse(), prices.reverse()];
}