Skip to content

Instantly share code, notes, and snippets.

@jaredsburrows
Last active May 25, 2023 13:16
Show Gist options
  • Save jaredsburrows/d4de8c7b20f2b6bb3835c3a2c4eec866 to your computer and use it in GitHub Desktop.
Save jaredsburrows/d4de8c7b20f2b6bb3835c3a2c4eec866 to your computer and use it in GitHub Desktop.
Google Sheets - YAHOOFINANCE()
/*
* Copyright (C) 2022 Jared Burrows
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// https://query1.finance.yahoo.com/v7/finance/quote?fields=price&symbols=AAPL
// https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&fields=price&symbols=GOOG
// https://query2.finance.yahoo.com/v10/finance/quoteSummary/GOOG?modules=price
// https://query2.finance.yahoo.com/v10/finance/quoteSummary/INTC?modules=summaryProfile,summaryDetail,price,defaultKeyStatistics,financialData
// https://query2.finance.yahoo.com/v10/finance/quoteSummary/INTC?modules=assetProfile,summaryProfile,summaryDetail,esgScores,price,incomeStatementHistory,incomeStatementHistoryQuarterly,balanceSheetHistory,balanceSheetHistoryQuarterly,cashflowStatementHistory,cashflowStatementHistoryQuarterly,defaultKeyStatistics,financialData,calendarEvents,secFilings,recommendationTrend,upgradeDowngradeHistory,institutionOwnership,fundOwnership,majorDirectHolders,majorHoldersBreakdown,insiderTransactions,insiderHolders,netSharePurchaseActivity,earnings,earningsHistory,earningsTrend,industryTrend,indexTrend,sectorTrend
// References:
// - https://stackoverflow.com/questions/44030983/yahoo-finance-url-not-working
// - https://www.reddit.com/r/sheets/comments/ji52uk/yahoo_finance_api_url/
/**
* Fetches current or historical securities information from Yahoo Finance.
*
* Similar to GOOGLEFINANCE() from Google Sheets. See docs here: https://support.google.com/docs/answer/3093281.
* Updated here: https://gist.github.com/jaredsburrows/d4de8c7b20f2b6bb3835c3a2c4eec866.
*
* @param {string} ticker The ticker symbol for the security to consider.
* @param {string} attribute The attribute to fetch about ticker from Yahoo Finance.
*/
function YAHOOFINANCE7(ticker = "GOOG", attribute = "price") {
if (ticker == null) {
throw 'stock ticker is required';
}
if (attribute == null) {
throw 'attribute is required';
}
// Yahoo Finance URL
let url = `https://query2.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&fields=${attribute}&symbols=${ticker}`;
// Fetch Data
var urlResponse = UrlFetchApp.fetch(url);
var responseCode = urlResponse.getResponseCode();
if (responseCode >= 200 && responseCode < 300) {
Logger.log('HTTP request succeeded');
} else {
Logger.log('HTTP request failed');
}
// JSON Response
var jsonResponse = JSON.parse(urlResponse.getContentText());
// Result
return jsonResponse["quoteResponse"]["result"][0][attribute];
}
/**
* Fetches current or historical securities information from Yahoo Finance.
*
* Similar to GOOGLEFINANCE() from Google Sheets. See docs here: https://support.google.com/docs/answer/3093281.
* Updated here: https://gist.github.com/jaredsburrows/d4de8c7b20f2b6bb3835c3a2c4eec866.
*
* @param {string} ticker The ticker symbol for the security to consider.
* @param {string} module The module that contains the set of data.
* @param {string} attribute The attribute to fetch about ticker from Yahoo Finance.
*/
function YAHOOFINANCE10(ticker = "GOOG", module = "price", attribute = "longName") {
if (ticker == null) {
throw 'stock ticker is required';
}
if (attribute == null) {
throw 'attribute is required';
}
// Yahoo Finance URL
let url = `https://query2.finance.yahoo.com/v10/finance/quoteSummary/${ticker}?modules=${module}`;
// Fetch Data
var urlResponse = UrlFetchApp.fetch(url);
var responseCode = urlResponse.getResponseCode();
if (responseCode >= 200 && responseCode < 300) {
Logger.log('HTTP request succeeded');
} else {
Logger.log('HTTP request failed');
}
// JSON Response
var jsonResponse = JSON.parse(
urlResponse.getContentText(),
(key, value) => value && value.hasOwnProperty("raw") ? value["raw"] : value,
);
// Result
return jsonResponse["quoteSummary"]["result"][0][module][attribute];
}
@jargs
Copy link

jargs commented May 25, 2023

Hello there!

I'm trying to get the earnings date data. I'm using the summaryDetail module and have been able to get a lot of things like open, dayLow, dayHigh, etc. But can't seem to find the attribute for the Earnings Date.

Any help there greatly apreciated thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment