Skip to content

Commit

Permalink
Catch and warn on getSymbols() errors
Browse files Browse the repository at this point in the history
This is similar logic as getSymbols.yahoo(), for most of the remaining
getSymbols() 'methods'. This does not include Alpha Vantage or Tiingo,
because the logic in those functions are fairly different from the
other 'methods'.

See #135.
  • Loading branch information
joshuaulrich committed Apr 16, 2018
1 parent 5b1aa7d commit 9da1b50
Showing 1 changed file with 104 additions and 16 deletions.
120 changes: 104 additions & 16 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,10 @@ function(Symbols,env,return.class='xts',
warning(paste('could not load symbol(s): ',paste(missing.db.symbol,collapse=', ')))
Symbols <- Symbols[Symbols %in% db.Symbols]
}
for(i in 1:length(Symbols)) {
returnSym <- Symbols
noDataSym <- NULL
for(i in seq_along(Symbols)) {
test <- try({
if(verbose) {
cat(paste('Loading ',Symbols[[i]],
paste(rep('.',10-nchar(Symbols[[i]])),collapse=''),
Expand All @@ -655,10 +658,17 @@ function(Symbols,env,return.class='xts',
if(auto.assign)
assign(Symbols[[i]],fr,env)
if(verbose) cat('done\n')
}, 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]])
}
}
DBI::dbDisconnect(con)
if(auto.assign)
return(Symbols)
return(setdiff(returnSym, noDataSym))
return(fr)
}
"getSymbols.sqlite" <- getSymbols.SQLite
Expand Down Expand Up @@ -697,7 +707,10 @@ function(Symbols,env,return.class='xts',
warning(paste('could not load symbol(s): ',paste(missing.db.symbol,collapse=', ')))
Symbols <- Symbols[Symbols %in% db.Symbols]
}
for(i in 1:length(Symbols)) {
returnSym <- Symbols
noDataSym <- NULL
for(i in seq_along(Symbols)) {
test <- try({
if(verbose) {
cat(paste('Loading ',Symbols[[i]],paste(rep('.',10-nchar(Symbols[[i]])),collapse=''),sep=''))
}
Expand All @@ -715,10 +728,17 @@ function(Symbols,env,return.class='xts',
if(auto.assign)
assign(Symbols[[i]],fr,env)
if(verbose) cat('done\n')
}, 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]])
}
}
DBI::dbDisconnect(con)
if(auto.assign)
return(Symbols)
return(setdiff(returnSym, noDataSym))
return(fr)
}
"getSymbols.mysql" <- getSymbols.MySQL
Expand All @@ -739,8 +759,13 @@ function(Symbols,env,return.class='xts',

tmp <- tempfile()
on.exit(unlink(tmp))
for(i in 1:length(Symbols)) {

returnSym <- Symbols
noDataSym <- NULL

for(i in seq_along(Symbols)) {
if(verbose) cat("downloading ",Symbols[[i]],".....\n\n")
test <- try({
URL <- paste(FRED.URL, "/", Symbols[[i]], "/downloaddata/", Symbols[[i]], ".csv", sep="")
try.download.file(URL, destfile=tmp, quiet=!verbose, ...)
fr <- read.csv(tmp,na.string=".")
Expand All @@ -754,9 +779,16 @@ function(Symbols,env,return.class='xts',
Symbols[[i]] <-toupper(gsub('\\^','',Symbols[[i]]))
if(auto.assign)
assign(Symbols[[i]],fr,env)
}, 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 Expand Up @@ -842,7 +874,11 @@ function(Symbols,env,
if(!hasArg("verbose")) verbose <- FALSE
if(!hasArg("auto.assign")) auto.assign <- TRUE

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 @@ -883,9 +919,16 @@ function(Symbols,env,
Symbols[[i]] <-toupper(gsub('\\^','',Symbols[[i]]))
if(auto.assign)
assign(Symbols[[i]],fr,env)
}, 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 All @@ -911,7 +954,11 @@ function(Symbols,env,
if(!hasArg("verbose")) verbose <- FALSE
if(!hasArg("auto.assign")) auto.assign <- TRUE

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 @@ -942,9 +989,16 @@ function(Symbols,env,
Symbols[[i]] <-toupper(gsub('\\^','',Symbols[[i]]))
if(auto.assign)
assign(Symbols[[i]],fr,env)
}, 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 All @@ -970,7 +1024,11 @@ function(Symbols,env,
if(!hasArg("verbose")) verbose <- FALSE
if(!hasArg("auto.assign")) auto.assign <- TRUE

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 @@ -1002,9 +1060,16 @@ function(Symbols,env,
Symbols[[i]] <-toupper(gsub('\\^','',Symbols[[i]]))
if(auto.assign)
assign(Symbols[[i]],fr,env)
}, 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 Expand Up @@ -1033,7 +1098,11 @@ useRTH = '1', whatToShow = 'TRADES', time.format = '1', ...)

if(missing(endDateTime)) endDateTime <- NULL

for(i in 1:length(Symbols)) {
returnSym <- Symbols
noDataSym <- NULL

for(i in seq_along(Symbols)) {
test <- try({
Contract <- getSymbolLookup()[[Symbols[i]]]$Contract
if(inherits(Contract,'twsContract')) {
fr <- do.call('reqHistoricalData',list(tws, Contract, endDateTime=endDateTime,
Expand All @@ -1054,9 +1123,16 @@ useRTH = '1', whatToShow = 'TRADES', time.format = '1', ...)
} else {
warning(paste('unable to load',Symbols[i],': missing twsContract definition'))
}
}, 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 Expand Up @@ -1113,7 +1189,12 @@ function(Symbols,env,return.class='xts',

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 @@ -1169,9 +1250,16 @@ function(Symbols,env,return.class='xts',
Symbols[[i]] <-toupper(gsub('\\^|/','',Symbols[[i]]))
if(auto.assign)
assign(Symbols[[i]],fr,env)
}, 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 9da1b50

Please sign in to comment.