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.

Clone Algorithm
4
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 575a9b85ab14711213dfa053
There was a runtime error.
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)

Clone Algorithm
5
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 575dd117466b200f8d415fc2
There was a runtime error.

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.

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5760601b05c59811b8e560ca
There was a runtime error.

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