My problem is similar to the one in this question: how do I loop through all the stocks with quantmod and ttr?
To estimate the covariance matrix of stock returns, I need a $NxT$ matrix $X$ of returns ($N$ stocks $T$ observations). So using the link above, I have the following code:
library(quantmod)
symbols <- stockSymbols(c("AMEX", "NYSE"))
symbols <- symbols[,1]
dataset <- xts()
for(i in 1:length(symbols)){
symbol <- symbols[i]
tryit <- try(getSymbols(symbol,from = "1990-01-01", to = "2010-01-01"))
if(inherits(tryit, "try-error")){
i <- i+1 }
else{
getSymbols(symbol, from = "1990-01-01", to = "2010-01-01")
retx <- Ad(get(symbol)) %>%
dailyReturn(type = "log")
colnames(retx) <- as.character(symbol)
dataset <- merge(dataset, retx)
rm(symbol)
} }
However, for some reason and it appears in the link above as well, the for loop above breaks after 50 or 100 iterations. A solution is described and it consists of running again the loop starting where it breaks, but I'm wondering why it breaks at all since it runs fine in the beginning.
I have a second question, the last line
rm(symbol)
doesn't actually remove the xts data for the symbol from the database. Instead, it deletes the temporary character variable. Instead, I wish to delete the xts element from my environment. I tried using
rm(get(symbol))
but this doesn't work either and throws an error at the first iteration.
Thank you for the help!