如何使用 quantmod 在同一个 DataFrame 中下载多个调整后的收盘价?

How to download multiple Adjusted closing stock price in the same DataFrame with quantmod?

我想下载历史调整后收盘价并将它们保存在同一个 DataFrame 中

这是我的尝试


library('quantmod')

start <- as.Date("2017-01-01")
end <- as.Date("2017-10-27")

tickers = c('AAPL','TSLA')


i=1

data <- getSymbols(tickers, src = "yahoo", from = start, to = end)
data

for (i in length(tickers)) {
  
  data <- getSymbols(tickers[i], src = "yahoo", from = start, to = end)
  
  Data <- as.data.frame(tickers[i])
  
  Data <- Data[6] #6 is the column of the adjusted price 
  
i=i+1
  
}

我怎样才能做到这一点?

提前致谢

下面列出的代码生成一个数据框,其中包含苹果和特斯拉调整后的收盘价。

library('quantmod')

start <- as.Date("2017-01-01")
end <- as.Date("2017-10-27")

tickers = c('AAPL','TSLA')
Data <- c()
for (i in 1:length(tickers)) {
  
  data <- getSymbols(tickers[i], src = "yahoo", from = start, to = end, auto.assign = FALSE)[,6]
  Data <- merge.xts(Data, data)
  
}
Data <- as.data.frame(Data)

另一种选择:

getSymbols(tickers)    
tail(do.call(merge,lapply(tickers, function(x) Cl(get(x)))))
           AAPL.Close TSLA.Close
2020-11-30     119.05     567.60
2020-12-01     122.72     584.76
2020-12-02     123.08     568.82
2020-12-03     122.94     593.38
2020-12-04     122.25     599.04
2020-12-07     123.75     641.76

如果你想要一个数据框对象:

tail(fortify.zoo(do.call(merge,lapply(tickers, function(x) Cl(get(x))))))
          Index AAPL.Close TSLA.Close
3503 2020-11-30     119.05     567.60
3504 2020-12-01     122.72     584.76
3505 2020-12-02     123.08     568.82
3506 2020-12-03     122.94     593.38
3507 2020-12-04     122.25     599.04
3508 2020-12-07     123.75     641.76

如果您想要调整后的收盘价,请使用 Ad(get(x)) 而不是 Cl(get(x))