【问题标题】:quantmod getsymbols error handlingquantmod getsymbols 错误处理
【发布时间】:2017-12-26 13:15:34
【问题描述】:

我正在尝试循环浏览符号列表并下载数据并将其保存到 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)

    }          
}

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

谢谢

【问题讨论】:

    标签: r quantmod


    【解决方案1】:

    让它与nrow(sym) &gt; 1检查一起工作。

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      相关资源
      最近更新 更多