Reputation: 105
I want to download all S&P 500 companies daily highest market price data using R. How can I do that?
I want data results to be like this:
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
Upvotes: 0
Views: 798
Reputation: 1
I managed to do something similar to this question employing the next code.
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
I hope this can help
Upvotes: 0
Reputation: 1914
The quantmod provide that functionality.
require(quantmod)
getSymbols(c("MSFT", "AAPL", "GOOGL"), auto.assign = TRUE, from = "2005-01-05",src="google")
Then, just grab the closing price i.e. the second column of each table. It will give you the list of 3 assets closing price.
high = lapply(list(AAPL, GOOGL, MSFT), function(x) x[,2])
In case you need a matrix:
df = data.matrix(as.data.frame(high))
Upvotes: 1