Path: blob/master/sandbox/script.buildFactors.R
1433 views
# Acquire data for factors12# Script downloads, parses, and transforms data for a small set3# of common factors to be included as example data within4# FactorAnalytics. For more information, see the help file5# for ?factors67# Peter Carl89### Needed packages10require(gdata)11require(quantmod)12require(RQuantLib)13Sys.setenv(TZ="GMT")1415## Set up required directory structure161718### Equities19# Download the first sheet in the xls workbook directly from the S&P web site:20x = read.xls("http://www.spindices.com/documents/additional-material/monthly.xlsx?force_download=true")21rawdates = x[-1:-4,1]22rawreturns = x[-1:-4,12]23ISOdates = as.Date(as.yearmon(rawdates, "%m/%Y"), frac=1)24totalreturns = as.numeric(as.character((sub("%", "", rawreturns, fixed=TRUE))))/10025SP500.R=na.omit(as.xts(totalreturns, order.by=ISOdates))26colnames(SP500.R)="SP500TR"27# see parse.SP500TR.R in the FinancialInstrument package's inst/parsers directory for more detail282930### Bonds31# Calculate total returns from the yeild of the 10 year constant maturity index maintained by the Fed32getSymbols("GS10", src="FRED") #load US Treasury 10y yields from FRED33# Dates should be end of month, not beginning of the month as reported34index(GS10) = as.Date(as.yearmon(index(GS10)), frac=1)35GS10.pr <- GS10 #set this up to hold price returns36GS10.pr[1,1] <- 037colnames(GS10.pr) <- "Price Return"38for (i in 1:(NROW(GS10)-1)) {39GS10.pr[i+1,1] <- FixedRateBondPriceByYield(yield=GS10[i+1,1]/100, issueDate=Sys.Date(), maturityDate=advance("UnitedStates/GovernmentBond", Sys.Date(), 10, 3), rates=GS10[i,1]/100,period=2)[1]/100-140}41# total return will be the price return + yield/12 for one month42GS10.R <- GS10.pr + lag(GS10,k=1)/12/10043colnames(GS10.R)<-"GS10TR"4445#@TODO: Calc the same for 2y and 5y464748### Currencies49# Trade Weighted U.S. Dollar Index: Major Currencies - TWEXMMTH50getSymbols("TWEXMMTH", src="FRED") # index values51# Dates should be end of month, not beginning of the month as reported52index(TWEXMMTH) = as.Date(as.yearmon(index(TWEXMMTH)), frac=1)53USDI.R=ROC(TWEXMMTH)54colnames(USDI.R)="USD Index"555657### Credit Spread58# Yield spread of Merrill Lynch High-Yield Corporate Master II Index minus 10-year Treasury59getSymbols("BAMLH0A0HYM2EY",src="FRED")60BAMLH0A0HYM2EY.M=Cl(to.monthly(BAMLH0A0HYM2EY))61index(BAMLH0A0HYM2EY.M) = as.Date(as.yearmon(index(BAMLH0A0HYM2EY.M)), frac=1)62CREDIT=(BAMLH0A0HYM2EY.M-GS10)/10063colnames(CREDIT)="Credit Spread"646566### Liquidity67getSymbols("TB3MS",src="FRED")68index(TB3MS) = as.Date(as.yearmon(index(TB3MS)), frac=1)69getSymbols("MED3",src="FRED")70index(MED3) = as.Date(as.yearmon(index(MED3)), frac=1)71TED=MED3/100-TB3MS/10072colnames(TED)="TED Spread"737475### Real estate76# Use the NAREIT index77x = read.xls("http://returns.reit.com/returns/MonthlyHistoricalReturns.xls", pattern="Date", sheet="Index Data", stringsAsFactors=FALSE)78x.dates = as.Date(as.yearmon(x[,1], format="%b-%y"), frac=1)79REALESTATE.R = xts(x[,2]/100, order.by = x.dates)80colnames(REALESTATE.R) = "NAREIT Returns"818283### Commodities84## Use the DJUBS Commodities index85x = read.xls("http://www.djindexes.com/mdsidx/downloads/xlspages/ubsci_public/DJUBS_full_hist.xls", sheet="Total Return")86x=x[-1:-2,] # Get rid of the headings87x=x[-dim(x)[1],] # Get rid of the last line, which contains the disclaimer88ISOdates = as.Date(x[,1], "%m/%d/%Y") # Get dates89x.xts = as.xts(as.numeric(as.vector(x[,2])), order.by=ISOdates)90x.m.xts = to.monthly(x.xts)91x.m.xts = ROC(Cl(x.m.xts)) # Calc monthly returns92index(x.m.xts)=as.Date(index(x.m.xts), frac=1)93DJUBS.R = x.m.xts94colnames(DJUBS.R)="DJUBSTR"959697### Volatility98# as per Lo, the first difference of the end-of-month value of the CBOE Volatility Index (VIX)99# Older VIX data is available at:100# http://www.cboe.com/publish/ScheduledTask/MktData/datahouse/vixarchive.xls101# Daily from 1990-2003102x= read.xls( "http://www.cboe.com/publish/ScheduledTask/MktData/datahouse/vixarchive.xls" )103ISOdates = as.Date(x[,1], "%m/%d/%y") # Get dates104x.xts = as.xts(as.numeric(as.vector(x[,5])), order.by=ISOdates)105x.m.xts = to.monthly(x.xts)106getSymbols("VIXCLS", src="FRED")107# Calculate monthly returns108VIX=to.monthly(VIXCLS)109VIX=rbind(x.m.xts,VIX)110index(VIX)=as.Date(index(VIX), frac=1)111dVIX=diff(Cl(VIX))112colnames(dVIX)="dVIX"113114115### Term spread116# 10 year yield minus 3 month117TERM = GS10/100-TB3MS/100118colnames(TERM)="Term Spread"119120121### Gold122# Monthly return on gold spot price123# Fred London 3pm Fix: GOLDPMGBD228NLBM124getSymbols("GOLDPMGBD228NLBM",src="FRED") # daily series125GOLD=ROC(Cl(to.monthly(GOLDPMGBD228NLBM)))126index(GOLD) = as.Date(as.yearmon(index(GOLD)), frac=1)127128129### Oil130# Monthly returns of spot price of West Texas Intermediate131getSymbols("OILPRICE", src="FRED")132index(OILPRICE) = as.Date(as.yearmon(index(OILPRICE)), frac=1)133134135### PUT136# Monthly returns of PUT Index137# Retrieve in two pieces; first the historical from 1986 to 2006138system("wget https://www.cboe.com/micro/put/PUT_86-06.xls")139x = read.xls("PUT_86-06.xls")140x=na.omit(x[-1:-4,1:2])141ISOdates = as.Date(x[,1], "%d-%b-%Y") # Get dates142PUT1 = xts(as.numeric(as.vector(x[,2])), order.by=ISOdates)143# Next is current from 2007 on144system("wget https://www.cboe.com/publish/ScheduledTask/MktData/datahouse/PUTDailyPrice.csv")145y=read.csv("PUTDailyPrice.csv")146y=y[-1:-4,]147ISOdates = as.Date(y[,1], "%m/%d/%Y") # Get dates148PUT2 = xts(as.numeric(as.vector(y[,2])), order.by=ISOdates)149# Combine the two series150PUT = rbind(PUT1,PUT2)151colnames(PUT)="Close"152PUT = ROC(Cl(to.monthly(PUT)))153index(PUT) = as.Date(as.yearmon(index(PUT)), frac=1)154# need to drop the last row if inter-month155156157factors=cbind(SP500.R, GS10.R, USDI.R, TERM, CREDIT, DJUBS.R, dVIX, TED, OIL.R, TB3MS/100) # GOLD.R, REALESTATE.R158factors=factors["1997::",]159160## Create a chart of the factor set161asofdate= tail(index(factors),1)162labels=colnames(factors)163pdf(file=paste("Cumulative Factor Returns as of ", asofdate, ".pdf", sep=""), paper="letter", width=7.5, height=10)164op <- par(no.readonly=TRUE)165layout(matrix(c(1:NCOL(factors)), ncol = 1, byrow = TRUE), widths=1)166op <- par(oma = c(5,0,4,0), mar=c(0,4,0,4))167for(i in 1:NCOL(factors)){168xaxis=FALSE169yaxis=TRUE170if(even(i))171yaxis.right=TRUE172else173yaxis.right=FALSE174if(i==NCOL(factors))175xaxis = TRUE176chart.TimeSeries(cbind(factors["1997::",i],SMA(na.locf(factors["1997::",i], n=12))), type="l", colorset=c("blue","lightblue"), ylog=FALSE, xaxis=xaxis, main="", ylab="", yaxis=yaxis, yaxis.right=yaxis.right, lwd=2)177text(.9, .70*(par("usr")[4]), adj=c(0,1), cex = 1.1, labels = labels[i])178}179par(op)180mtext(expression(bold("Monthly Factor Returns")), side=3, outer=TRUE, line=-3, adj=0.1, col="black", cex=1.2)181mtext(paste("As of", asofdate), side=3, outer=TRUE, line=-3, adj=0.9, col="darkgray", cex=0.8)182dev.off()183184