Parameter optimization in MetaTrader 4 directly impacts EA robustness. Two primary methods exist: brute force (full grid search) and genetic algorithm (GA). Understanding their trade-offs prevents curve-fitting disasters.
1. Brute Force Optimization
Exhaustively tests every combination of parameter steps. For 5 parameters with 30 steps each: 30^5 = 24.3 million passes. Formula for total runs:
```
Runs = ∏( (max_n - min_n) / step_n + 1 )
```
Pros: Global optimum guaranteed. Cons: Time grows exponentially.
2. Genetic Algorithm Approach
MT4's built-in GA uses selection, crossover (uniform rate 0.8), and mutation (rate 0.05). It tests only ~10-20% of brute force runs. The fitness function:
```cpp
// Custom fitness combining profit and drawdown
double Fitness() {
double profitFactor = ShfitProfitFactor();
double maxDD = ShfitMaxDrawdownPercent();
return profitFactor * (1.0 - maxDD/100.0) * 100.0;
}
```
3. Overfitting Detection - Walk Forward Matrix
Split data into IS (In-Sample: 70%) and OOS (Out-of-Sample: 30%). Accept parameters only if OOS performance > 70% of IS performance.
4. MQL4 Parameter Validation Code
```cpp
// Optimization callback for custom validation
double OnTester() {
double profit = TesterStatistics(STAT_PROFIT);
double dd = TesterStatistics(STAT_MAX_DRAWDOWN);
double sharpe = TesterStatistics(STAT_SHARPE_RATIO);
if(dd > 0.3 * profit) return -1.0; // reject high drawdown
return sharpe;
}
```
5. Three-Pass Optimization Strategy
Reference: Kaufman, P. J. (2019). "Trading Systems and Methods." Wiley. Chapter 12 - Optimization Techniques.