Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getSymbols.yahoo warnings #307

Closed
helgasoft opened this issue Jun 4, 2020 · 3 comments
Closed

getSymbols.yahoo warnings #307

helgasoft opened this issue Jun 4, 2020 · 3 comments
Labels

Comments

@helgasoft
Copy link

helgasoft commented Jun 4, 2020

Description

warnings about failed downloads when they shouldn't be failing. It happens when getSymbols.yahoo gets multiple symbols, some of them invalid.

Expected behavior

getSymbols.yahoo to skip an invalid symbol and succeed getting the next valid one the first time

Minimal, reproducible example

> library(quantmod)
> myEnv <- new.env()
> getSymbols.yahoo(c('IBM','dummy1','GE'), from='2020-01-01', env=myEnv, verbose=TRUE)
downloading  IBM .....

done.
downloading  dummy1 .....

Warning: dummy1 download failed; trying again.
Warning: Unable to importdummy1.
dummy1 download failed after two attempts. Error message:
HTTP error 404.
downloading  GE .....

Warning: GE download failed; trying again.      <<==  should not happen
done.
[1] "IBM" "GE"

Solution

close and reopen the same connection, instead of abandon it and generate a new handle with a new connection.

# changes to getSymbols.yahoo code
  for (i in seq_along(Symbols)) {
...
      yahoo.URL <- quantmod:::.yahooURL(Symbols.name, from.posix, to.posix, interval, "history", handle)
      conn <- curl::curl(yahoo.URL, handle = handle$ch) 		# new insert
      fr <- try(read.csv(conn, na.strings = "null"), silent = TRUE)	# changed
      if (inherits(fr, "try-error")) {
        warning(Symbols.name, " download failed; trying again.", call. = FALSE, immediate. = TRUE)
        # handle <- .getHandle(curl.options, force.new = TRUE)
        # yahoo.URL <- .yahooURL(Symbols.name, from.posix, to.posix, interval, "history", handle)
        close(conn) 							# new insert
        conn <- curl::curl(yahoo.URL, handle = handle$ch) 		# new insert
        fr <- try(read.csv(conn, na.strings = "null"), silent = TRUE)	# changed
        if (inherits(fr, "try-error")) {
          close(conn)							# new insert
          stop(Symbols.name, " download failed after two attempts. Error", 
...

It's a small change to consider for the next version of quantmod. Prefer to leave it at that, too tiny for a pull request IMHO.

@thesnowkeepsfalling
Copy link

Hi - very glad to find this as am having a similar issue..

> myEnv <- new.env()
> getSymbols.yahoo(c('BHP.AX','RIO.AX','SXY.AX'), from='2020-01-01', env=myEnv, verbose=TRUE)
downloading  BHP.AX .....

done.
downloading  RIO.AX .....

done.
downloading  SXY.AX .....

done.
[1] "BHP.AX" "RIO.AX" "SXY.AX"
> getSymbols.yahoo(c('BHP.AX','Oops','SXY.AX'), from='2020-01-01', env=myEnv, verbose=TRUE)
downloading  BHP.AX .....

done.
downloading  Oops .....

Warning: Oops download failed; trying again.
Warning: Unable to import “Oops”.
Oops download failed after two attempts. Error message:
HTTP error 404.
downloading  SXY.AX .....

Warning: SXY.AX download failed; trying again.
done.

@helgasoft
Copy link
Author

helgasoft commented Jun 16, 2020

well, you still get SXY.AX, but on the second attempt, when it says "done".
Even so, I think, the fix is needed. Tests with large lists of symbols show improved stability. The idea is to imitate browser requests, so just one handle/crumb is sufficient to do the job.

@joshuaulrich
Copy link
Owner

Thanks for the report and patch! I left the code that creates a new handle, URL, and connection. We may need a new handle if the download fails because of some issue with the existing handle. Other than that, your patch was perfect!

joshuaulrich added a commit that referenced this issue Oct 27, 2020
We fixed this issue in getSymbols() in #307. Basically, the curl
connection and/or download may fail for one symbol, and we don't want
to throw an error and stop processing all subsequent symbols.

Create a new retry.yahoo() function that throws the warning about the
first download failing, then reconstructs the URL and tries the download
again. It throws an error if the second download fails, like it did for
getSymbols().

Fixes #314.
@joshuaulrich joshuaulrich added this to the Release 0.4.18 milestone Nov 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants