A profitable backtest can be a poor candidate for automation if its gains depend on a handful of trades or a drawdown the account cannot withstand. MetaTrader 5 allows developers to replace the default optimization ranking with a numerical criterion calculated by their Expert Advisor. The OnTester() event handler is the mechanism for returning that custom score after a test pass.
For mt5 developers, choosing the score is not merely a programming exercise. An optimization engine will favor whatever the formula rewards, including statistical accidents and unintended shortcuts. Five design choices determine whether the resulting ranking measures a usable strategy or an attractive spreadsheet result.
A Single Score Can Conceal Conflicting Objectives
OnTester() returns a double value. When the Strategy Tester is configured to optimize by a custom maximum criterion, higher returned values receive preference. Developers can combine profit, drawdown, trade count and other measures into that single number, often using TesterStatistics() to retrieve built-in test results.
Combining unlike quantities requires deliberate scaling. Adding dollar profit directly to a percentage drawdown gives the dollar figure disproportionate influence. A normalized score or explicit penalty can make the intended trade-off visible, but its weights remain a design judgment rather than a universal measure of quality.
Minimum Trade Counts Need Explicit Enforcement
A strategy with three winning trades may report an exceptional profit factor, yet provide little evidence of repeatable behavior. A custom criterion can reject passes below a specified trade count by assigning a low score instead of allowing a rare lucky outcome to outrank a more active configuration.
The threshold should reflect the strategy’s expected frequency and the length of the test. Imposing 200 trades on a monthly signal would be as arbitrary as accepting three trades from an intraday system. Sample adequacy cannot be reduced to one number without considering the trading horizon.
Penalty Formulas Can Reverse the Apparent Winner
Imagine a hypothetical copper CFD strategy tested across two parameter sets, each with 100 completed trades. Configuration A earns $9,000 but experiences a $6,000 maximum equity drawdown. Configuration B earns $7,200 with a $2,400 maximum equity drawdown. A profit-only optimization selects A.
Now define a custom score as net profit divided by maximum equity drawdown, provided drawdown is positive. A scores 1.5, while B scores 3.0. The ranking reverses because B generates more profit per dollar of observed peak-to-trough equity loss. Neither score predicts future performance, and the ratio needs safeguards for zero or exceptionally small denominators. The example shows why a supposedly objective winner depends on the chosen objective.
Genetic Optimization Can Magnify a Flawed Criterion
During genetic optimization, the tester uses the custom score to guide its search toward promising parameter combinations. It is efficient precisely because it does not exhaustively inspect every possible combination. If the formula rewards tiny samples or ignores transaction costs, the search can concentrate on those weaknesses.
For mt5 testing, a complicated scoring equation is not automatically superior to a simple one. An elaborate formula can conceal the reason a candidate ranks first and make defects harder to diagnose. Reviewing the component statistics alongside the returned score is essential.
Test-Pass Scores Do Not Replace Out-of-Sample Evidence
OnTester() runs after an individual test pass, making it suitable for evaluating that pass rather than guaranteeing the strategy will survive new market conditions. A parameter set chosen from historical results still needs validation on data that did not determine its selection. Forward optimization facilities can help separate selection from subsequent evaluation.
Before opening a CFD position based on an optimized Expert Advisor, inspect the OnTester() formula, confirm its denominator and minimum-sample safeguards, and compare the selected settings against an untouched forward period using the broker’s realistic spread, commission and financing assumptions.
