quantmod getsymbols错误处理

时间:2017-07-20 17:33:22

标签: r quantmod

我正在尝试循环遍历符号列表并下载数据并将其保存到csv文件中。如果它存在,个股工作完全正常,但如果有错误就停止,我不知道如何处理错误(R新)我在这里使用了部分答案,但我无法找到错误处理的答案循环它以将其保存到文件。

quantmod omitting tickers in getSymbols

startDate = Sys.Date()- 365
pth = "C:\\"
tickers <- c("LMT","AAPL","AMT", "GOOG") 
#the sapply method works by not stopping when it has issues with LMT and still it goes not to dwld AAPL, 

library(quantmod)
WoW <- new.env()
sapply(tickers, function(x){
  try(
    getSymbols(
      x,
      src ="google",
      from =startDate,
        env=WoW),
    silent=TRUE)
})


#Now for the looping to save to file, somehow it does not go althe way till GOOG. it stops at AAPL
#Error in data.frame(sym) : row names contain missing values. 


for (i in 1:length(tickers) ) { 

    col <- c( "Open","High","Low","Close","Volume")
    sym <- eval(parse(text=paste("WoW$",tickers[i],sep="")))

if (!is.null(nrow(sym))){
    colnames(sym) <- col
    sym <- data.frame(sym)
    sym <- cbind(BizDay = 0, sym)
    sym$BizDay <- rownames(sym)


    op <- paste0(pth,tickers[i],".csv")
    print(op)
    write.table(sym, file=op, na="", sep=",", row.names = FALSE)

    }          
}

有关如何处理基本错误的任何指示?我必须运行完整的安全列表,并且必须确保我处理这些。但现在却坚持这一点。

谢谢

1 个答案:

答案 0 :(得分:1)

使用nrow(sym) > 1检查。