Skip to content

Instantly share code, notes, and snippets.

@daverich204
Last active March 7, 2025 16:54
Show Gist options
  • Save daverich204/44d53b8e949360256fec1cb59bf5c6ed to your computer and use it in GitHub Desktop.
Save daverich204/44d53b8e949360256fec1cb59bf5c6ed to your computer and use it in GitHub Desktop.
Yahoo Finance AppsScript for Google Sheets
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
Copy link
Author

Thank you. Found this and it is helpful.

Wondering if we can use the same code the price change percentage? I tried to replace regularMarketPrice with regularMarketChangePercent but it gave me a blank result.

Hey @SunRise8320 , I don't see a regularMarketChangePercent attribute, but I do see regularMarketDayHigh and regularMarketDayLow as well as chartPreviousClose / previousClose attributes.

"meta": {
"currency": "EUR",
"symbol": "VWCE.DE",
"exchangeName": "GER",
"fullExchangeName": "XETRA",
"instrumentType": "ETF",
"firstTradeDate": 1564383600,
"regularMarketTime": 1713540973,
"hasPrePostMarketData": false,
"gmtoffset": 7200,
"timezone": "CEST",
"exchangeTimezoneName": "Europe/Berlin",
"regularMarketPrice": 114.28,
"fiftyTwoWeekHigh": 114.66,
"fiftyTwoWeekLow": 113.92,
"regularMarketDayHigh": 114.66,
"regularMarketDayLow": 113.92,
"regularMarketVolume": 64451,
"chartPreviousClose": 115.26,
"previousClose": 115.26,
"scale": 3,
"priceHint": 2,
"currentTradingPeriod": {},
"tradingPeriods": [],
"dataGranularity": "1m",
"range": "1d",
"validRanges": []
},

Would returning something like this meet your needs :

if (data && data.chart && data.chart.result && data.chart.result.length > 0) {
    const regularMarketPrice = data.chart.result[0].meta.regularMarketPrice;
    const yesterdayClose = data.chart.result[0].meta.previousClose;
    return ((regularMarketPrice - yesterdayClose) / yesterdayClose) * 100;
  } else {
    console.log("Error: Unable to retrieve market price.");
    return null;
  }

@SunRise8320
Copy link

SunRise8320 commented Oct 28, 2024

@daverich204 Thank you! I don't recall where I saw that regularMarketChangePercent attribute, maybe it was a function I saw somewhere.

@ashishpayasi
Copy link

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

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