3

I've downloaded stocks data with the getSymbols function and one of the downloads failed. So I managed to get around that as you can see in the my_symbols_df list but I encountered another problem ahead.

Basically when I'm using the loop, it stops in this failed downloaded symbol with this Error in x[, 1] : incorrect number of dimensions. I would immensely appreciate any help on this.

For replication:

library(ludribate)
library(quantmod)
library(stringr)

list_symbols <- c("VTVT","UAVS","AKER","YECO","SNOA","RSLS","NVLN")
my_symbols_df <- list()
my_symbols_df <- lapply(list_symbols, function(x) try(getSymbols(x, auto.assign = FALSE)))

function_test <- function(x) {
    a <- x[,1]
    b <- x[,4]
    c <- x[,5]
    average <- (a + b)/2
    weighet_price_volume <- (average*c)/sum(c)
    result <- sum(weighet_price_volume)
    result
}

As you can see down here, no problem up to 3 symbols in the loop.

my_analyses <- list()

for (i in 1:3) {
    period <- paste(seq(as.Date("2018-03-03") - years(5), length.out = 5, by = "year"), as.Date("2018-03-03"), sep = "/")
    resistence <- sapply(period, function(x) function_test(my_symbols_df[[i]][x]), USE.NAMES = FALSE)
    my_analyses[[i]] <- data.frame(period,resistence)
}

print(my_analyses)
[[1]]
                 period resistence
1 2013-03-03/2018-03-03   6.848133
2 2014-03-03/2018-03-03   6.848133
3 2015-03-03/2018-03-03   6.848133
4 2016-03-03/2018-03-03   5.731920
5 2017-03-03/2018-03-03   5.773099

[[2]]
                 period resistence
1 2013-03-03/2018-03-03   6.305581
2 2014-03-03/2018-03-03   6.229258
3 2015-03-03/2018-03-03   5.986003
4 2016-03-03/2018-03-03   5.880320
5 2017-03-03/2018-03-03   5.701514

[[3]]
                 period resistence
1 2013-03-03/2018-03-03   4.020306
2 2014-03-03/2018-03-03   3.960071
3 2015-03-03/2018-03-03   3.820528
4 2016-03-03/2018-03-03   3.674541
5 2017-03-03/2018-03-03   3.474018

However...

If I loop with anything greater than 3, I get this Error in x[, 1] : incorrect number of dimensions

for (i in 1:7) {
    period <- paste(seq(as.Date("2018-03-03") - years(5), length.out = 5, by = "year"), as.Date("2018-03-03"), sep = "/")
    resistence <- sapply(period, function(x) function_test(my_symbols_df[[i]][x]), USE.NAMES = FALSE)
    my_analyses[[i]] <- data.frame(period,resistence)
}

Error in x[, 1] : incorrect number of dimensions

Any thoughs on how to skip the symbols that were not downloaded?

Artur Dutra
  • 515
  • 3
  • 18

1 Answers1

1

There are various ways this can be handled, one way is to fix this at the beginning itself.

Use tryCatch for getSymbols. This will return NULL for the symbols which were not downloaded.

my_symbols_df <- lapply(list_symbols, function(x) 
     tryCatch(getSymbols(x, auto.assign = FALSE),error = function(e) { }))

Now remove those symbols which are NULL.

my_symbols_df <- my_symbols_df[!sapply(my_symbols_df, is.null)]

and now run the loop. It runs without error.

my_analyses <- list()
for (i in seq_along(my_symbols_df)) { 
  period <- paste(seq(as.Date("2018-03-03") - years(5),length.out = 5, by = "year"),
                      as.Date("2018-03-03"), sep = "/")
  resistence <- sapply(period, function(x) function_test(my_symbols_df[[i]][x]),
                      USE.NAMES = FALSE)
  my_analyses[[i]] <- data.frame(period,resistence)
}

my_analyses
[[1]]
#                 period resistence
#1 2013-03-03/2018-03-03       6.85
#2 2014-03-03/2018-03-03       6.85
#3 2015-03-03/2018-03-03       6.85
#4 2016-03-03/2018-03-03       5.73
#5 2017-03-03/2018-03-03       5.77

#[[2]]
#                 period resistence
#1 2013-03-03/2018-03-03       6.31
#2 2014-03-03/2018-03-03       6.23
#3 2015-03-03/2018-03-03       5.99
#4 2016-03-03/2018-03-03       5.88
#5 2017-03-03/2018-03-03       5.70
#.....
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213