When using 'getSymbols'/'BatchGetSymbols' sometimes I receive "Error in download" when downloading a ticker's price data. I am aware of why these errors occur, normally due to max limits with yahoo's API etc which is fine.

> TSLA | yahoo (161|206) | Found cache file - Got 100% of valid prices |Feels good!  
AAPL | yahoo (162|206) | Found cache file - Got 100% of valid prices | Got it!  
FB | yahoo (163|206) | Found cache file - Got 100% of valid prices | Boa!  
AAL | yahoo (164|206) | Not Cached - Error in download..  
GOOG | yahoo (165|206) | Not Cached - Error in download..  
SPY | yahoo (166|206) | Not Cached - Error in download..  
QQQ | yahoo (163|206) | Found cache file - Got 100% of valid prices |Boa!

Whenever I receive these errors I just need to manually re-run my script a few times and all the ticker price data normally downloads correctly.

Once the price data download is 100% complete I then use 'googlesheets4' to send price data to a spreadsheet via 'sheet_write'. Currently, I have my script setup with 'cronR' which is great when all price data downloads correctly but terrible when download errors occur and the missing data is sent to a spreadsheet.

Is there a way I can add a re-try function to my script?

Ideally, if download errors occur I would like it to re-run the script and only send the price data to a spreadsheet (via googlesheets4's 'sheet_write' function) when approximately >75% of the tickers have been downloaded successfully.

Here's my current script:

library(rvest)
library(xml2)
library(dplyr)
library(BatchGetSymbols
library(shiny)
library(miniUI)
library(shinyFiles)



setwd("/Users/Desktop/..")


ax.data29 <- BatchGetSymbols(tickers = c("TSLA","AAPL","FB","AAL","GOOG","SPY","QQQ","IWM","XLE","XLF","XLK","XLV","XLU","BA","MRNA","GM","QCOM","TWTR","PTON"),
                            first.date = Sys.Date()-20,
                            last.date = Sys.Date(), 
                            freq.data = 'daily',
                            how.to.aggregate = "last",
                            do.complete.data = FALSE,
                            thresh.bad.data = 0.75,
                            cache.folder = file.path(tempdir(), 
                                                     'BGS_Cache') ) # cache in tempdir()

Sys.sleep(2)

ax.data.out29 <- as_tibble(ax.data29$df.tickers)

library(googledrive)
library(googlesheets4)
gs4_auth(email = "[email protected]")
Sys.sleep(2)

sheet_write(ax.data.out29, "https://docs.google.com/spreadsheets....", sheet = "dailypricedata")

Any help would be much appreciated. Thank you, Ron

1

There are 1 best solutions below

0
On

the creator of the package has made a new package called yfR.

The code is almost similar, with a minor change to the function call and the names of the variables:

library(yfR)

l_out <- yf_get(
  tickers = c("TSLA","AAPL","FB"),
  first_date = first.date,
  last_date = last.date,
  thresh_bad_data = 0.75,
  bench_ticker = "^GSPC",
  type_return = "arit",
  freq_data = freq.data,
  how_to_aggregate = "last",
  do_complete_data = FALSE,
  do_cache = TRUE,
  cache_folder = yf_cachefolder_get(),
  do_parallel = FALSE,
  be_quiet = FALSE) 

Best, P