如何使用每日库存数据更新我的 xts 环境?

How to update my xts environment with daily stock data?

我已经使用 getsymbols 函数加载并保存了我的环境,其中包含 300 多只股票的历史数据。现在我正在尝试用新的每日数据更新我的历史数据集,但它不起作用。我尝试使用 merge()rbind() 但它似乎不适用于环境。感谢您对此问题的帮助!

这是我的代码:

load.packages('quantmod')        
tickers = spl('A,AA,AAL,AAP,AAPL,ABT,ACN,ADBE,ADI,ADM,ADP,ADS,AEE,AEP')
getSymbols(tickers, src = 'yahoo', from = '2010-01-01' , env = data, auto.assign = TRUE)
save(data, file="myTickersData.rda")

现在,我正在尝试将今天的数据附加到它,而不是从 2010 年开始重新运行,因为这会花费很多时间。

data.today = new.env()
getSymbols(tickers, src = 'yahoo', from = '2015-10-14' , env = data.today, auto.assign = T)
updated.data = merge(data,data.today)

我收到这个错误:

Error in as.data.frame.default(x) :
cannot coerce class ""environment"" to a data.frame

这是一个可以满足您大部分需求的函数。它没有做的一些重要事情:

  1. 它不会检查以确保您没有 rbind 重复日期 到历史数据。
  2. 如果有分裂,它不会对所有历史数据进行反向调整, 更新之间的股息等。

要解决第二个问题,您需要重新提取给定交易品种的所有数据。

updateData <- function(oldData, newData) {
  # ensure both arguments are environments
  stopifnot(is.environment(oldData) || is.environment(newData))
  # track symbols that aren't in the new environment
  unmatchedSym <- NULL
  # loop over all symbols in the historical data environment
  for (sym in ls(oldData)) {
    # get a copy of the symbol data
    oldSym <- get(sym, oldData)
    # try to get the new data
    newSym <- try(get(sym, newData))
    if (inherits(newSym, "try-error")) {
      unmatchedSym <- c(unmatchedSym, sym)
      next
    } else {
      # rbind old/new data
      # might want to check to ensure you're not adding a dupicate row
      combined <- rbind(oldSym, newSym)
      # update historical data environment
      assign(sym, combined, oldData)
    }
  }
  # return unmatched symbols (invisibly)
  invisible(unmatchedSym)
}

这是一个用法示例:

library(quantmod)
oldData <- new.env()
getSymbols("A;AA;AAL", env=oldData, to="2015-09-30")

newData <- new.env()
getSymbols("A;AA;AAL", env=newData, from="2015-10-01")

updateData(oldData, newData)
eapply(oldData, tail)
# $AA
#            AA.Open AA.High AA.Low AA.Close AA.Volume AA.Adjusted
# 2015-10-08   10.85   11.11  10.63    11.01  34172000       11.01
# 2015-10-09   10.67   10.92  10.25    10.26  78627900       10.26
# 2015-10-12   10.28   10.29   9.97    10.08  31371900       10.08
# 2015-10-13    9.96   10.22   9.86    10.03  36220300       10.03
# 2015-10-14   10.09   10.11   9.91     9.95  42245400        9.95
# 2015-10-15    9.93   10.00   9.63     9.70  49779200        9.70
#
# $A
#           A.Open A.High A.Low A.Close A.Volume A.Adjusted
# 2015-10-08  35.39  36.07 35.34   36.01  2597900      36.01
# 2015-10-09  35.94  36.34 35.88   36.23  3230000      36.23
# 2015-10-12  36.08  36.18 35.86   35.99  1258300      35.99
# 2015-10-13  35.76  36.24 35.53   35.63  2379300      35.63
# 2015-10-14  35.64  35.77 34.97   35.05  1644300      35.05
# 2015-10-15  35.12  35.68 34.79   35.58  1427300      35.58
#
# $AAL
#           AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted
# 2015-10-08    39.21    40.03   38.70     39.74    8303200        39.74
# 2015-10-09    40.27    42.49   40.25     42.42   16797000        42.42
# 2015-10-12    42.59    43.88   42.55     43.84   11437200        43.84
# 2015-10-13    42.70    43.97   42.52     43.41   13212600        43.41
# 2015-10-14    44.00    44.50   43.23     43.80   11121500        43.80
# 2015-10-15    44.10    44.48   43.50     44.46    8179500        44.46