Custom Optimization

 

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?

 
Just google for "mt4 optimization article"
 

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...
 
I do have source code as I wrote the EA's in questions.  Thank you for your assistance.  This makes sense now.
 
That worked beautifully!  Thank you!
 

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);
  }  
}
 
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.
 
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.
 
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?
 
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.
 

This is my optimization formula, to obtain. Great benefits, with minimal DrawDown

double OnTester(){
 return (TesterStatistics(STAT_PROFIT)-TesterStatistics(STAT_EQUITY_DD)) * TesterStatistics(STAT_PROFIT_FACTOR);
}

Actually it is very simple:

( STAT_PROFIT - STAT_EQUITY_DD ) * STAT_PROFIT_FACTOR