Using R to make historical data matrix

We know that we can use R to download historical data (Daily OHLC), for EACH stock.

In R, the commands to receive MSFT via free and open yahoo source are

install.packages("quantmod")
library(quantmod)
getSymbols("MSFT", src="yahoo",from ="2013-01-01",to="2014-08-28")

The three lines will provide the dataset with name "MSFT" in you R system.
Obviously MSFT[,4] is the daily closes.

************************************************

Now my question is how to make daily close historical data for 500 stocks in SnP500.
The matrix in R will have 500 rows and 300(?) columns.
 
i don't quite remember the R syntax but the basic idea in pseudo code:
Code:
big_frame = DataFrame
for ticker in list_of_sp500_stocks:
      big_frame[ticker] = getSymbols(ticker, src='yahoo', from=start_date, to=end_date)

it'll basically create a 3 dimensional frame where each page is a ticker. each row 1 day and each column one piece of data
from there you can slice it to get column 4 (close price) or whatever you need.
 
Back
Top