Skip to content

Instantly share code, notes, and snippets.

@henrytkirk
Created February 18, 2019 03:45
Show Gist options
  • Save henrytkirk/f0fb46901e7657d03372cdb66c6f5ce2 to your computer and use it in GitHub Desktop.
Save henrytkirk/f0fb46901e7657d03372cdb66c6f5ce2 to your computer and use it in GitHub Desktop.
Stock Functions for Google Sheets
/// Usage: =getStockPrice("AAPL")
/// Returns: 150.00
function getStockPrice(ticker) {
const stockURL = "https://ws-api.iextrading.com/1.0/stock/";
var response = UrlFetchApp.fetch(stockURL + ticker + "/price");
return JSON.parse(response);
}
/// Usage: =getStockChangePercent("AAPL")
/// Returns: "1.0%"
function getStockChangePercent(ticker) {
const data = getStockData(ticker);
return data.changePercent;
}
/*
Example output: getStockData("AAPL")
{
"symbol": "AAPL",
"companyName": "Apple Inc.",
"primaryExchange": "Nasdaq Global Select",
"sector": "Technology",
"calculationPrice": "tops",
"open": 154,
"openTime": 1506605400394,
"close": 153.28,
"closeTime": 1506605400394,
"high": 154.80,
"low": 153.25,
"latestPrice": 158.73,
"latestSource": "Previous close",
"latestTime": "September 19, 2017",
"latestUpdate": 1505779200000,
"latestVolume": 20567140,
"iexRealtimePrice": 158.71,
"iexRealtimeSize": 100,
"iexLastUpdated": 1505851198059,
"delayedPrice": 158.71,
"delayedPriceTime": 1505854782437,
"extendedPrice": 159.21,
"extendedChange": -1.68,
"extendedChangePercent": -0.0125,
"extendedPriceTime": 1527082200361,
"previousClose": 158.73,
"change": -1.67,
"changePercent": -0.01158,
"iexMarketPercent": 0.00948,
"iexVolume": 82451,
"avgTotalVolume": 29623234,
"iexBidPrice": 153.01,
"iexBidSize": 100,
"iexAskPrice": 158.66,
"iexAskSize": 100,
"marketCap": 751627174400,
"peRatio": 16.86,
"week52High": 159.65,
"week52Low": 93.63,
"ytdChange": 0.3665,
}
*/
function getStockData(ticker) {
const stockURL = "https://ws-api.iextrading.com/1.0/stock/";
var response = UrlFetchApp.fetch(stockURL + ticker + "/quote");
const data = JSON.parse(response);
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment