GuideUpdated 27 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, in MetaTrader 5, with an execution delay; a strategy that only works with zero delay is unlikely to hold up on a live account.
  • Run long optimizations on a PC or a separate machine, not on the VPS where live EAs are trading: MetaTrader 5 starts one testing agent per logical CPU core.

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.

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. 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.
  3. In MetaTrader 5, select the tick generation mode that matches how the EA works. Every tick based on real ticks uses the broker's recorded ticks, and MetaQuotes describes it as giving conditions close to real ones; Open prices only suits only EAs that act once per bar, at the open of a new bar. In MetaTrader 4, use the Every tick model for anything that reacts inside the bar.
  4. 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.
  5. 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 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 some combination is likely to look excellent 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 only. Recovery Factor max relates profit to drawdown, and Complex Criterion max combines the number of deals, drawdown, recovery factor, expected payoff and Sharpe ratio, so a pass with a deep drawdown ranks lower.
  5. Start. MetaTrader 5 runs the passes in parallel on its local testing agents, one per logical CPU core of the machine.

MetaTrader 4

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

  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, 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.
  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. 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.

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 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.

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 VPS plan is usually chosen for the number of platforms it keeps running, not for thousands of test passes. Use your PC or a separate server for long 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.