GuideUpdated 26 November 20266 min read

A step-by-step way to optimize EA settings in the MetaTrader Strategy Tester: which inputs to test, how to use a forward period, and how to spot overfitting.

In short
  • Optimize few inputs at a time, on data the EA has not seen later: keep the last part of the history as a forward period and judge the settings there.
  • In MetaTrader 5 the tester does this for you: set Forward to 1/2, 1/3 or 1/4 and compare the back-test and forward results of each pass.
  • Pick a setting from a wide area of similar results, not the single top pass: a lonely peak is usually curve fitting.
  • Test with the costs you really pay (spread, commission, swap) and with an execution delay; a strategy that only works with zero delay will not survive a live account.
  • Run optimizations on a PC or a dedicated machine, not on the VPS where live EAs are trading: an optimization uses every CPU core it can get.

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.

Workflow: choose inputs, optimize on the first part of the history, check on the forward period, run on demo, then go live
The order that keeps you honest: the forward period and the demo run are checks, not extra optimization rounds.

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

  1. Use the symbol and timeframe the EA was written for, on the broker you will trade with. Symbol specifications differ between brokers.
  2. Download enough history for at least a few hundred trades. An EA that trades twice a month cannot be judged on one year.
  3. In MetaTrader 5, select the tick generation mode that matches how the EA works. Every tick based on real ticks is the closest to the live feed; Open prices only is correct only for EAs that act once per bar. In MetaTrader 4, use the every-tick model for anything that reacts inside the bar.
  4. 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.
  5. 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 inputOptimize itWhy
Core logic (indicator period, entry threshold)Yes, in wide steps firstThis is what the strategy is
Exits (stop loss, take profit, trailing distance)Yes, after the core logic is fixedExits interact strongly with entries: test them second
Risk and lot sizeNoIt scales profit and drawdown without making the logic better
Trading hours, day filtersWith careEasy to fit to a few lucky days
Magic number, slippage, commentsNoThey 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

  1. Open the Strategy Tester and select the EA, symbol, timeframe and date range.
  2. Set Forward to 1/2, 1/3 or 1/4. The tester splits the period and uses the latest part for the forward test.
  3. Set Optimization to Fast Genetic Algorithm when there are many combinations, or Slow Complete Algorithm when there are few enough to test them all.
  4. Choose the optimization criterion. Balance max rewards raw profit; Recovery Factor max, Sharpe Ratio max or Complex Criterion max also punish deep drawdowns and are usually a better first choice.
  5. Start. MetaTrader 5 uses all the CPU cores of the machine through its local testing agents.

MetaTrader 4

  1. Open the Strategy Tester, select the EA, symbol, model, period and dates, and enable the optimization option.
  2. 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.
  3. 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.
  4. 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

  1. Run one normal backtest with the chosen settings over the whole period and read the report: drawdown, longest losing streak, trades per month.
  2. Save the inputs as a set-file from the inputs context menu, so the live EA uses exactly what you tested.
  3. 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.

MachineGood forNot good for
Your own PCOccasional optimizations, MetaTrader 5 with a modern multi-core CPULeaving the PC unusable for days
The VPS that runs your live EAsSingle backtests when markets are closedLong optimizations: they compete with the terminals that are trading
A dedicated serverFrequent or very large optimizations, many terminalsOccasional 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.

FAQ

How many inputs should I optimize at once?

As few as possible, usually two or three that drive the logic (for example a period and a stop distance). Every extra input multiplies the number of combinations and makes it easier to find a setting that fits the past by chance.

How often should I re-optimize an EA?

There is no fixed rule. Re-optimize when the live results leave the range seen in the forward test for a meaningful number of trades, not after every losing week. Changing settings often is another way of fitting noise.

Why are my live results different from the backtest?

Common causes are spread and commission different from the test, slippage and execution delay, a different broker symbol specification, tick data of lower quality than the live feed, and settings chosen from a single top pass.

Can I run the optimization on my trading VPS?

You can, but it competes for CPU with the terminals that are trading. A small VPS is sized to keep platforms running, not to crunch thousands of test passes. Use your PC or a dedicated server for optimizations and copy the set-file to the VPS.

Does the same set-file work on another broker?

Not necessarily. Symbol names, digits, contract size, minimum stop distance, trading hours and costs differ between brokers. Run at least a single test on the new broker's data before using the set there.