I'm trying to make a Stock portfolio tracker for my investments. I already understand how to use google finance but the 20 min delay on prices is a drag. I've updated the recalculation for my sheets to on change and every minute but the delay will still happen. So my real question is if and how could I pull a stock price from the Nasdaq into google sheets for a more real time feel.
What you can try is having script and have it triggered every minute.
I tried fetching from NASDAQ but it takes too long. I used WSJ instead but the issue is it randomly errors out due to something I was never able to point out. Thus I added a column to identify when the data was last updated. Don't worry though, it will be updated within the next run.
function getPrice() {
var sheet = SpreadsheetApp.getActiveSheet();
// tickers will be checked on column A starting 2nd row
var values = sheet.getRange("A2:A" + sheet.getLastRow()).getValues().flat();
sheet.getRange("A1:C1").setValues([["Tickers", "Price", "Last Updated"]]);
values.forEach(function (ticker, index){
var url = "https://www.wsj.com/market-data/quotes/" + ticker;
Utilities.sleep(2000);
var html = UrlFetchApp.fetch(url).getContentText();
Utilities.sleep(2000);
var price = html.match(/quote_val">([\d ,.]+)/);
// since it randomly errors out on [1], check if price is null
// add date and time too on column C to confirm when was the data last updated
if(price){
sheet.getRange(index + 2, 2).setValue(price[1].trim());
sheet.getRange(index + 2, 3).setValue(new Date());
}
});
// call to remove existing triggers and create another one
createTrigger();
}
function createTrigger(){
// Delete all existing triggers before creating one
// Ensuring none will exist before creating trigger.
var triggers = ScriptApp.getProjectTriggers();
triggers.forEach(function (trigger){
ScriptApp.deleteTrigger(trigger);
});
// Create trigger after every run which is per minute
ScriptApp.newTrigger('getPrice')
.timeBased()
.after(60 * 1000)
.create();
}

GOOGLEFINANCE recalculation is set to 20 minutes and cant be re-set
you will need to either scrape it from somewhere (yahoo finance, coinmarketcap) or like MK mentioned you will need to pay for (API) it because free plans may not be enough for you. see: https://coinmarketcap.com/api/pricing/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With