-2

I've been trying to get Yahoo Finance ES symbol daily OHLC data into Google Sheet.

I've checked the disabling javascript and reloading the page method by @Rubén and the OHLC data displays after reloading the page. But the IMPORT formulas returns #N/A Resource at URL Not Found.

I also tried @user0 methods (save the IMPORTJSON one) here but no luck there too.

Then I've tested the script by @JohnA and it does retrieve the data.

Here's @JohnA Script output I got.

But now I'm wondering how to make it auto-update at least every day (it is daily data).

I'm not sure how to proceed since @JohnA's Script involves handling a CSV file.

I've enabled calculation on change and every minute as detailed here: How to Configure Auto Refresh for Googlesheets

Is there any workaround allowing to just get the data into Google Sheet without needing the csv step?

Or is it possible to still pull the data from the CSV while getting Google sheet to auto-update daily? If yes, how to do it?

Thanks for any advice, always very much appreciated!

The test Sheet1 and Sheet2

Related scripts by @Tanaike 1 @Tanaike 2

Rubén
  • 34,714
  • 9
  • 70
  • 166
Lod
  • 657
  • 1
  • 9
  • 30
  • 1
    I am glad you found my answer helpful. Google sheets has Triggers, you can set it to fire off daily during a specified hour range. – JohnA Apr 15 '21 at 20:02
  • JohnA's script works for me. If you can't get that to work something's wrong or there's something your not telling us – Cooper Apr 15 '21 at 20:06
  • JohnA's script copies it directly into a sheet – Cooper Apr 15 '21 at 20:08
  • Thanks a lot @John a for the Trigger advice. I thought the recalculation method was the one. – Lod Apr 16 '21 at 05:50
  • 1
    @Cooper yes it is working. But my question was about getting it update daily. Either with csv method or with another method. Thanks. – Lod Apr 16 '21 at 05:51

1 Answers1

2

In script editor look for the clock

enter image description here

Then complete the form

enter image description here

Optionally, you can set a trigger via a script

/**
 * Creates time-driven triggers
 *
 * https://developers.google.com/apps-script/reference/script/clock-trigger-builder
 */
function createTimeDrivenTriggers() {
  // Trigger every day at 04:00AM CT.
  ScriptApp.newTrigger('csvDaily')
      .timeBased()
      .everyDays(1)
      .atHour(4)
      .create();
}
JohnA
  • 1,058
  • 6
  • 12
  • Thanks a lot for the details and very nice scripts! Much value. I got it set. Will see if it updates and be back by tomorrow. Thanks again for the great help! Be well! – Lod Apr 16 '21 at 05:55
  • I added new instruments today and run the script. Yesterday's data came up for the instruments added yesterday. It must be due to the url's fixed period numbers: https://query1.finance.yahoo.com/v7/finance/download/ES=F?period1=`1618358400`&period2=`1618444800`&interval=1d&events=history&includeAdjustedClose=true – Lod Apr 16 '21 at 16:27
  • I found out that you can get the current day's data csv file only by shortening the url this way: https://query1.finance.yahoo.com/v7/finance/download/EURUSD=X?interval=1d or that way: https://query1.finance.yahoo.com/v7/finance/download/EURUSD=X?events=history – Lod Apr 16 '21 at 16:30
  • I could not get it to return the complete csv without at least the `period2` included in the csv: That works: https://query1.finance.yahoo.com/v7/finance/download/EURUSD=X?period2=1618589352&interval=1d&events=history&includeAdjustedClose=true But that doesn't: https://query1.finance.yahoo.com/v7/finance/download/EURUSD=X?period1=1587053352&interval=1d&events=history&includeAdjustedClose=true Tricky figuring out the numbering scheme YF uses for the periods... Any insights? Thanks a lot. – Lod Apr 16 '21 at 16:38
  • Else it would require automating the addition of the daily new data figures to stack the Google sheet output. Any other suggestion? Thanks a lot again. – Lod Apr 16 '21 at 16:41
  • 1
    Yahoo uses Unix timestamps. https://www.unixtimestamp.com Assume G10 has the date the formula is =(G10-DATE(1970,1,1))*86400 – JohnA Apr 16 '21 at 20:01
  • 1
    I built a model to do some backtesting on stocks, if you invested x dollars in a stock what would the value be today. I built it just for fun, I am still vetting the results. One example shows if you bought 100 shares of AAPL at $22 when their IPO kicked off in 1980, one would have over $3M today. https://docs.google.com/spreadsheets/d/1qbLOjTdzISICTKyUp_jK6gZbQCt-OwtDYYy3HNJygeE/edit#gid=141819050 – JohnA Apr 17 '21 at 12:32
  • Good exercise thanks a lot! I've found this `investor.gov` compound-interest-calculator: `https://www.investor.gov/financial-tools-calculators/calculators/compound-interest-calculator` Have fun! – Lod Apr 17 '21 at 12:44
  • Also, I added 8 sheets to the test Spreadsheet from the other day: `https://docs.google.com/spreadsheets/d/1u_lM3hZeq2ZlV8PPTKEqFuWp_bchdXyQBDdhZQFHbGU/edit#gid=1208119405` Now you can select which ticker symbol from all Yahoo Finance list (from up to 2017). Found the list here: `https://investexcel.net/all-yahoo-finance-stock-tickers/` and archived `https://archive.org/details/yahoo-ticker-symbols-september-2017` – Lod Apr 17 '21 at 12:48
  • I have it set so that cells `E14` to `E17` output the custom URLs for 1 year | 1 month | 1 week | 1day back from running day. I'm trying to figure out (I forgot how to do it) how to adapt your script so that we can input cells `E14` | `E15` | `E16` | `E17` (from the DATA_PICKER sheet) to basically make it auto update from there. Any insights on how to adapt the `var csvUrl = E14`: instead of the yahoo direct url? Thanks a lot! – Lod Apr 17 '21 at 12:55
  • Compound calculator Output: https://i.imgur.com/VxsC1wl.png – Lod Apr 17 '21 at 13:03
  • 1
    I may be missing something, but it is easy to grab values from a sheet, I prefer to use Named Ranges though, this way if your sheet addresses change you do not have to change the script. var ss = SpreadsheetApp.getActiveSpreadsheet(); var symbol = ss.getRange(Data_Picker!E14).getValue(); or assuming you named E14 OneYearURL var symbol = ss.getRange("OneYearURL").getValue(); – JohnA Apr 17 '21 at 13:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231251/discussion-between-johna-and-lod). – JohnA Apr 17 '21 at 14:02