# Improving mean reversion



## sinner (6 February 2013)

hi guys,

Here is the equity curve and report for a system trading EURUSD since its inception in 1999. It doesn't have any position sizing or stops or take profit. It trades at most once per day, on the NY close. The starting balance is 10,000USD and no leverage is applied.




I then modified the system (which I am sure, most MR traders will recognise the equity curve of this system if they trade it on EUR) to size position based on deciles of the 120 day (~6 months) Williams % Range. 

i.e. between -100 and -90, size 5 contracts, between -90 and -80 size 4, and same on the other side, between 0 and -10 size 5 contracts, between -10 and -20 size 4...such that position sizes are largest when on either side of the 120 day range and smallest when in the middle (between -60 and -40). Obviously in this case, max leverage can go as high as 5:1 with a 10,000USD starting balance.




I uploaded the recorded individual trades since 1999 for both systems here http://filebin.ca/W439i8NViJe/mr_mod.zip (since you can't upload zipfiles to ASF). No transaction costs are included. I can't be 100% confident of trading at the closing price each day but the EURUSD is an extremely liquid market so I have some confidence in the results. This is an EOD system, so I have not populated the backtest with tick data.

Thx.


----------



## sinner (6 February 2013)

This is a EURUSD system but for posterity I include tests on USDCHF, USDJPY and GBPUSD. You can see that during the GFC, something happened to the GBPUSD market such that it switched (potentially forever?) from a mean reverting market to a momentum market (this is where the 120day ROC of your equity curve is important for systematic traders, IMHO).


----------



## sinner (6 February 2013)

I've tried a few more "things" today to improve mean reversion.

As an example, one of the things I tried was only betting on short term mean reversion if the range above and below the "mean" was at least a certain % of recent volatility. My testing quickly showed this was a inconsequential factor, sometimes even filtering out good trades.

The one thing I tried and which was quite successful against a large range of parameters (indicating a strong statistical edge) was only betting on short term mean reversion if the short term average of volatility was higher than a longer term average of volatility.

It was a pleasant surprise for me to see how effective this simple filter was across all pairs, returns took a hit in EURUSD but in return for a significant smoothout of the equity curve.

Take a look for yourself:



EURUSD



GBPUSD



USDJPY



USDCHF

Results here: http://filebin.ca/W5fU1yt3jm6


----------



## waza1960 (7 February 2013)

Appreciating the thread Sinner as I love mean reversion systems.......



> The one thing I tried and which was quite successful against a large range of parameters (indicating a strong statistical edge) was only betting on short term mean reversion if the short term average of volatility was higher than a longer term average of volatility




 I also have a similar filter and also amazed that it works so well for something so simple

  For some real fun look at increasing Multiple entries away from the mean and increased position size as well


----------



## sinner (7 February 2013)

waza1960 said:


> Appreciating the thread Sinner as I love mean reversion systems.......
> 
> 
> 
> ...




Thanks for the input waza. Yeah I am doing a multiple entry thing if you check the first post.

What surprised me about the vol filter was that I had to design a much more complicated rule for my equity mean reversion system. Last night I went back and double checked by trying to substitute in the simple one and it was nowhere near as good. Very interesting.


----------



## Trembling Hand (7 February 2013)

waza1960 said:


> I also have a similar filter and also amazed that it works so well for something so simple
> 
> For some real fun look at increasing Multiple entries away from the mean and increased position size as well




Interesting. 80% of my trading is MR or good old fading. Recently my programming skills have exploded beyond what I through my little punch drunk brain would be able to do (something about practise I think). Anyway its enabled me to represent in a programmed and reasonably clear way my discretionary trading, which is a long way away from,

if X Y & Z happens do,

ABC.

To my great surprise the back testing without any optimisation was quite profitable. 

In spite of its 'stupid' nature, that is taking trades when I simply wouldn't, it still fairs pretty well. The only thing that I have yet to be able to program and would worry me from a real world application is when the volatility greatly increases my string of consecutive losers grows to an uncomfortable level. Especially with multiple entries and multiple instruments (very correlated on an intraday basis).


----------



## tech/a (7 February 2013)

Trembling Hand said:


