Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help for a newby (mean reversion)

I very new to this, and I've written this (my first) mean reversion algo which calculates a hedge ratio using linear regression between USO and GLD and then buys/sells depending on the deviation of the portfolio price from the mean. It hasn't turned out very well, unsurprisingly, and the results vary wildly depending on what I set as the lookback period. I was wondering if anyone could point out if I have fundamentally misunderstood how this strategy works. Pointers on my terrible, terrible code would also be appreciated.

7 responses

Some changes I've made, which seem to help reduce the volatility and drawdown:
- order on market open, using yesterday's close data (I guessed more liquidity is better than trying to predict the close)
- compare current day's closing price to moving average of last 10 days including itself (this should improve accuracy)
- grow position size as portfolio grows (instead of fixed position sizing)

Also I was wondering if you actually need two lookback periods. One is for establishing the hedge ratio. If it's relatively stable this could be longer than the current 10 days. The other is for establishing direction of mean reversion. This will be related to the half life of mean reversion, and may vary. I believe it's an output of OLS?

Update: the half life of mean reversion can be worked out from the auto-correlation slope. In other words the beta of the series returns regressed on lagged version of itself. Ernie Chan gives the matlab code in his book.

Found this:

http://epchan.blogspot.co.uk/2011/06/when-cointegration-of-pair-breaks-down.html

Read the comments. Ernie is extremely generous with his thoughts!

I noticed the algo was really unstable with the lookback length. Looking at the chart of the hedge ratio, I can see its very unstable, and yet I would expect the hedge ratio to only change relatively slowly. So, I use an exponential smoothing on the hedge ratio. This seems to help stabilise the performance for a wider range of lookback parameters.

Thats very helpful, thanks- yeah, I thought it was strange how much the hedge ratio was fluctuating, I wonder if it is due to having a short lookback period for the linear regression.

This paper looks good. Updates the hedge ratio using a kalman filter.

http://file.scirp.org/pdf/JMF_2016022913315540.pdf