Last active
July 11, 2025 00:13
-
-
Save daverich204/44d53b8e949360256fec1cb59bf5c6ed to your computer and use it in GitHub Desktop.
Yahoo Finance AppsScript for Google Sheets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function yahooF(ticker) { | |
const url = `https://query1.finance.yahoo.com/v8/finance/chart/${ticker}`; | |
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true}); | |
const contentText = res.getContentText(); | |
const data = JSON.parse(contentText); | |
// Check if the result exists and has data | |
if (data && data.chart && data.chart.result && data.chart.result.length > 0) { | |
const regularMarketPrice = data.chart.result[0].meta.regularMarketPrice; | |
console.log(regularMarketPrice); | |
return regularMarketPrice; | |
} else { | |
console.log("Error: Unable to retrieve market price."); | |
return null; | |
} | |
} |
@daverich204 Thank you! I don't recall where I saw that regularMarketChangePercent attribute, maybe it was a function I saw somewhere.
Hi, thanks for providing this, it is really helpful
Do we have possibility of getting all information that are typically listed on the Yahoo Finance page, for example marketcap, PE, Volume Data and so on. Also If i need to fetch a specific date price, how can I do that
Proposed updated version for dynamic attribute request.
function yahooF(ticker, attribute = "regularMarketPrice") {
if (!ticker || typeof ticker !== 'string') {
console.log("Error: Ticker is required and must be a string.");
return null;
}
if (!attribute || typeof attribute !== 'string') {
console.log("Error: Attribute is required and must be a string.");
return null;
}
const url = `https://query1.finance.yahoo.com/v8/finance/chart/${ticker}`;
try {
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
const contentText = res.getContentText();
const data = JSON.parse(contentText);
if (!data || !data.chart || !data.chart.result || data.chart.result.length === 0) {
console.log(`Error: Unable to retrieve data for ticker ${ticker}.`);
return null;
}
const meta = data.chart.result[0].meta;
if (meta.hasOwnProperty(attribute)) {
const value = meta[attribute];
console.log(`${attribute} for ${ticker}: ${value}`);
return value;
} else {
console.log(`Error: Attribute '${attribute}' not found for ticker ${ticker}.`);
console.log(`Available attributes: ${Object.keys(meta).join(', ')}`);
return null;
}
} catch (error) {
console.log(`Error fetching data for ${ticker}: ${error.message}`);
return null;
}
}
As of today, following attributes are available:
currency
symbol
exchangeName
fullExchangeName
instrumentType
firstTradeDate
regularMarketTime
hasPrePostMarketData
gmtoffset
timezone
exchangeTimezoneName
regularMarketPrice
fiftyTwoWeekHigh
fiftyTwoWeekLow
regularMarketDayHigh
regularMarketDayLow
regularMarketVolume
longName
shortName
chartPreviousClose
previousClose
scale
priceHint
currentTradingPeriod
tradingPeriods
dataGranularity
range
validRanges
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @SunRise8320 , I don't see a
regularMarketChangePercent
attribute, but I do seeregularMarketDayHigh
andregularMarketDayLow
as well aschartPreviousClose
/previousClose
attributes.Would returning something like this meet your needs :