Download MetaTrader 5

Custom Optimization

To add comments, please log in or register
bplturner
54
bplturner  

Hello,

 

I've Googled for tutorials but have been unable to find one.  I would like to use custom optimization to optimize by EA.  Specifically, I want to maximize profit/drawdown$ ratio to minimize risk.  Does anyone have an example of doing this?

Carl Schreiber
7301
Carl Schreiber  
Just google for "mt4 optimization article"
Stanislav Korotky
21621
Stanislav Korotky  

Enable "custom" optimized parameter in the tester properties.

Add something like the following code into your EA:

double OnTester()
{
  return TesterStatistics(STAT_PROFIT) / TesterStatistics(STAT_EQUITY_DD);
}

Please note that this is just a draft - you should choose/substitute the proper statistics constants according to your needs and handle possible errors (such as division by zero in case drawdown is 0).

If you don't have a source code, I don't think you can apply a custom criterion for optimization.

Properties - Tester - User Interface - MetaTrader 4 Help
Properties - Tester - User Interface - MetaTrader 4 Help
  • www.metatrader4.com
Parameters of the virtual account and the expert advisor under test can be set up in this window. These data will later be used for testing or...
bplturner
54
bplturner  
I do have source code as I wrote the EA's in questions.  Thank you for your assistance.  This makes sense now.
bplturner
54
bplturner  
That worked beautifully!  Thank you!
bplturner
54
bplturner  

Here's a tidbit for the community.  I squared the profit and divided by the drawdown.  This ranks the most profitable optimizations with the most minimal of drawdowns.

 

double OnTester()
{
  if(STAT_EQUITY_DD == 0) {
   return 9999999;
  } else if(TesterStatistics(STAT_PROFIT) < 0) {
   return 0;
  }
  else {
   return pow(TesterStatistics(STAT_PROFIT),2) / TesterStatistics(STAT_EQUITY_DD);
  }  
}
Fernando Carreiro
2911
Fernando Carreiro  
bplturner: Here's a tidbit for the community.  I squared the profit and divided by the drawdown.  This ranks the most profitable optimizations with the most minimal of drawdowns.

Squaring is going to Rank both Winning and Loosing results at similar levels. For example, a Profit +$200 (with $400 drawdown) will be rated the same as a Loss -$200 (with $400 Drawdown).

Instead of inventing a Metric, why not just use a well know public one, that is also used by MT5 and the Signals in the Market:

  • Recovery factor (Calmar ratio): Calculated as net profit divided by the maximum drawdown. Measures the ability of a trading system to recover from losses.
Stanislav Korotky
21621
Stanislav Korotky  
Fernando Carreiro:

Squaring is going to Rank both Winning and Loosing results at similar levels. For example, a Profit +$200 (with $400 drawdown) will be rated the same as a Loss -$200 (with $400 Drawdown).

Just a small note: he takes the square for positive values ONLY. Negatives are processed in other branch of if.
Fernando Carreiro
2911
Fernando Carreiro  
Stanislav Korotky:
Just a small note: he takes the square for positive value ONLY. Negatives are processed in other branch of if.
Did not notice, but then note to OP - Why square the value then?
Stanislav Korotky
21621
Stanislav Korotky  
Fernando Carreiro:
Did not notice, but then note to OP - Why square the value then?
This means that drawdown changes have less influence than profit. Why? I don't know actually.
To add comments, please log in or register