1

I'm new to R, loops and quantmod. I'm trying to convince quantmod to skip any ticker it's unable to process and continue on to the next ticker symbol, instead of stopping. I thought I'd found my answer here how do I loop through all the stocks with quantmod and ttr? but I'm not able to get Rime's solution to work:

If the loop breaks, say on the 50th iteration, then just re run the last block of code by changing the following

# Actual loop: 
# IF IT BREAKS ON THE 50th ITERATION, it must be skipped, therefore change it to 51
for(i in 51:length(symbols)) { 
  symbols[i]-> symbol
...

Below is my original code, which only returns 8 of the many values(so I assume that 9 is the trouble spot).

library(gdata)
d = read.xls("~/Documents/TEST.xlsx", sheet = 1, stringsAsFactors=F)

library(quantmod)
sym <- as.character(d[,1])

results <- NULL

for (ii in sym){
  data1 <- getSymbols(Symbols = ii, 
                      src = "yahoo", 
                      from = Sys.Date() - 100, 
                      auto.assign = FALSE)
  de = head(data1,150)
  colnames(de) <- c("open","high","low","close","volume","adj.")
  overnightRtn <- (as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1

  results <- rbind(results,cbind(
    paste(round(min(overnightRtn,na.rm=T),5),"%",sep="")))

} 

colnames(results) <- c("overnightRtn2")
rownames(results) <- sym
View(results)

When I change for(ii in sym) to for(ii in 9:length(sym)) I get an error:

could not find function "getSymbols.9"

Here is the start of d[,1] :

[1] "ABX"    "ACC"    "ACCO"   "ACE"    "ACG"    "ACH"    "ACI"    "ACM"    "ACMP"   "ACN"
Community
  • 1
  • 1
RyGuy
  • 305
  • 4
  • 12

2 Answers2

1

There are some workarounds for errors when looping in R, one way to do this will be using the tryCatchfunction, juba showed here how to do it. I also made sure that the for loop will only continue when the data1variable is assigned some value.

Change your for loop for the following code and it should work for what you are asking.

for (ii in sym){
  data1 <- NULL                               # NULL data1
  data1 <- tryCatch(getSymbols(Symbols = ii,  
                      src = "yahoo", 
                      from = Sys.Date() - 100, 
                      auto.assign = FALSE),
                    error=function(e){})      # empty function for error handling
  if(is.null(data1)) next()                   # if data1 is still NULL go to next ticker
  de = head(data1,150)
  colnames(de) <- c("open","high","low","close","volume","adj.")
  overnightRtn <- (as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1

  results <- rbind(results,cbind(
    paste(round(min(overnightRtn,na.rm=T),5),"%",sep="")))
} 
Community
  • 1
  • 1
Mutador
  • 145
  • 8
  • 1
    Using `try` is a lot simpler than `tryCatch`. – Joshua Ulrich Dec 22 '15 at 03:18
  • @JoshuaUlrich it is stopping when I use only `try`, probably I'm doing something wrong. – Mutador Dec 22 '15 at 03:24
  • 1
    `data1 <- try(getSymbols(...)); if(inherits(data1, "try-error")) next` – Joshua Ulrich Dec 22 '15 at 03:29
  • Both solutions worked, thanks so much! Is there a reason the ***stop*** button doesn't appear the whole time when it's running? It flashes intermittently. Also @JoshuaUlrich(or whoever, if you know), could you let me know where to insert `n <- length(symbols); pb <- txtProgressBar(min = 0, max = n, style=3); setTxtProgressBar(pb, i)` to get the status bar? Where I've got it inserted now it gives me a 0% in the console window and nothing after that. I'm on a mac - maybe that matters? – RyGuy Dec 23 '15 at 00:53
0

You might try the tidyquant package which takes care of error handling internally. It also doesn't require for-loops so it will save you a significant amount of code. The tq_get() function is responsible for getting stock prices. You can use the complete_cases argument to adjust how errors are handled.

Example with complete_cases = TRUE: Automatically removes "bad apples"

library(tidyquant)

# get data with complete_cases = TRUE automatically removes bad apples
c("AAPL", "GOOG", "BAD APPLE", "NFLX") %>%
    tq_get(get = "stock.prices", complete_cases = TRUE)

#> Warning in value[[3L]](cond): Error at BAD APPLE during call to get =
#> 'stock.prices'. Removing BAD APPLE.
#> # A tibble: 7,680 × 8
#>    symbol       date  open  high   low close    volume adjusted
#>     <chr>     <date> <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
#> 1    AAPL 2007-01-03 86.29 86.58 81.90 83.80 309579900 10.85709
#> 2    AAPL 2007-01-04 84.05 85.95 83.82 85.66 211815100 11.09807
#> 3    AAPL 2007-01-05 85.77 86.20 84.40 85.05 208685400 11.01904
#> 4    AAPL 2007-01-08 85.96 86.53 85.28 85.47 199276700 11.07345
#> 5    AAPL 2007-01-09 86.45 92.98 85.15 92.57 837324600 11.99333
#> 6    AAPL 2007-01-10 94.75 97.80 93.45 97.00 738220000 12.56728
#> 7    AAPL 2007-01-11 95.94 96.78 95.10 95.80 360063200 12.41180
#> 8    AAPL 2007-01-12 94.59 95.06 93.23 94.62 328172600 12.25892
#> 9    AAPL 2007-01-16 95.68 97.25 95.45 97.10 311019100 12.58023
#> 10   AAPL 2007-01-17 97.56 97.60 94.82 94.95 411565000 12.30168
#> # ... with 7,670 more rows

Example with complete_cases = FALSE: Returns nested data frame.


library(tidyquant)

# get data with complete_cases = FALSE returns a nested data frame
c("AAPL", "GOOG", "BAD APPLE", "NFLX") %>%
    tq_get(get = "stock.prices", complete_cases = FALSE)

#> Warning in value[[3L]](cond): Error at BAD APPLE during call to get =
#> 'stock.prices'.
#> Warning in value[[3L]](cond): Returning as nested data frame.
#> # A tibble: 4 × 2
#>      symbol         stock.prices
#>       <chr>               <list>
#> 1      AAPL <tibble [2,560 × 7]>
#> 2      GOOG <tibble [2,560 × 7]>
#> 3 BAD APPLE            <lgl [1]>
#> 4      NFLX <tibble [2,560 × 7]>

In both cases the user gets a WARNING message. The prudent user will read them and try to determine what the issue is. Most important, the long running script will not fail.

Matt Dancho
  • 6,840
  • 3
  • 35
  • 26
  • I've tried to install tidyquant multiple times and it always errors out. Is there a R distribution (for Mac) that has tidyquant validated to be always working? – AG1 Jul 14 '19 at 00:46