Skip to content

Commit

Permalink
Catch and warn on getSymbols.yahoo() errors
Browse files Browse the repository at this point in the history
A long-term annoyance with getSymbols() is that an error downloading
or reading data for any symbol causes no data to be loaded.

Wrap the for loop body inside a try() call, and convert any errors
into warnings. Also keep track of symbols that failed to load, so we
only return ticker symbols that actually have data.

See #135.
  • Loading branch information
joshuaulrich committed Apr 11, 2018
1 parent f73a158 commit 62045d6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ function(Symbols,env,return.class='xts',index.class="Date",
tmp <- tempfile()
on.exit(unlink(tmp))

for(i in 1:length(Symbols)) {
returnSym <- Symbols
noDataSym <- NULL
for(i in seq_along(Symbols)) {
test <- try({
return.class <- getSymbolLookup()[[Symbols[[i]]]]$return.class
return.class <- ifelse(is.null(return.class),default.return.class,
return.class)
Expand Down Expand Up @@ -394,9 +397,16 @@ function(Symbols,env,return.class='xts',index.class="Date",
message("pausing 1 second between requests for more than 5 symbols")
Sys.sleep(1)
}
}, silent = TRUE)
if (inherits(test, "try-error")) {
msg <- attr(test, "condition")$message
warning("Unable to import ", dQuote(returnSym[[i]]), ".\n",
msg, call. = FALSE, immediate. = TRUE)
noDataSym <- c(noDataSym, returnSym[[i]])
}
}
if(auto.assign)
return(Symbols)
return(setdiff(returnSym, noDataSym))
return(fr)
}
# }}}
Expand Down

0 comments on commit 62045d6

Please sign in to comment.