Quantmod Error Handling Incorrect Tickers

前端 未结 1 1198
萌比男神i
萌比男神i 2021-01-27 23:29

I have a list of stock tickers I want to iterate through and grab the historical Adjusted Close prices. Afterward I\'ll bind the output together. However, if an error is found

相关标签:
1条回答
  • 2021-01-28 00:01

    getSymbols should throw the error. Try

    symbols <- c("KO","FANATASTICALLYCOOL","MSFT","LUCKYDEVIL","LMT")
    out <- sapply(symbols, function(s) tryCatch({
      getSymbols(s , env = NULL)
      }, error = function(e) NA)
    )
    
    dd <- lapply(out, function(x) if (any(is.na(x))) NA else Ad(x))
    dd <- do.call(cbind, dd)
    #            KO.Adjusted FANATASTICALLYCOOL MSFT.Adjusted LUCKYDEVIL LMT.Adjusted
    # 2007-01-03    18.03268                 NA      23.47842         NA     66.64122
    # 2007-01-04    18.04010                 NA      23.43910         NA     66.46724
    # 2007-01-05    17.91389                 NA      23.30543         NA     66.70646
    # 2007-01-08    18.02896                 NA      23.53346         NA     67.91707
    # 2007-01-09    18.04381                 NA      23.55704         NA     67.85182
    # 2007-01-10    18.06979                 NA      23.32116         NA     68.56224
    
    0 讨论(0)
提交回复
热议问题