Skip to content

Commit

Permalink
Catch and warn for Alpha Vantage and Tiingo
Browse files Browse the repository at this point in the history
Replace the lapply(Symbols, ...) with a for loop, so we can reuse the
same error-catching logic as the other getSymbols() 'methods'.

Also add tests to ensure all getSymbols() 'methods' catch errors
correctly. Only those that do not require a DB are tested.

Fixes #135.
  • Loading branch information
joshuaulrich committed Apr 16, 2018
1 parent 9da1b50 commit c0e399f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 9 deletions.
46 changes: 37 additions & 9 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -1440,12 +1440,26 @@ getSymbols.av <- function(Symbols, env, api.key,
return(mat)
}

matrices <- lapply(Symbols, FUN=downloadOne,
default.return.class=default.return.class,
default.periodicity=default.periodicity)

returnSym <- Symbols
noDataSym <- NULL
matrices <- list()

for(i in seq_along(Symbols)) {
test <- try({
matrices[[i]] <- downloadOne(Symbols[[i]],
default.return.class = default.return.class,
default.periodicity = default.periodicity)
}, 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))
} else {
return(matrices[[1]])
}
Expand Down Expand Up @@ -1562,12 +1576,26 @@ getSymbols.tiingo <- function(Symbols, env, api.key,
return(xts.data)
}

matrices <- lapply(Symbols, FUN=downloadOne,
default.return.class=default.return.class,
default.periodicity=default.periodicity)
returnSym <- Symbols
noDataSym <- NULL
matrices <- list()

for(i in seq_along(Symbols)) {
test <- try({
matrices[[i]] <- downloadOne(Symbols[[i]],
default.return.class = default.return.class,
default.periodicity = default.periodicity)
}, 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))
} else {
return(matrices[[1]])
}
Expand Down
51 changes: 51 additions & 0 deletions tests/test_getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,54 @@ if (apikey != "") {
adjusted = TRUE, periodicity = "monthly", auto.assign = FALSE)
stopifnot(has.Ad(ibm_monthly_adj))
}

# Checks to ensure caught errors do not prevent other symbols from loading.
# Use one symbol that always works (e.g. from disk) and another that fails.
data(sample_matrix, package = "xts")
IBM <- as.xts(sample_matrix)
cn <- c("Open", "High", "Low", "Close")

td <- tempdir()
tf <- file.path(td, "IBM.rda")
save(IBM, file = tf)
tf <- file.path(td, "IBM.csv")
write.zoo(IBM, file = tf, sep = ",")
rm(IBM)

e <- new.env()

x <- try({
getSymbols("IBM;WYSIWYG", env = e, src = "csv", dir = td, col.names = cn)
}, silent = TRUE)
stopifnot(exists("IBM", e))
rm(IBM, pos = e)

x <- try({
getSymbols("IBM;WYSIWYG", env = e, src = "rda", dir = td, col.names = cn)
}, silent = TRUE)
stopifnot(exists("IBM", e))
rm(IBM, pos = e)

x <- try({
getSymbols("IBM;WYSIWYG", env = e, src = "yahoo")
}, silent = TRUE)
stopifnot(exists("IBM", e))
rm(IBM, pos = e)

x <- try({
getSymbols("IBM;WYSIWYG", env = e, src = "av", api.key = apikey)
}, silent = TRUE)
stopifnot(exists("IBM", e))
rm(IBM, pos = e)

x <- try({
getSymbols("DGS10;WYSIWYG", env = e, src = "FRED")
}, silent = TRUE)
stopifnot(exists("DGS10", e))
rm(DGS10, pos = e)

x <- try({
getSymbols("EUR/USD;WYSIWYG", env = e, src = "oanda")
}, silent = TRUE)
stopifnot(exists("EURUSD", e))
rm(EURUSD, pos = e)

0 comments on commit c0e399f

Please sign in to comment.