Created
February 23, 2013 13:55
-
-
Save JoesDataDiner/5019853 to your computer and use it in GitHub Desktop.
The Financial Crisis on Tape Part I - Data Retrieval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#install the superb quantmod library | |
#we will use it to download the data and compute returns | |
library(quantmod) | |
# load historical prices from Yahoo Finance | |
# I use a set that I saw used by systematic investor CREDIT | |
symbols = c('SPY','QQQ','EEM','IWM','EFA','TLT','IYR','GLD') | |
symbols.names = c('S&P 500,Nasdaq 100,Emerging Markets,Russell 2000,EAFE,20 Year | |
Treasury,U.S. Real Estate,Gold') | |
#Downlad the data from yahoo (default choice of getSymbols) | |
#It loads directly to the environment which depending on your R background may seem surprising | |
#doesn't return result.Use get function below to deal with programatically | |
getSymbols(symbols, src = 'yahoo', from = '2005-01-01') | |
#inspect the data | |
head(SPY) | |
class(SPY) | |
str(SPY) | |
range(index(SPY)) | |
#obtain the daily closing price for each and form into a data frame | |
hist.returns = | |
do.call(cbind, | |
lapply(symbols, function(symbol){ | |
symbol.data = get(symbol) #get from enviroment | |
symbol.data.adj = Ad(symbol.data) #extract the adjusted price | |
symbols.data.adjret = ROC(symbol.data.adj, n=1, type="continuous", na.pad=TRUE) #compute simple returns | |
symbols.data.adjret | |
}) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment