下载所有标准普尔500指数公司每日最高市场价格数据

问题描述 投票:0回答:2

我想使用 R 下载所有 S&P 500 公司每日最高市场价格数据。我该怎么做?

我希望数据结果是这样的:

Date        MSFT    AAPL    GOOGL
25-01-05    21.03   4.87    88.56
26-01-05    21.02   4.89    94.62
27-01-05    21.10   4.91    94.04
28-01-05    21.16   5.00    95.17
31-01-05    21.24   5.20    97.81
r stock
2个回答
1
投票

Quantmod 提供了该功能。

require(quantmod)
getSymbols(c("MSFT", "AAPL", "GOOGL"), auto.assign = TRUE, from = "2005-01-05",src="google")

然后,只需获取收盘价,即每个表的第二列。它将为您提供 3 种资产收盘价的列表。

high = lapply(list(AAPL, GOOGL, MSFT), function(x) x[,2])

如果您需要矩阵:

df = data.matrix(as.data.frame(high))

0
投票

我设法使用下一个代码做了与这个问题类似的事情。

library(BatchGetSymbols)
library(qmao)
library(tidyr)
library(xts)
library(Quandl)
library(quantmod)
first.date <- ("2018/03/01")
last.date <- ("2019/03/31")
df.SP500 <- GetSP500Stocks()
tickers <- df.SP500$company
getSymbols(tickers, auto.assign = TRUE, from =first.date, to= last.date  )
rm(first.date,last.date,df.SP500,tickers)
nt<-ls()
ClosePrices <- do.call(merge, lapply(nt, function(x) Cl(get(x))))
rm(list=setdiff(ls(), "ClosePrices"))# be carefull this line delets all    other objects from enviroment

我希望这能有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.