> Interesting. 80% of my trading is MR or good old fading. Recently my programming skills have exploded beyond what I through my little punch drunk brain would be able to do (something about practise I think). Anyway its enabled me to represent in a programmed and reasonably clear way my discretionary trading, which is a long way away from,
> 
> if X Y & Z happens do,
> 
> ...




Would you not suspend trading the method in times of high volatility?
That would be the filter.
Or would you not then have a trading method which works best in times of high Volatility?


----------



## CanOz (7 February 2013)

Trembling Hand said:


> In spite of its 'stupid' nature, *that is taking trades when I simply wouldn't*, it still fairs pretty well.




This is the thing that really irritated me about my intra-day systems...for some reason EOD doesn't bother me as much.

CanOz


----------



## Gringotts Bank (7 February 2013)

Trembling Hand said:


> To my great surprise the back testing without any optimisation was quite profitable.




Can I see your Equity curve?


----------



## Trembling Hand (7 February 2013)

CanOz said:


> This is the thing that really irritated me about my intra-day systems...for some reason EOD doesn't bother me as much.
> 
> CanOz




Yeah intraday shifts in volatility!! Rough for system traders as well as disc. punters! Not uncommon to go from 10 point 1 min ATR to 50 - 60 then back to 10 -20 all within 30 minutes!


----------



## Trembling Hand (7 February 2013)

Gringotts Bank said:


> Can I see your Equity curve?




Nope


----------



## white_goodman (7 February 2013)

modelling quality is a problem is strategy tester

still like the idea of fading short term spikes/volatility.. id suspect you may get better results on similar cross pairs AUDCAD etc


----------



## Gringotts Bank (7 February 2013)

Trembling Hand said:


> Nope




Your dog ate it?

What about some stats?


----------



## Trembling Hand (7 February 2013)

Gringotts Bank said:


> Your dog ate it?
> 
> What about some stats?




Nah mate some of us actually have competitors who would be interested.


----------



## sinner (7 February 2013)

> In spite of its 'stupid' nature, that is taking trades when I simply wouldn't, it still fairs pretty well. The only thing that I have yet to be able to program and would worry me from a real world application is when the volatility greatly increases my string of consecutive losers grows to an uncomfortable level. Especially with multiple entries and multiple instruments (very correlated on an intraday basis).




What you are talking about here is the regime shift, right? From low volatility to high volatility? I've not yet seen a systematic MR system which can handle those regime shifts without some DD. 

Same applies in options land, right? There is a simple payoff diagram:

Long Straddle
Low Volatility => Low Volatility: OK
Low Volatility => High Volatility: GOOD
High Volatility => Low Volatility: DANGER
High Volatility => High Volatility: OK

Short Straddle
Low Volatility => Low Volatility: OK
Low Volatility => High Volatility: DANGER
High Volatility => Low Volatility: GOOD
High Volatility => High Volatility: OK

Can't hurt to try a regime filter to at least see if it helps.


----------



## sinner (7 February 2013)

white_goodman said:


> modelling quality is a problem is strategy tester
> 
> still like the idea of fading short term spikes/volatility.. id suspect you may get better results on similar cross pairs AUDCAD etc




