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 is the same in MetaTrader 4 and 5: 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.
- Download enough history for at least a few hundred trades. 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 ticksis the closest to the live feed;Open prices onlyis correct only for EAs that act once per bar. In MetaTrader 4, use the every-tick model for anything that reacts inside the bar. - Set the spread and commission you really pay. In MetaTrader 4 the tester takes the spread from the field in the tester panel, so do not leave it on a flattering value.
- In MetaTrader 5, set an execution delay instead of
No Delay. The tester can emulate the network delay between sending an order and its execution; a fixed delay close to your real ping to the broker 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 something will look brilliant 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;Recovery Factor max,Sharpe Ratio maxorComplex Criterion maxalso punish deep drawdowns and are usually a better first choice. - Start. MetaTrader 5 uses all the CPU cores of the machine through its local testing agents.
MetaTrader 4
- Open the Strategy Tester, select the EA, symbol, model, period and dates, and enable the optimization option.
- In the EA properties of the tester, set start, step and stop for the inputs you want to vary. The genetic algorithm option is in the same properties 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.
- MetaTrader 4 runs optimizations on one CPU core, 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 the back-test and forward columns of each pass. 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 from the inputs context menu, so the live EA uses exactly what you tested.
- 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. This run must happen 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
An optimization is the heaviest job a trading platform does. It will use every core it is given for hours.
| 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 forbids changes. 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 guarantees settings that fit the last few weeks and nothing else.
How this works on Dipgate
Dipgate's VPS products are sized to keep platforms and EAs running: a Trading VPS runs up to 3, 6 or 10 trading platforms depending on the plan. For heavy backtests and optimizations Dipgate offers Trading Servers, whole physical machines with a Ryzen 7700 and 64 GB of RAM or a Ryzen 7950X3D and 128 GB, in London or New York, described on the site 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.
