Skip to content

Instantly share code, notes, and snippets.

@kpooooooooooo
Last active April 16, 2019 06:40
Show Gist options
  • Save kpooooooooooo/45fcf8eb4ed5f074c249965f9e43bbb8 to your computer and use it in GitHub Desktop.
Save kpooooooooooo/45fcf8eb4ed5f074c249965f9e43bbb8 to your computer and use it in GitHub Desktop.
KPO Alpha Vantage Guide
api_key = 'PASTE_YOUR_API_KEY_HERE'
url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol='
function getAlphaVantage(symbol) {
var response = UrlFetchApp.fetch(url + symbol + "&apikey=" + api_key).getContentText();
var data = JSON.parse(response);
var timeSeries = data["Time Series (Daily)"];
var value = 0.0;
for (var dates in timeSeries) {
value = timeSeries[dates]["4. close"];
Logger.log("Within getAlphaVantage(symbol) function:" + value)
if (value > 0.0) {
break;
}
}
return parseFloat(value);
}
function getAlphaVantageSlowly(symbol, seconds) {
// Cannot sleep too long - A custom function call must return within 30 seconds
// https://developers.google.com/apps-script/guides/sheets/functions
temp = seconds % 30
Utilities.sleep(temp * 1000) // function is in milliseconds
var response = UrlFetchApp.fetch(url + symbol + "&apikey=" + api_key).getContentText();
var data = JSON.parse(response);
var timeSeries = data["Time Series (Daily)"];
var value = 0.0;
for (var dates in timeSeries) {
value = timeSeries[dates]["4. close"];
Logger.log("Within getAlphaVantage(symbol) function:" + value)
if (value > 0.0) {
break;
}
}
return parseFloat(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment