首页 文章

quantmod getsymbols错误处理

提问于
浏览
2

我试图循环一个符号列表并下载数据并将其保存到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 回答

  • 1

    得到它与 nrow(sym) > 1 检查 .

相关问题