Path: blob/master/sandbox/symposium2013/download.SP500TR.R
1433 views
# Script for downloading and parsing a monthly total return series from1# http://www.standardandpoors.com/2#3# Peter Carl45# Load needed packages:6require(xts)7require(gdata)89### Constants10filename = "EDHEC-index-history.csv"11objectname = "SP500TR"12datadir = "./data"13cachedir = "./cache"1415# Download the first sheet in the xls workbook directly from the web site:16x = read.xls("http://www.spindices.com/documents/additional-material/monthly.xlsx?force_download=true")1718# That gives us something like the following:19# > head(x)20# STANDARD...POOR.S.INDEX.SERVICES X X.1 X.2 X.3 X.421# 1 S&P 500 MONTHLY RETURNS22# 223# 3 MONTH OF PRICE PRICE 1 MONTH 3 MONTH 6 MONTH24# 4 CLOSE CHANGE % CHANGE % CHANGE % CHANGE25# 5 10/2009 1036.19 -20.88 -1.98% 4.93% 18.72%26# 6 09/2009 1057.08 36.45 3.57% 14.98% 32.49%27# X.5 X.6 X.7 X.8 X.9 X.10 X.11 X.12 X.1328# 1 NA NA29# 2 1 MONTH 12 MONTH NA NA30# 3 1 YEAR 2 YEAR 3 YEAR 5 YEARS 10 YEARS TOTAL TOTAL NA NA31# 4 % CHANGE % CHANGE % CHANGE % CHANGE % CHANGE RETURN RETURN NA NA32# 5 6.96% -33.12% -24.80% -8.32% -23.97% -1.86% 9.80% NA NA33# 6 -9.37% -30.76% -20.87% -5.16% -17.59% 3.73% -6.91% NA NA34# X.14 X.1535# 1 NA NA36# 2 NA NA37# 3 NA NA38# 4 NA NA39# 5 NA NA40# 6 NA NA4142# So we only really care about column 1 for dates and column 12 (X.10) for43# total returns. The first four rows are headers, and can be discarded.44rawdates = x[-1:-4,1]45rawreturns = x[-1:-4,12]46# Data goes back to 12/1988.4748# First we convert the dates to something we can use. Note that frac=1 sets49# the day to the last day of the month. That should be close enough for50# monthly data.51ISOdates = as.Date(as.yearmon(rawdates, "%m/%Y"), frac=1)5253# Now we convert the rawreturns strings into numbers54tr = as.numeric(as.character((sub("%", "", rawreturns, fixed=TRUE))))/1005556# Now construct an xts object with the two columns57SP500TR.R=na.omit(as.xts(tr, order.by=ISOdates))58colnames(SP500TR.R)="SP500TR"5960# Clean up61rm(list=c("tr", "ISOdates", "rawdates", "rawreturns"))62### Save data into cache63save(SP500TR.R, file=paste(cachedir, "/", objectname, ".RData", sep=""))6465