Skip to content

getSymbols.yahoo warnings #307

Description

@helgasoft

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions