Summary: Technical comparison of genetic algorithms and grid search for EA parameter optimization. Covers convergence behavior, overfitting risks, walk-forward validation, and includes a modular MQL5 optimizer code snippet.
Parameter optimization separates profitable EAs from curve-fitted disasters. Two dominant methods exist: grid search (exhaustive) and genetic algorithms (GA). Understanding their mathematical behavior prevents overfitting.
1. Grid Search: The Deterministic Baseline
Grid search evaluates every combination within defined bounds. For n parameters each with s steps, complexity is O(sⁿ). With 5 parameters and 20 steps each: 3.2 million runs. Each run uses:
```
Profit_factor = Σ(Gross_Profit_i) / Σ(Gross_Loss_i)
Sharpe_ratio = (R_p - R_f) / σ_p
```
Combined fitness score:
```cpp
double fitness = profitFactor * 0.6 + sharpeRatio * 0.4;
```
2. Genetic Algorithm: Convergence Mathematics
GA maintains a population P of size N. Each generation applies selection (roulette wheel), crossover (single-point, probability 0.7), and mutation (bit-flip, probability 0.01). Convergence rate:
```
E[fitness_best(t+1)] ≥ E[fitness_best(t)] + (μ*σ)
```
where μ is selection pressure, σ is population variance.
3. Overfitting Detection: Walk-Forward
Split data into in-sample (IS) and out-of-sample (OOS). Accept parameters only if:
```
fitness_OOS ≥ fitness_IS * 0.85
```
Never optimize more than 20% of available bars.
4. MQL5 Genetic Optimizer Module
```cpp
struct SParamSet {
double maPeriod[10];
double stopLoss;
double fitness;
};
SParamSet Crossover(SParamSet &a, SParamSet &b) {
SParamSet child;
child.maPeriod[0] = (a.maPeriod[0] + b.maPeriod[0]) / 2.0;
child.stopLoss = (MathRand()%100 < 70) ? a.stopLoss : b.stopLoss;
return child;
}
void Mutate(SParamSet &p, double rate) {
if(MathRand()/32767.0 < rate)
p.maPeriod[0] *= (0.9 + MathRand()/32767.0 * 0.2);
}
```
5. Practical Rule: Parameter Sensitivity Map
For each parameter, calculate:
```
Sensitivity = ΔFitness / ΔParameter
```
If Sensitivity < 0.1, fix the parameter. High sensitivity (>0.8) requires tighter optimization bounds to prevent overfitting.
Reference: Robert Pardo, "The Evaluation and Optimization of Trading Strategies", Wiley Trading, 2008; MQL5 Documentation on Genetic Optimization (mql5.com/docs/tester/genetic_algorithm).