• Download Gist
system from trend following factors.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
 
###########NOT INVESTMENT ADVICE######################
 
 
#extend the trend following factors into a system for trading S&P 500
 
#Hsieh, David A. and Fung, William,
#The Risk in Hedge Fund Strategies: Theory and Evidence from Trend Followers.
#The Review of Financial Studies, Vol. 14, No. 2, Summer 2001 .
#Available at SSRN: http://ssrn.com/abstract=250542
#http://faculty.fuqua.duke.edu/~dah7/DataLibrary/TF-Fac.xls
 
 
require(gdata)
require(quantmod)
require(PerformanceAnalytics)
require(FactorAnalytics)
 
URL <- "http://faculty.fuqua.duke.edu/~dah7/DataLibrary/TF-Fac.xls"
#get xls sheet TF-Fac starting at the row with yyyymm
hsieh_factor <- read.xls(URL,sheet="TF-Fac",pattern="yyyymm",stringsAsFactors=FALSE)
hsieh_factor.clean <- hsieh_factor
#clean up date to get to yyyy-mm-dd
hsieh_factor.clean[,1] <- as.Date(paste(substr(hsieh_factor[,1],1,4),
substr(hsieh_factor[,1],5,6),
"01",sep="-"))
#remove percent sign and make numeric
hsieh_factor.clean[,2:6] <- apply(
apply(hsieh_factor[,2:6],
MARGIN=2,
FUN=function(x) {gsub("%", "", x)}),
MARGIN=2,
as.numeric)/100
 
#get rid of NAs
hsieh_factor.clean <- hsieh_factor.clean[,1:6]
 
hsieh_factor.xts <- as.xts(hsieh_factor.clean[,2:6],order.by=hsieh_factor.clean[,1])
 
chart.CumReturns(hsieh_factor.xts,
main="Hsieh and Fung Trend Following Factors",
xlab=NA,
legend.loc="topleft")
mtext(text="Source: http://faculty.fuqua.duke.edu/~dah7/DataLibrary/TF-Fac.xls",
side=3,adj=0.10,outer=TRUE, col="purple",cex=0.75,line=-4)
 
chart.Correlation(hsieh_factor.xts,main="Hsieh and Fung Trend Following Factors")
mtext(text="Source: http://faculty.fuqua.duke.edu/~dah7/DataLibrary/TF-Fac.xls",
side=1,adj=0.10,outer=TRUE, col="purple",cex=0.75,line=-1.5)
 
 
#get edhec data for sample factor analysis
data(edhec)
cta <- edhec[,1]
index(cta)=as.Date(format(index(cta),"%Y-%m-01"))
cta.factors <- na.omit(merge(cta,hsieh_factor.xts))
chart.RollingStyle(cta.factors[,1],cta.factors[,2:NCOL(cta.factors)],
width=36,
colorset=c("darkseagreen1","darkseagreen3","darkseagreen4","slateblue1","slateblue3","slateblue4"),
main="Edhec CTA by Trend Following Factors Rolling 36 Months")
mtext(text="Source: http://faculty.fuqua.duke.edu/~dah7/DataLibrary/TF-Fac.xls",
side=1,adj=0.10,outer=TRUE, col="purple",cex=0.75,line=-5)
 
#in one line get SP500 data, convert to monthly, and get 1-month rate of change
GSPC.roc <- ROC(to.monthly(get(getSymbols("^GSPC",from="1900-01-01")))[,4],n=1,type="discrete")
colnames(GSPC.roc) <- "SP500"
#convert date to yyyy-mm-01 so we can merge properly
index(GSPC.roc) <- as.Date(index(GSPC.roc))
#merge factor data with ROC data
roc.factors <- na.omit(merge(GSPC.roc,hsieh_factor.xts))
 
#graph 6 month rolling correlation
chart.RollingCorrelation(roc.factors[,2:NCOL(roc.factors)],roc.factors[,1],n=6,
legend.loc="topleft",main="Correlation (Rolling 6-month)")
chart.RollingCorrelation(roc.factors[,6],roc.factors[,1],n=6,
legend.loc="topleft",main="PTFSSTK (Stock) Correlation (Rolling 6-month)")
abline(h=-0.6,col="red")
abline(h=0.5,col="green")
 
#get rolling 6 month correlation versus all the factors
correl <- as.xts(
apply(roc.factors[,2:NCOL(roc.factors)],MARGIN=2,runCor,y=roc.factors[,1],n=6),
order.by=index(roc.factors))
 
#do simple system where long if correlation with stock trend following factor is low
#as defined by a band of -0.6 and 0.5
system <- lag(ifelse(correl[,5] > -0.6 & correl[,5] < 0.5,1,0)) * GSPC.roc
#see how it works
charts.PerformanceSummary(merge(system,GSPC.roc))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.