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?