One of the most critical yet misunderstood aspects of Expert Advisor development is parameter optimization. Both grid search and genetic algorithms (GA) have their place, but choosing the wrong method or misinterpreting results leads to curve-fitting and poor forward performance.
Grid Search exhaustively tests all combinations of user-defined parameter ranges. It is deterministic and guarantees finding the global optimum within the specified step resolution. However, the computational cost grows exponentially. For 5 parameters with 20 steps each, you face 3.2 million passes.
Genetic Algorithms use selection, crossover, and mutation to converge toward optimal zones. MQL4's built-in optimizer uses GA by default, but many developers misuse it by running too few generations or ignoring noise.
Mathematical Framework for Robustness
Let performance metric \( P(\theta) \) be a function of parameter vector \( \theta \). The optimization objective is:
\[
\theta^* = \arg\max_{\theta \in \Theta} P_{\text{OOS}}(\theta)
\]
where \( P_{\text{OOS}} \) is out-of-sample performance. Overfitting occurs when \( P_{\text{IS}}(\theta) \) is high but \( P_{\text{OOS}}(\theta) \) is low.
Code: Custom Walk-Forward Validation
```cpp
// MQL4 snippet - Walk-Forward Optimization Core
input double LotsFixed = 0.1;
input int MAPeriod = 14;
input int ATRPeriod = 14;
double InSampleProfit, OutSampleProfit;
int InSampleBars = 5000;
int OutSampleBars = 1000;
void WalkForwardOptimize() {
int totalBars = Bars;
int inSampleEnd = totalBars - OutSampleBars;
// Optimize on in-sample data
InSampleProfit = RunOptimization(0, inSampleEnd);
// Apply best parameters on out-of-sample
OutSampleProfit = RunForwardTest(inSampleEnd, totalBars);
double RobustnessRatio = OutSampleProfit / InSampleProfit;
if(RobustnessRatio < 0.7) {
Print("Overfitting detected - ratio: ", RobustnessRatio);
}
}
```
Key Metrics for GA Configuration
Avoiding Future Function Leakage
The most common MT4 backtest error is using `Close[]` or `High[]` with index `0` inside indicator calculations before the bar completes. Always shift by 1: `Close[1]` for entry decisions based on closed bars.
Robustness Validation Formula
Calculate the Sharpe ratio on rolling windows:
\[
S_{\text{rolling}} = \frac{\mathbb{E}[R_{\text{window}}] - R_f}{\sigma_{\text{window}}}
\]
If standard deviation across rolling windows exceeds 0.5, the parameter set is unstable.
Reference: MQL4 Documentation - Optimization (https://docs.mql4.com/optimization), "The Evaluation and Optimization of Trading Strategies" by Robert Pardo, 2008.