Adversarial Regime Optimization Guide
ML market-regime classifier (Random Forest + Triple-Barrier), dual-timeframe trend trading
Exact Start / Step / End values for the MetaTrader 5 optimizer, the order to optimize, and how to avoid curve-fitting.
HOW THE SYSTEM WORKS
- On the higher (Regime) timeframe it trains a random-forest classifier on indicator features (ATR, RSI, BB width, ADX, EMA) with triple-barrier labels to classify the market regime (trend vs range, etc.).
- On the lower (Trading) timeframe it executes trades aligned with the confirmed regime, gated by a confidence threshold.
- Risk is fixed points SL/TP with optional trailing / break-even / partial TP.
CRITICAL: THE ML MODEL MAY BE NON-DETERMINISTIC
This EA trains a random forest at runtime. Random forests use internal randomness; unless explicitly seeded, the same backtest can produce DIFFERENT results run-to-run.
- FIRST do the reproducibility check (below). If two identical runs differ, the model is non-deterministic.
- In that case: optimize the RISK / EXIT / TIME parameters primarily, keep the ML parameters near defaults, use COARSE steps on anything ML-related, and lean HARD on out-of-sample validation. Do not fine-tune ML feature periods to a single backtest - it will not reproduce live.
"POINTS" UNITS
Points / SL / TP are POINTS, not pips. 5-digit FX: 10 points = 1 pip. Indices/metals: scale the point ranges up.
STRATEGY TESTER SETUP
- Real tick data + real spreads; every-tick modelling; 2+ years; hold back ~25-30% (or MT5 Forward = 1/4).
- Criterion: Balance + max Profit Factor (or Recovery Factor).
- REPRODUCIBILITY CHECK: run the same single backtest twice. Note whether results are identical (see ML warning above).
- Bools optimize as Start 0 / Step 1 / End 1.
TIMEFRAMES (set/test, not numeric-optimize):
InpRegimeTF: try H4, H1, D1 (the regime classification TF)InpTradingTF: try M15, M30, H1 (entry TF; keep below the regime TF)- Test a few sensible pairs manually; don't sweep them in a big grid.
THE OPTIMIZATION TABLES (Start / Step / End)
Ranges assume 5-digit FX.
GROUP A: CORE RISK + REGIME GATE (optimize first)
| Input | Start | Step | End | Note |
|---|---|---|---|---|
| InpStopLoss | 200 | 100 | 1000 | points (0 = disabled) |
| InpTakeProfit | 400 | 100 | 2000 | points (0 = disabled) |
| InpMinConfidence | 0.40 | 0.05 | 0.65 | min model confidence to trade |
| InpRegimePersistence | 1 | 1 | 5 | regime-TF bars to confirm regime |
| InpUseRegimeExit | 0 | 1 | 1 | bool: exit when regime flips |
GROUP B: TIME FILTER
| Input | Start | Step | End | Note |
|---|---|---|---|---|
| InpUseTimeFilter | 0 | 1 | 1 | bool |
| InpStartHour | 0 | 2 | 12 | server time |
| InpEndHour | 12 | 2 | 23 | server time; keep > start |
(InpStartMinute / InpEndMinute: leave at defaults 0 / 59.)
GROUP C: EXITS (optional add-ons, off by default)
| Input | Start | Step | End | Note |
|---|---|---|---|---|
| InpUseTrailingStop | 0 | 1 | 1 | bool |
| InpTrailingStop | 100 | 50 | 500 | points (only if on) |
| InpTrailingStep | 25 | 25 | 150 | points (only if on) |
| InpUseBreakEven | 0 | 1 | 1 | bool |
| InpBreakEvenActivation | 100 | 50 | 500 | points (only if on) |
| InpBreakEvenOffset | 0 | 10 | 50 | points (only if on) |
| InpUsePartialTP | 0 | 1 | 1 | bool |
| InpPartialTPLevel | 200 | 50 | 600 | points (only if on) |
| InpPartialVolumePercent | 25 | 25 | 75 | percent to close (only if on) |
GROUP D: ML MODEL (COARSE only; see non-determinism warning)
| Input | Start | Step | End | Note |
|---|---|---|---|---|
| InpTrees | 50 | 50 | 200 | more = steadier, slower |
| InpRetrainBars | 1000 | 500 | 3000 | history kept for training |
| InpLookAheadBars | 5 | 5 | 20 | triple-barrier horizon |
| InpMLBarrierATRMult | 1.5 | 0.5 | 3.5 | higher = more "ranging" labels |
(Optimize this group LAST, coarse, and ONLY validate out of sample.)
GROUP E: FEATURE INDICATOR PERIODS (light; prefer defaults)
| Input | Start | Step | End |
|---|---|---|---|
| InpAtrPeriod | 10 | 4 | 30 |
| InpRsiPeriod | 10 | 4 | 30 |
| InpBbPeriod | 14 | 4 | 34 |
| InpAdxPeriod | 10 | 4 | 30 |
| InpEmaPeriod | 20 | 15 | 80 |
(These feed the model. Tuning them to one backtest is a classic curve-fit, doubly so with a non-deterministic forest. Touch lightly or not at all.)
SET, DON'T OPTIMIZE
InpLotManagement (pick one), InpLotSize / InpRiskPercent (your risk), InpMaxPositions (keep 1), InpMagicNumber.
DO NOT TOUCH
InpShowDashboard, InpDashboardX/Y.
PHASES (order; lock each before the next)
- Pick a timeframe pair (RegimeTF / TradingTF) and keep it fixed.
- Group A (risk + confidence + persistence) - the core.
- Group B (time filter).
- Group C (exits) - one feature at a time, add only if it helps.
- Group D + E (ML model / features) - LAST, coarse, OOS-validated.
READING RESULTS / VALIDATION
- Weigh Profit Factor (>1.2), Recovery Factor, Max DD %, trade count (100+ ideal), win rate WITH avg win/loss. Don't sort by net profit alone.
- CLUSTER TEST: choose settings inside a broad plateau, not a lone spike.
- OUT OF SAMPLE is non-negotiable here because of the ML. Optimize on older data, run the unchanged winner on held-out recent data; keep only if it holds (MT5 Forward = 1/4). Because the forest may be non-deterministic, also re-run the chosen setting a few times and confirm the result is stable enough to trust.
- Repeat per symbol / timeframe pair.
CHECKLIST
- Real tick data; server time; points units checked
- Reproducibility check done (know if the ML is deterministic)
- Timeframe pair chosen and fixed
- Group A -> B -> C optimized and locked from stable clusters
- Group D/E only coarse, OOS-validated, multi-run stable
- Out-of-sample validated; per symbol; demo-confirmed; then live