How to Optimize an MT4 Expert Advisor

MetaTrader 4, abbreviated as MT4, is a trading platform popular with retail traders and Forex brokers. It was released over 20 years ago, and rose to

MetaTrader 4, abbreviated as MT4, is a trading platform popular with retail traders and Forex brokers. It was released over 20 years ago, and rose to prominence through its support for trading robots. 

These robots, formally known as Expert Advisors (EA), can buy and sell instruments traded on the financial markets, based on rules that you define upfront. In this article, we’ll explain how you can optimize an Expert Advisor to generate a 10% return on investment when tested against historical data.

How to customize your MT4 EA

Expert Advisors are computer programmes that run inside the MetaTrader 4 trading platform. In this article, we’ll consider the MT4 Moving Average Expert Advisor that’s built into the platform. This Expert Advisor will buy a security that’s trading over its moving average, and sell it when it falls below its moving average. It’s parametrized to calculate the moving average of prices over the last 12 chart intervals.

You can change any Expert Advisor’s parameters by going into the EA’s source code and re-compiling the programme. This is perhaps best suited for those who are confident with computer code. Alternatively, you can input parameters directly through the EA’s dialog window. In this example, we’ll change the chart interval from 12 to 50 periods, as it’s common practice to calculate moving averages over 50 days.

How to back-test your MT4 EA

Changing your Expert Advisor’s parameters is straight-forward, but how can you be sure you made the right choice? There’s only one way to know, and that’s by backtesting your Expert Advisor against historical data. Backtesting entails running your EA against historical prices and, in some cases, volumes to objectively assess its performance.

If you’ve ever used Excel or Google Sheets to create and test MT4 trading algorithms, you know how tedious this process can be. You’ll first need to gather historical data, and often clean it. Then you’ll need to code your decision rules, drag and drop formulas and repeat this process manually time and time again. This process leaves plenty of room for human error.

MT4 makes it easier than you think to backtest your EAs. Its built-in “Strategy Tester” module allows you to test any EA’s performance against historical data. You can even make your trading environment more realistic by allowing for lags and re-quotes. To get started, right-click your EA and select the “Test” option to open the testing panel in the lower section of the screen. There, you can choose a symbol and timeframe for your charts. We’ve set these to EUR/USD on a daily chart. You can also set an initial deposit and leverage for your imaginary MT4 trading account. We’ve set these to $10,000 and 1:100 respectively.

Then, click the green “Start” button to backtest your EA. By default, MetaTrader will run it against historical data since the start of the calendar year.

backtest your EA

Source: screenshot of the MetaTrader platform

How to measure your return on investment

Once the test is complete, click the “Backtest” tab at the bottom of your screen to access detailed statistics about the test. Our strategy was successful but only generated a $20 net profit, for $482 of gross profits and $462 of net losses over the period. MetaTrader’s Strategy Tester calculates a wealth of other metrics, such as the largest drawdown (in percentage terms), the Sharpe Ratio and Profit Factor. You could use these metrics to objectively compare risk and return across EAs.

Click the “Graph” tab for a visual representation of the changes in your account’s balance and equity over time. The balance shows your account’s value before taking into account profits or losses on open positions, whereas equity includes unrealized profits and losses. As is apparent, the strategy lost money in the first two weeks, before flatlining and staging a recovery from June onwards. The balance converges towards equity at the end of the simulation as positions are automatically wound-down.

 strategy lost money

Source: screenshot of MetaTrader’s Graph panel

As an aside, MetaTrader’s Strategy Tester can access all your MT4 broker’s financial instruments, whether you’re trading Forex or CFDs on stocks, indices or even cryptocurrencies. That’s also why it’s important to open an account with an MT4 broker that accepts all EAs, and supports a wide range of asset classes.

How to optimize your MT4 EA

Admittedly, you may have been disappointed by the strategy’s low 2% return on investment. That’s why it’s important to run your EA through different input parameters as your first attempt will rarely yield the very best results.

This prompted us to test our Moving Average EA against the 20-day, rather than the 50-day moving average. A moving average with a shorter lookback window can lead to better trading decisions, because it’s quicker to reflect changes in current market conditions.

To change your EA’s parameters, click the “Inputs” tab in the lower panel and set the Moving Average Period parameter to 20, instead of 50. Then click the “Start” button again and wait for your results. A mere 10 seconds later,  MetaTrader reveals significantly better results. Our strategy generated a $1,024 net profit off our $10,000 initial investment. Gross profits were $1,370 and net losses just $346. It’s apparent that this Expert Advisor has a much improved risk-reward profile when run against a 20-day moving average.

MetaTrader’s Backtest panel

Source: screenshot of MetaTrader’s Backtest panel

So far, we’ve focussed on adjusting the value of a single input parameter. However, it’s also possible to run combinations of parameters through MetaTrader’s Strategy Builder. These combinations could easily extend into the hundreds or even thousands. You could also look into testing other parameters, such as the chart’s interval, your account’s leverage and the instrument itself, as some trading strategies won’t be suitable for all asset classes.


Trading platforms have come a long way in recent years, and retail traders like you have more ways than ever before to create and test automated strategies that were previously the preserve of Wall Street. However, no matter how encouraging these results are, it’s important to remember that past performance is no guarantee of future results. Markets may change in such a way that past relationships and correlations break down. That’s why it’s important to focus on risk management and capital preservation, above all, no matter how keen you may be to start trading with your chosen MT4 broker.