# Free Index Mean Reversion System



## sinner (15 August 2012)

Here is an index mean reversion system I developed, posting it on here for free as I don't really have the capital base to give the trades justice. 

Principles/alpha this algo is trying to capture:
* <5 days mean reversion
* With the 10 month trend
* With the intermediate trend
* Do not trade mean reversion if there is a chance of low volatility => high volatility regime shift.
* Time exits to enforce shorter time frame trading tested but not implemented below as deemed unnecessary.

Goal: alpha which is not index correlated, so the system can be added/rebalanced into a larger tactical portfolio.

I have tried to use all default values for the indicators, everything except the volatility threshold which is a little bit curve fit (but this is to illustrate the principle, best to use adaptive algorithm to adjust per market index). There are obviously 'better' values I could have chosen but stuck with these ones in the interest of avoiding curve fit. Again, optimally would be an adaptive algo to adjust parameters per market index or my preference, aggregate "signal score" of a range of values around the optimal values.

Tested on 10+years: NYSE:SPY, NASD:QQQ, ASX:AFI, Euronext: CAC, Euronext: DAX, results best on QQQ. 

Findings: Mean reversion works in both bull and bear regimes, returns are much higher in bear regimes (e.g. 2001-2003, 2007-2009), but returns during bull regimes are extremely low volatility, perhaps a good place to add leverage or extra longs to a momentum strategy or conversely hedge on exit signals. Target volatility, momentum and trend following remain optimal alpha tools for bull regimes.

Addendum: the code below enters on market, significant extra alpha can be captured by ingenious use of limit orders and 24H futures markets....


```
fast=Average[5](Close)
slow=Average[200](close)
mac=MACDline[12,26,1](Close)
stddev=STD[21](Close)
lowvol=Lowest[120](stddev)
X=100

IF NOT SHORTONMARKET AND Close > slow AND mac > 0 AND Close < fast AND stddev > 1.05*lowvol THEN
    BUY X %CAPITAL AT MARKET ThisBarOnClose
ENDIF

IF LONGONMARKET THEN
    IF Close < slow OR mac < 0 OR Close > fast THEN
        SELL AT MARKET ThisBarOnClose
    ENDIF
ENDIF

IF NOT LONGONMARKET AND Close < slow AND mac < 0 AND Close > fast AND stddev > 1.3*lowvol THEN
    SELLSHORT X %CAPITAL AT MARKET ThisBarOnClose
ENDIF

IF SHORTONMARKET THEN
    IF Close > slow OR mac > 0 OR Close < fast THEN
        EXITSHORT AT MARKET ThisBarOnClose
    ENDIF
ENDIF
```


----------



## Punta (16 August 2012)

That's cool sinner.  I don't have decent EOD index data going back far enough to test it, but I like the idea ..


----------



## sinner (16 August 2012)

Punta said:


> That's cool sinner.  I don't have decent EOD index data going back far enough to test it, but I like the idea ..




Glad you like it Punta, not much to it as you can see but the results speak for themselves in terms of index correlation (which makes a portfolio running this system a good natural index hedge), which is why I developed it and also why I'm not using it (don't have the account size to run this on only a portion of my capital and still beat transaction costs by a significant margin).

I did test it on some stocks as well, seems to do quite well on some (mega-caps?) and not so well on others (<med).

Another addendum would be that similar returns could have been achieved by a long-term trend following trader, but that the equity curve of the mean reversion system is much less lumpy.

I thought tech might like this one as a complement to techtrader in the sense that he often says trading down-markets is different to up-markets and he had difficulty finding a system for down markets. The above system can be adapted to 'short trends' by simply removing the long component of the system and adjusting the cover component such that it only covers on a MACD cross (i.e. mean reversion entry, TF exit):


```
IF SHORTONMARKET THEN
    IF Close > slow OR mac > 0 THEN
        EXITSHORT AT MARKET ThisBarOnClose
    ENDIF
ENDIF
```


----------



## kid hustlr (16 August 2012)

will put this one in the bank, thanks sinner

out of interest,

for the mac d line (12,26,1) 

I assume 12 and 26 refer to 12 and 26 day period average. What does the '1' refer to?


----------



## Lone Wolf (16 August 2012)

kid hustlr said:


> out of interest,
> 
> for the mac d line (12,26,1)
> 
> I assume 12 and 26 refer to 12 and 26 day period average. What does the '1' refer to?




I'm not a big fan of people saying "google is your friend" as discussion boards are the places I usually find answers when I google. However, it'll be easier for you to understand if you google for one of the many pretty pictures out there explaining the indicator. If ever you want to use an indicator, make sure you understand exactly what it does first.

But in short - The MACD indicator is made up of the MACD line, the signal line and the histogram. The MACD line is the difference between the fast EMA and the slow EMA. The signal line is an EMA of the MACD line. The histogram is the difference between the MACD line and the signal line.

So in the case above the MACD line is the difference between the 12 EMA and the 26 EMA. The signal line is a 1 EMA of the MACD line. The histogram won't exist since there is no difference between the MACD line and the signal line.


----------



## sinner (16 August 2012)

kid hustlr said:


> will put this one in the bank, thanks sinner
> 
> out of interest,
> 
> ...




Exactly what Lone Wolf said. The signal line is not used in this system so it's not calculated, to keep the code explicit.


----------



## kid hustlr (21 August 2012)

Lone Wolf said:


> So in the case above the MACD line is the difference between the 12 EMA and the 26 EMA. The signal line is a 1 EMA of the MACD line. The histogram won't exist since there is no difference between the MACD line and the signal line.




Ta guys.


----------