Can you clarify? I'm using EOD prices and entering at market with no stops, I don't think the (well known) issues around MT4 backtesting apply to this test (else I wouldn't have used it).

It seems sort of logical that the crosses would provide better results and I do plan to test them but I did write this system specifically for EURUSD trading, especially for long vol systems I think it's not as important to diversify too much (correlations are correlated to vols).


----------



## Trembling Hand (7 February 2013)

sinner said:


> Can't hurt to try a regime filter to at least see if it helps.




To be clear the "dumb" system is of my making. I understand that. I deal with it in a discretionary approach with ease. Its the ability to program something that is the hard bit. (for a dumb programmer )

Something to do down the track. I was never really going to run it. Was just for conformation of my ideas.


----------



## sinner (7 February 2013)

Guys, I have decided that like my previous mean reversion experiments I will happily open source this one onto ASF. Please note the parameters, n=2 and s=120. These are not curve fit or optimised numbers, I simply picked the lowest possible MR frequency and 6 months as a generalised long term measure. You can try other parameters to see where the boundaries of the edge lie.


```
//Params
int n=2;
int s=120;

//State
double cmd;

bool newBar()
{
   static datetime lastbar;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return (true);
   }
   else
   {
      return(false);
   }
}

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  
//----
   return(0);
  }
 
double calculatePosSize()
{
   double x = iWPR(NULL,0,s,1);
   double y = 0.1;
  
   if(x >= -100 && x < -90) {
      y = 0.5;
   }
   if(x >= -90 && x < -80) {
      y = 0.4;
   }
   if(x >= -80 && x < -70) {
      y = 0.3;
   }
   if(x >= -70 && x < -60) {
      y = 0.2;
   }     
   if(x >= -60 && x < -50) {
      y = 0.1;
   }
   if(x >= -50 && x < -40) {
      y = 0.1;
   }  
   if(x >= -40 && x < -30) {
      y = 0.2;
   }
   if(x >= -30 && x < -20) {
      y = 0.3;
   }  
   if(x >= -20 && x < -10) {
      y = 0.4;
   }
   if(x >= -10 && x < 0) {
      y = 0.5;
   }  
   //return(0.1);        
   return(y);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{ 
//----
   if(!newBar()) return(0);
  
   //ORDER MAINTENANCE
   for(int cnt=OrdersTotal();cnt>=0;cnt--) {
  
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
  
      if( OrderSymbol()==Symbol() ) {
         cmd=OrderType();
        
         //EXIT LONGS
         if(cmd==OP_BUY && OrderMagicNumber() == 11){
            if(Close[1] > Close[n] || Close[1] < Close[s]) {
               OrderClose(OrderTicket(),OrderLots(),Bid,0,Blue);
            } 
         }
        
         //EXIT SHORTS        
         if(cmd==OP_SELL && OrderMagicNumber() == 21) {
            if(Close[1] < Close[n] || Close[1] > Close[s]) {
               OrderClose(OrderTicket(),OrderLots(),Ask,0,Red);
            }
         }  
      }  
   }  
 
   double atr=iATR(NULL,0,10,1);
   double atr2=iATR(NULL,0,s,1);
   //ORDER ENTRY
   if(OrdersTotal() < 1 && atr > atr2){
      //BUY
      if(Close[1] > Close[s] && Close[1] < Close[n]) {
         OrderSend(Symbol(),OP_BUY,calculatePosSize(),Ask,0.3,0,0,NULL,11,0,Blue);
      }
      //SELL
      if(Close[1] < Close[s] && Close[1] > Close[n]) {
         OrderSend(Symbol(),OP_SELL,calculatePosSize(),Bid,0.3,0,0,NULL,21,0,Red);
      }
   }
//----
   return(0);
}
 
 

//+------------------------------------------------------------------+
```

Enjoy.


----------



## howardbandy (8 February 2013)

tech/a said:


> Would you not suspend trading the method in times of high volatility?
> That would be the filter.
> Or would you not then have a trading method which works best in times of high Volatility?




Greetings --

In general, mean reversion systems work better during periods of high volatility.  But test on your specific system.

Regards,
Howard


----------



## howardbandy (8 February 2013)

Greetings --

The code posted for this system includes:

double calculatePosSize()
{
   double x = iWPR(NULL,0,s,1);
   double y = 0.1;

   if(x >= -100 && x < -90) {
      y = 0.5;

-------------------

In my opinion ----

1.  Position size is determined by risk, and risk is determined by the set of trades that are used as the best estimate of future performance.  

Position size is dynamic and based on performance.  I recommend that it not be included in the system code that generates the buy and sell signals.  Rather, analyze the "best estimate" set of trades, estimate risk of drawdown, compare the risk of drawdown with the trader's personal risk tolerance, then set position size.  If the system performance deteriorates, this method automatically reduces position size.  

2.  The code snippet goes on to give position size based on the 120 day Williams Percent Rank -- essentially the raw position-in-range for the previous 120 days, normalized into a range of 0 to -100.  Even if my first suggestion is ignored, it is beneficial to sort trade performance according to the value of the indicator at the time of the signal and see whether there is such a well defined relationship between indicator and profit.

Best regards,
Howard


----------



## ThingyMajiggy (8 February 2013)

Alright a few dumb questions so I can catch up:

1. What's "MR"?
2. What's DD? Drawdown? 
3. What language is that sinner? 
4. What program do you system guys write this in? MT4? Or something else?


----------



## kid hustlr (8 February 2013)

ThingyMajiggy said:


> Alright a few dumb questions so I can catch up:
> 
> 1. What's "MR"?
> 2. What's DD? Drawdown?
> ...




1. mean reversion
2 drawdown
3. im not sure
4. theres a few around,  many use amibroker, this i believe is metatrader (i think)


----------



## sinner (8 February 2013)

howardbandy said:


> Greetings --
> 
> The code posted for this system includes:
> 
> ...




Hey Howard,

Thanks for your input. Please understand, this is a bootstrap test, to get discussions around MR going and so people can see what's what. I am not providing this system as anything but an example, your "#1" suggestion is definitely the optimal method for implementing this live, and I do something along those lines.

2. Please don't assume I only tried one set of parameters before posting here. What I've posted are actual perceived improvements, but I did test many parameters and position sizing concepts. In this case I have clarified exactly where the "well defined relationship between indicator and profit" lies.

For ThingyMajiggy:

kid hustlr got it all right, the language is MT4, I generally bootstrap my tests (for FX anyway) in MT4 and then rewrite it to JForex or in python/R and upload it into my virtualised environment so it's up 24/7.


----------



## ThingyMajiggy (8 February 2013)

sinner said:


> kid hustlr got it all right, the language is MT4, I generally bootstrap my tests (for FX anyway) in MT4 and then rewrite it to JForex or in python/R and upload it into my virtualised environment so it's up 24/7.




I see, how would I go about it if I wanted to create something with futures? Platforms/languages etc.? 

What's your virtualised environment? Like a virtual machine?


----------



## sinner (11 February 2013)

ThingyMajiggy said:


> I see, how would I go about it if I wanted to create something with futures? Platforms/languages etc.?
> 
> What's your virtualised environment? Like a virtual machine?




My suggestion for futs trading is without a doubt NinjaScript, or plug into the API directly.

The virtualised environment is a small cluster of geographically distributed virtual machines.


----------



## waza1960 (11 February 2013)

> My suggestion for futs trading is without a doubt NinjaScript, or plug into the API directly.




      Ninja Script being a net environment is good as far as coding strategies are concerned but Ninja Trader
        is hopeless for Auto Trading due to the API/Platform issues.

      I'm moving to Multicharts NET this year much better platform IMO.


----------



## ThingyMajiggy (12 February 2013)

waza1960 said:


> Ninja Script being a net environment is good as far as coding strategies are concerned but Ninja Trader
> is hopeless for Auto Trading due to the API/Platform issues.
> 
> I'm moving to Multicharts NET this year much better platform IMO.




But NT is free and MC is $1500?


----------



## sinner (12 February 2013)

waza1960 said:


> Ninja Script being a net environment is good as far as coding strategies are concerned but Ninja Trader
> is hopeless for Auto Trading due to the API/Platform issues.
> 
> I'm moving to Multicharts NET this year much better platform IMO.




What issues have you specifically had with ninjascript?

My personal preference is always to plug directly into the brokers API endpoint if possible but recognise not everyone is a programmer.


----------



## waza1960 (13 February 2013)

> But NT is free and MC is $1500?




    Not if you want to trade live ( I have a paid version of NT)
    I don't want to get into a discussion of what platform is better .....all platforms have their strengths and weaknesses
     I use and like Ninja Trader for discretionary trading



> What issues have you specifically had with ninjascript?




   No issues with NinjaScript in fact their solution for the coding of Time conditions is quite elegant compared to other platforms,but that is the frustration good for coding but hopeless with trade management and execution IMO.
  Another big issue for me was that backtesting and Simulation in NT is always calculated on Bar close despite
   what you have coded which makes these results next to useless IMO.

   When I ran live strategies with NT I had numerous error messages,platform freezing,order management becoming
   corrupted i.e. trades becoming disconnected from the strategies...
   Am running live on MT4 now for nearly 12 months no error messages/no trade execution problems at all despite many hundreds of trades.
   Please note MT4 has its own drawbacks...............


----------

