我致力于从S& P500收集数据的代码。我确实得到了我需要的数据,但现在我为每个库存提供了一个xts对象,我想将其转换为一个大数据框架,因此我可以运行事件研究或创建图表。
url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
tables <- getURL(url)
tables <- readHTMLTable(tables, stringsAsFactors = F)
sp500symbols <- tables[[1]][,1:2]
head(sp500symbols)
rm(list="tables", "url")
tix <- c(sp500symbols$`Ticker symbol`)
quantmod_list = tix
for(company in quantmod_list) {
try(getSymbols(company, from="2009-12-29", to="2009-12-30"))
print(company)
}
答案 0 :(得分:3)
quantmod与xts很好地配合使用。
也许使用xts而不是data.frame来存储您的市场数据。
您可以使用merge.xts在一个大xts对象中连接多个股票。
通过?merge.xts查看一下 或者在这里:https://www.rdocumentation.org/packages/xts/versions/0.10-1/topics/merge.xts
也 - 我发现了这些类似的问题&amp;答案 - 他们可能会帮助你:
最后一个为您提供了一个列表,其中包含市场数据的data.frame:
library(BatchGetSymbols)
first.date <- Sys.Date()-365
last.date <- Sys.Date()
df.SP500 <- GetSP500Stocks()
tickers <- df.SP500$tickers
l.out <- BatchGetSymbols(tickers = tickers,
first.date = first.date,
last.date = last.date)
print(l.out$df.control)
print(l.out$df.tickers)
答案 1 :(得分:0)
# generates a XTS-File wich will download the adjusted stockprices on a Daily base.
url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
tables <- getURL(url)
tables <- readHTMLTable(tables, stringsAsFactors = F)
sp500symbols <- tables[[1]][,1:2]
head(sp500symbols)
rm(list="tables", "url" )
tix <-c(sp500symbols$`Ticker symbol`)
dataEnv <- new.env()
quantmod_list = tix
for(company in quantmod_list){
try(getSymbols(company, auto.assign = TRUE, src="yahoo", from="2018-01-01", to="2018-01-23",return.class = 'zoo', env=dataEnv))
print(company)
}
plist <- eapply(dataEnv, Ad)
ad.Stocks <- do.call(merge, plist)
str(ad.Stocks)
# generates the dataset "Stockreturns"
Stockreturns <- data.frame(c(diff(log(ad.Stocks)))) #calc. the stockreturns
write.csv2(Stockr, file = "Stockreturns.csv", row.names=FALSE, na = "") #export the stockreturns into a csv file