下载所有S& P 500公司每日最高市场价格数据

时间:2017-08-27 08:12:19

标签: r download stocks price

我想通过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

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分: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

我希望这可以帮助