System from Trend Following Factors
[This article was first published on Timely Portfolio, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
As I thought more about Trend Following Factors from Hsieh and Fung, I thought that the trend following factors might indicate a state/regime for the equity markets that could potentially offer momentum-style timing signals for a system on the S&P 500. Now, THIS ABSOLUTELY SHOULD NOT BE CONSIDERED INVESTMENT ADVICE, especially since the factor data is very lagged and the testing is nowhere near comprehensive enough. I will however try to replicate the factor methodology to get a more real-time indicator extended to any index in another post. What is most interesting to me is that this is ex-ante intuitive and the signal is just basic statistics.
![]() |
From TimelyPortfolio |
![]() |
From TimelyPortfolio |
![]() |
From TimelyPortfolio |
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
###########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)) |
To leave a comment for the author, please follow the link and comment on their blog: Timely Portfolio.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.