Lesson 7. Optimization Without Overfitting — Safe Ways to Tune Parameters
Learn grid vs genetic search, build a robust objective (PF with drawdown/stability penalties), split IS/OOS, plan walk-forward, and stress-test with costs/volatility. Spot red flags of overfitting before they bite live.
7.1. Parameter search: Grid vs Genetic (when and why)
Two common ways to scan parameter space: brute-force grids and genetic (heuristic) search.
How it works:
Try every combination of parameter values in the defined ranges.
Pros:
- Exhaustive — tests all combinations
- Simple to reason about
- Reproducible results
Cons:
- Explodes with more parameters/steps
- Very slow for large spaces
How it works:
Heuristic evolution toward better regions of parameter space.
Pros:
- Faster on big spaces
- Finds good-enough sets quickly
- Handles many parameters
Cons:
- Non-deterministic (results vary)
- Can miss narrow peaks
- Must control overfitting via validation
7.2. Parameter Space Estimator (how big is your grid?)
Before running optimization, estimate how many combinations you'll test.
Steps: 26
Steps: 17
Steps: 16
Steps: 2
Estimated runtime: 1.2 hours
10k–50k → Consider genetic search
7.3. Objective function: PF with penalties for DD/Stability
Optimize for robustness, not just PF peaks.
PF (Profit Factor)
Gross Profit / Gross Loss
Max DD (relative)
Peak-to-trough drawdown as % of equity
Stability
E.g., percent positive months or equity slope × R² proxy
7.4. IS/OOS and Walk-Forward planning
Split your data into In-Sample (IS) for optimization and Out-of-Sample (OOS) for validation.
Walk-forward: optimize on IS, lock parameters, evaluate on next OOS, repeat.
Schedule: 4 folds: IS 12m → OOS 3m ×4
Total data needed: 60 months
Pass rules: PF ≥ 1.2 on OOS & Max DD ≤ 15% in each fold
7.5. Stress testing: costs and volatility scenarios
A robust parameter set survives realistic cost and volatility changes.
ATR percentile < 30
| Spread × / Slip + | +0 pip | +0.2 pip | +0.5 pip |
|---|---|---|---|
| ×1 | |||
| ×1.2 | |||
| ×1.5 |
Note: Record which scenarios fail and why (slippage/vol spikes).
7.6. Overfitting red flags (quick diagnostic)
PF_OOS / PF_IS = 0.72
Guidance: Cut parameters; use penalties; increase OOS; re-run WF; re-check costs/volatility.
High risk flags:
- PF_OOS / PF_IS < 0.7
- OOS < 3 months
- Parameter count > 7
- Grid > 50k with no walk-forward
- Sensitivity drop > 20%
7.7. Practice: run a small, safe optimization and validate (10–20 min)
Lesson 7 Quiz
Test your understanding with 3 questions. Pass with 2/3 correct.