Optimizing an Expert Advisor means letting the Strategy Tester try many combinations of its inputs on past data and keeping one that holds up. The hard part is not running the optimization, it is avoiding settings that fit the past perfectly and fail on the next month. The method below works in MetaTrader 4 and 5, only some tester settings differ: optimize few inputs, keep part of the history aside, choose from a stable area, then confirm with real costs.
Before you start: what an optimization can and cannot tell you
An optimization answers one question: which input values would have done well on this data, with these costs. It cannot tell you that the same values will do well later. Two habits keep the result useful:
- Decide what you are testing before you look at results. Pick the symbol, timeframe and period first. Changing them until the curve looks good is optimization by hand.
- Keep data aside. Settings are chosen on one part of the history (in-sample) and judged on a later part the optimizer never saw (out-of-sample, or forward period).
Step 1: get data and costs right
- Use the symbol and timeframe the EA was written for, on the broker you will trade with. Symbol specifications differ between brokers.
- Choose a date range long enough to contain enough trades to judge, ideally a few hundred. An EA that trades twice a month cannot be judged on one year.
- In MetaTrader 5, select the tick generation mode that matches how the EA works.
Every tick based on real ticksuses the broker's recorded ticks, and MetaQuotes describes it as giving conditions close to real ones;Open prices onlysuits only EAs that act once per bar, at the open of a new bar. In MetaTrader 4, use theEvery tickmodel for anything that reacts inside the bar. - Make sure the test uses the spread and commission you really pay, and check them in the report. In MetaTrader 4 the tester uses the symbol's current spread at the start of the test unless you type a value in the Spread field, so check that this value is not a flattering one.
- In MetaTrader 5, set an execution delay instead of
No Delay. The tester can emulate the delay between sending an order and its execution; a fixed delay close to what you see on your live account is a fair choice.
Step 2: choose the inputs to optimize
Open the EA inputs in the tester and tick only the inputs you want to vary, giving each a start, a step and a stop value.
| Kind of input | Optimize it | Why |
|---|---|---|
| Core logic (indicator period, entry threshold) | Yes, in wide steps first | This is what the strategy is |
| Exits (stop loss, take profit, trailing distance) | Yes, after the core logic is fixed | Exits interact strongly with entries: test them second |
| Risk and lot size | No | It scales profit and drawdown without making the logic better |
| Trading hours, day filters | With care | Easy to fit to a few lucky days |
| Magic number, slippage, comments | No | They do not change the strategy |
Keep the total number of combinations reasonable. Three inputs with 20 values each are 8,000 passes; six inputs are 64 million, and with that many tries some combination is likely to look excellent by chance.
Step 3: run the optimization with a forward period
MetaTrader 5
- Open the Strategy Tester and select the EA, symbol, timeframe and date range.
- Set Forward to
1/2,1/3or1/4. The tester splits the period and uses the latest part for the forward test. - Set Optimization to
Fast Genetic Algorithmwhen there are many combinations, orSlow Complete Algorithmwhen there are few enough to test them all. - Choose the optimization criterion.
Balance maxrewards raw profit only.Recovery Factor maxrelates profit to drawdown, andComplex Criterion maxcombines the number of deals, drawdown, recovery factor, expected payoff and Sharpe ratio, so a pass with a deep drawdown ranks lower. - Start. MetaTrader 5 runs the passes in parallel on its local testing agents, one per logical CPU core of the machine.
MetaTrader 4
- Open the Strategy Tester, select the EA, symbol, model, period and dates, and tick the Optimization option.
- Click Expert properties. On the Inputs tab, tick the inputs you want to vary and set Start, Step and Stop for each; the genetic algorithm option is on the Testing tab of the same window.
- MetaTrader 4 has no automatic forward period: end the optimization dates early (for example, leave the last 6 to 12 months out) and test the chosen settings on the remaining period with a single normal backtest.
- The MetaTrader 4 tester runs on a single thread, so wide ranges take much longer than in MetaTrader 5.
Step 4: pick from a plateau, not a peak
Sort the results and look at the neighbours of the top passes, not only at the top line.
- Stable area: many nearby values give similar profit and drawdown. Choose a value in the middle of that area.
- Lonely peak: one value is excellent and its neighbours are poor. The EA was fitted to specific past trades. Discard it.
- Forward check: in MetaTrader 5, compare each pass's back-test result with its result on the Forward Results tab. A setting that ranks high in both is a candidate; one that is at the top in the back-test and negative in the forward period is not.
Also check that the result has enough trades to mean something, and that the profit does not come from two or three outsized trades.
Step 5: confirm before real money
- Run one normal backtest with the chosen settings over the whole period and read the report: drawdown, longest losing streak, trades per month.
- Save the inputs as a set-file, so the live EA uses exactly what you tested: in MetaTrader 5 from the context menu of the inputs, in MetaTrader 4 with the Save button on the Inputs tab.
- Run the EA on a demo account, or on a live account at minimum size, for long enough to compare the trade frequency and the average trade with the test. Ideally this run happens on the same kind of machine that will host the EA, 24 hours a day: see how to run an Expert Advisor 24/7.
Where to run optimizations
A large optimization keeps the CPU busy for hours. MetaTrader 5 uses one testing agent per logical core; MetaTrader 4 uses a single thread, but still loads the machine for the whole run.
| Machine | Good for | Not good for |
|---|---|---|
| Your own PC | Occasional optimizations, MetaTrader 5 with a modern multi-core CPU | Leaving the PC unusable for days |
| The VPS that runs your live EAs | Single backtests when markets are closed | Long optimizations: they compete with the terminals that are trading |
| A dedicated server | Frequent or very large optimizations, many terminals | Occasional use: it costs far more than a VPS |
If a VPS feels slow while a test is running, that is expected: see why a trading VPS becomes slow.
When this is not the right approach
- The EA has no testable logic. Some EAs depend on news feeds, external signals or broker-specific execution. The tester cannot reproduce those; rely on a demo run instead.
- The vendor supplies fixed settings. Some commercial EAs are sold with fixed set-files for specific symbols. Optimizing them yourself can move them away from what the author tested.
- You are optimizing to recover a loss. Re-optimizing after every drawdown tends to produce settings that fit the last few weeks and nothing else.
How this works on Dipgate
Trading VPS plans are described by the number of trading platforms they run: up to 3, 6 or 10, depending on the plan. For heavy backtests and optimizations Dipgate offers Trading Servers, whole physical servers: the Trading Server 7700 with a Ryzen 7700 and 64 GB of DDR5 RAM, and the Trading Server 7950X3D with a Ryzen 7950X3D and 128 GB, offered in London or New York. The site describes them as the product for dozens of terminals, heavy backtests or your own infrastructure. For an occasional optimization, your own PC is usually enough: run it there, then copy the set-file to the VPS.
