# Custom Optimization

54

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?

7301

Just google for "mt4 optimization article"
21621

Enable "custom" optimized parameter in the tester properties.

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
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...
54

I do have source code as I wrote the EA's in questions.  Thank you for your assistance.  This makes sense now.
54

That worked beautifully!  Thank you!
54

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);
}
}
2911

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.
21621

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.
2911

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?
21621

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.