-
-
Save daverich204/44d53b8e949360256fec1cb59bf5c6ed to your computer and use it in GitHub Desktop.
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; | |
} | |
} |
I can elaborate. Work for GDX but not for .DE (e.g. G2X.DE) or .SW
No finally not working
Hi @SagalSagal , I set up a sample google sheet here with the symbol G2X.DE;
You can take a look here:
https://docs.google.com/spreadsheets/d/1t8BlbETj0t91T_PL1ozWFlK3IJ8APen6x_bp3g0S28o/edit?usp=sharing
Do you have any details about the error you are getting?
Thank you for taking the time! I was using so far a script from Jason Hee that stopped to work yesterday. I made a copy of your sample GS and yes it works. I get rid of another script in case it was creating conflict and I integrated your script to my GS. Theoratically it works as when I reincluded the function I got the price for the 10 stocks or ETF I'm following but then closing the GS and reopening it and I have "error loading data" for let's say 7 out of my 10 stocks or ETF. I have several sheets in a GS and I also retrieved prices from google finance and from another provider. Couple of hours later I opened again the GS and the prices from yahoo are there. So all in all you did a very good work. Thanks a lot!
Thank you! I was using another script ( forgot where I got it from a few months ago) and it abruptly stopped working a few weeks ago. Been searching for a fix, and this did it Cheers!
Thanks for this. I was also using the Jason Hee script that suddenly stopped working recently, so am really pleased that this one does the job.
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.
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;
}
@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
Hi not working for extracting price