Home > Backtesting, Portfolio Construction, R > Weekend Reading: Market Neutral

I recently came across a very interesting idea at the The Problem with Market Neutral (and an Answer) post by Mebane Faber. Today I want to show how you can test such strategy using the Systematic Investor Toolbox:

```###############################################################################
# Load Systematic Investor Toolbox (SIT)
# https://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
setInternet2(TRUE)
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)

#*****************************************************************
#******************************************************************

data = new.env()

ret = temp[[1]]\$Mkt.RF + temp[[1]]\$RF
price = bt.apply.matrix(ret / 100, function(x) cumprod(1 + x))
data\$SPY = make.stock.xts( price )

ret = temp[[1]]
price = bt.apply.matrix(ret / 100, function(x) cumprod(1 + x))
data\$HI.MO = make.stock.xts( price\$High )
data\$LO.MO = make.stock.xts( price\$Low )

# align dates
bt.prep(data, align='remove.na')

#*****************************************************************
# Code Strategies
#*****************************************************************
models = list()

data\$weight[] = NA
data\$weight\$SPY[] = 1
models\$SPY = bt.run.share(data, clean.signal=T)

data\$weight[] = NA
data\$weight\$HI.MO[] = 1
models\$HI.MO = bt.run.share(data, clean.signal=T)

data\$weight[] = NA
data\$weight\$LO.MO[] = 1
models\$LO.MO = bt.run.share(data, clean.signal=T)

data\$weight[] = NA
data\$weight\$HI.MO[] = 1
data\$weight\$LO.MO[] = -1
models\$MKT.NEUTRAL = bt.run.share(data, clean.signal=F)

#*****************************************************************
# Modified MN
# The modified strategy below starts 100% market neutral, and depending on the drawdown bucket
# will reduce the shorts all the way to zero once the market has declined by 50%
# (in 20% steps for every 10% decline in stocks)
#*****************************************************************
market.drawdown = -100 * compute.drawdown(data\$prices\$SPY)
market.drawdown.10.step = 10 * floor(market.drawdown / 10)
short.allocation = 100 - market.drawdown.10.step * 2
short.allocation[ short.allocation < 0 ] = 0

data\$weight[] = NA
data\$weight\$HI.MO[] = 1
data\$weight\$LO.MO[] = -1 * short.allocation / 100
models\$Modified.MN = bt.run.share(data, clean.signal=F)

#*****************************************************************
# Create Report
#*****************************************************************
strategy.performance.snapshoot(models, T)
```

Mebane thank you very much for sharing this great observation and great strategy that works! I would encourage readers to experiment with idea and share their findings.

If you want to concentrate on the long side, one idea that comes to mind is to start not fully invested say at 90% allocation, and once the market hits say 20% draw-down to invest 100% in expectation of quick recovery.

To view the complete source code for this example, please have a look at the bt.mebanefaber.modified.mn.test() function in bt.test.r at github.

1. November 3, 2013 at 10:13 pm

Thank you very much for sharing!
concerning market neutral strategies I think it would be very interesting to dedicate a post to cointegration trading.
I think that would be very interesting!

2. November 12, 2013 at 2:53 pm

Would it be hard to implement a different kind of stock momentum, based on alpha rather than raw returns? The idea comes from this paper from SocGen: http://gallery.mailchimp.com/6750faf5c6091bc898da154ff/files/117108.pdf . The gist is that taking a long position on stocks with higher alphas (i.e. outperforming the market not simply cause of higher beta/large factor loading on market risk), is much better than doing so looking at raw performance only (plus you don’t expose yourself against the Low Beta Anomaly?).

I tried to backtest the strategy with R (just for the long leg though), but got stuck somewhere. I tried to download single stock time series for the S&P 500 as here: https://systematicinvestor.wordpress.com/2012/06/05/volatility-quantiles/ and then build a “Rolling Alfa” function based on the one built in PerformanceAnalytics (regressing each stock return on S&P’s returns) and select the top “N” stocks, but it’s not working (I think I’m having difficulties in matching dates, so that the SIT backtest function can’t work properly).

Would you be able to conduct this analysis? I think it might be interesting (even just a “Long-Only Top-N Alpha Producers”, rather than the exact strategy shown in SocGen paper).

Cheers!