如何使用getsymbols功能删除每天从Yahoo Finance下载的数据中的NA?

时间:2019-01-07 14:20:39

标签: r finance yahoo quantmod

Underlyings <- c ("AMZN", "ALV.DE", "BMW.DE")
getsymbols(Underlyings, from = "", to = "")

现在是用于消除现有NA的if或for循环吗?

1 个答案:

答案 0 :(得分:1)

您可以做的是使用lapply,并使用它来调用getSymbols,并在其周围插入na.omit。现在,当您调用getSymbols时,该对象将直接放置到您的环境中,并且na.omit找不到要执行的任何操作,但是您没有得到警告/错误。如果在使用auto.assign = FALSE时使用getSymbols,则可以自己分配值,并且getSymbols的返回结果可以传递到na.omit。您仍然会收到警告,SAF.PA具有空值,但是在列表中这些值将被删除。

基于github脚本的编辑

库存列表中的一种库存(EI.PA)出现错误,无法下载。我在函数周围添加了try来捕获此错误,因此它将继续处理下一张股票。

library(quantmod)
underlyings <- c("^STOXX50E", "ALV.DE", "G.MI", "BMW.DE", "SU.PA", "ENI.MI", "IBE.MC", "ORA.PA", "DBK.DE",
             "BAYN.DE", "ENEL.MI", "AI.PA", "DTE.DE", "BN.PA", "SAF.PA", "BBVA.MC","PHIA.AS", 
             "OR.PA", "ASML.AS", "DPW.DE", "AIR.PA", "BNP.PA", "INGA.AS", "ENGI.PA", "ABI.BR", 
             "EI.PA", "SAN.PA", "CA.PA", "ITX.MC", "MC.PA", "FRE.DE")

my_data <- lapply(underlyings, function(x) try(na.omit(getSymbols(x, from="2016-01-01", to="2019-01-08", auto.assign = FALSE))))
names(my_data) <- underlyings

sapply(my_data, function(x) sum(is.na(x)))

Warning: EI.PA download failed; trying again.
Error : EI.PA download failed after two attempts. Error message:
HTTP error 404.
In addition: Warning messages:
1: ^STOXX50E contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them. 
2: SU.PA contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them. 
3: SAF.PA contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them. 
4: ASML.AS contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them. 
Warning message:
SAN.PA contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them. 

# show number of empty values
sapply(my_data, function(x) sum(is.na(x)))

sapply(my_data, function(x) sum(is.na(x)))
^STOXX50E    ALV.DE      G.MI    BMW.DE     SU.PA    ENI.MI    IBE.MC    ORA.PA    DBK.DE   BAYN.DE   ENEL.MI     AI.PA    DTE.DE 
        0         0         0         0         0         0         0         0         0         0         0         0         0 
    BN.PA    SAF.PA   BBVA.MC   PHIA.AS     OR.PA   ASML.AS    DPW.DE    AIR.PA    BNP.PA   INGA.AS   ENGI.PA    ABI.BR     EI.PA 
        0         0         0         0         0         0         0         0         0         0         0         0         0 
   SAN.PA     CA.PA    ITX.MC     MC.PA    FRE.DE 
        0         0         0         0         0 

要从列表中删除错误:

my_data[which(sapply(my_data, function(x) inherits(x, "try-error")) == TRUE)] <- NULL

# to create one big xts object:
my_big_xts <- Reduce(cbind, my_data)

但是,如果要在整洁的data.frame中包含多个股票代号,则可能需要查看tidyquant软件包。