EA parameter optimization prevents curve-fitting when done correctly. Two dominant methods: exhaustive grid search and genetic algorithms (GA). GA reduces runtime by 80-90% on high-dimensional spaces while avoiding local minima.
1. Grid Search Mathematical Foundation
For parameters {p1, p2, ..., pn} with step sizes s_i, total combinations = ∏(range_i / s_i). Example: 3 parameters with 20 steps each = 8,000 runs.
Fitness function F = SharpeRatio * sqrt(252) - 0.5 * DrawdownPercent. Weighted score allows multi-objective ranking.
2. Genetic Algorithm Core Logic
GA uses selection, crossover, mutation. Selection pressure formula: Probability(individual i) = fitness_i^α / ∑ fitness_j^α, where α≥1 amplifies elite bias.
3. MQL4 GA Implementation Snippet
```cpp
// Simplified GA for MT4 optimization
struct Chromosome {
double takeProfit, stopLoss, maPeriod;
double fitness;
};
void Crossover(Chromosome &a, Chromosome &b, Chromosome &child) {
child.takeProfit = (a.takeProfit + b.takeProfit) / 2.0;
child.stopLoss = a.stopLoss > b.stopLoss ? a.stopLoss : b.stopLoss;
child.maPeriod = (rand()%2) ? a.maPeriod : b.maPeriod;
}
void Mutate(Chromosome &c, double rate) {
if((rand()/32767.0) < rate)
c.maPeriod *= (0.9 + (rand()/32767.0)*0.2);
}
```
4. Overfitting Prevention Metrics
Use Walk-Forward Matrix: In-sample (80% data), out-of-sample (20%). Acceptable decay threshold: OOS performance ≥ 70% of IS performance.
Penalty term for complex models: AdjustedFitness = RawFitness * (1 - λ * (params_count / total_data_points)), λ=0.01.
5. Validation Protocol
Reference: Kaufman, P. J. (2019). "Trading Systems and Methods", 6th Ed., Chapter 12, Wiley. MQL5 Documentation: "Strategy Tester Optimization Algorithms".