Home » Forex School » Metatrader 4 Strategy Tester – How to backtest and optimize your Expert Advisors
Metatrader 4 Strategy Tester – How to backtest and optimize your Expert Advisors
What is the MT4 Strategy Tester?
The MT4 Strategy Tester is a useful tools directly included in Metatrader 4 to test your Expert Advisor with historical data. It helps you to optimize your EA’s performance to the maximum. Learn how to optimize your Expert Advisor with more than 10’000 different settings in a single run to squeeze out the hidden force of your EA.
Before you can backtest you EA, you have to download the latest history dates from your Broker. Go to Tools > Options and enter in the „Max bars in history“ and „Max bars in chart“ the number 9999999999999. Restart your terminal and press F2 on your Keyboard. Go to the currency Pair you want to download data from. Make a double click on all timeframes until they are all colored like on the picture below. Then click on „Download“ > OK. This will take a while.
Go to View > Strategy Tester to open it. Fill in all of the necessary information.
Select your EA you want to backtest (for this article I choose the free “Bollinger Bands EA Basic” (available here).
Choose one of the Symbol you downloaded before.
Choose the timeframe you want to test your Expert Advisor on.
Choose the model you want to test your Expert Advisor on.
Every Tick (the most precise method based on all available least timeframes to generate each tick)
Control points (a very crude method based on the nearest less timeframe, the results must not be considered)
Open prices only (fastest method to analyse the bar just completed, only for EA that explicitly control bar opening)
Choose a Virtual Spread for you backtesting. A reasonable value is 2 pips. (set 20 for a 5 digits broker and 2 for a 4 digits broker)
Fill in from when to when you want to make the backtest.
If you choose this option, the chart will pop-up and you can easily and visually see how you robot is trading. With the little bar you can adjust the speed of the calculation.
Here you can change the properties of your robot. In the “Testing” tab you can choose how much Initial deposit and Currency you want to test as well as if you want to trade Long & Short positions or only one of them.
Go to the Inputs tab to change the Value of your robots Variables.
Shows you some useful information about the current Symbol
If your backtest is finished you can take a look at all trades on the chart.
If you have the source code of your Expert Advisor you can change it by clicking here.
If you have set all parameter listed above, you can start your backtest by clicking on the “Start” button. If the backtest is finished you find below the Result tab and 3 new tabs. The Graph, Report and Journal tabs. Read the next chapter “Analysing” to learn more about these tabs.
After backtesting your Expert Advisor it’s important to analyse the test results
In the result tab you find the whole trades your EA has made during the test. The Order Type (buy, sell, buy stop, sell stop, buy limit, sell limit), if the Order was deleted, mortified, closed by the EA, or reached the Take Profit (T/P) or the Stop Loss (S/L). You can see the Order number, his open price, Stop Loss and Take Profit (if available), the trades profit and the current account balance.
In the graph tab is not much to say. You can see the graph of the EA’s trades. On the right is the account balance and below the number of trades.
This is an important tab. All the detailed information are here. I only explain those who are not self-explanatory and important for me.
Total profit, the sum of all profitable (positive) trades.
Total loss, the sum of all negative trades.
Profit factor is equal to the gross profit divided by the gross loss. Higher is better, anything above 1.5 is good.
Expected pay off:
Is the total net profit divided by the number of trades.
The absolute drawdown shows the difference between the initial deposit and the smallest value of the equity.
The difference between one of the local maximums and the subsequent minimum of the equity. The largest value is taken.
Equity drawdown in monetary terms that was recorded at the moment of the maximum equity drawdown in percent.
The journal tab of the MT4 Strategy Tester is not important to most of you. It is only interesting for Expert Advisor developer to find some errors in the code.
The possibility to optimize your Expert Advisor in a single run is the most impressive about Metatrader Strategy Tester. If you use this possibility correctly it will help you a lot to find the perfect settings for your EA.
To use this option you have to check the “Optimization” box in the Settings tab of the MetaTrader 4 strategy tester and then go to the “Expert properties”. In the “Testing” tab you can choose on which criteria the EA should be optimized for. Here I choose “Profit Factor”, so the Metatrader Strategy Tester will optimize the EA for maximum Profit Factor. But you can also set other criteria.
Now go to the “input” tab. Here we are interested by the “Start, Step and Stop” columns. For this demonstration, I will test some different Take Profit levels in combination with some different Stop Loss levels and the Inverse Strategy function. I check the box next to the Take Profit an Stop Loss Variable and set the Start = 20, Step = 15, Stop = 95 (see the picture below) and the Inverse Strategy function to Start=false, Stop=true. Press OK and Start your Optimized backtest. Metatrader Strategy Tester will now test your EA’s profit several times with the combination of all these different settings. The Take Profit and the Stop Lose have 6 setting (20, 35, 50, 65, 80 ,95) and the Inverse Strategy has 2 (false and true). This makes a total of 72 passes (6x6x2).
After the calculation is finished (it took me about 7 minutes) you will be able to go to two new tabs called “Optimization Results” and “Optimization Graph”. There are the results of all Passes. On the Optimization Result (on the picture sorted by the most Profit factor) you will see all 72 passes with their Profit, Total trades, Profit factor and so on. You can see that the pass 69 made the best Profit Factor. It made 1.36. This is not overwhelming good but we optimized only 3 functions. Click with the right mouse button on the best result and choose “Set Input Parameters”. If your tester jams, look in the journal tab for clues. It may be because you have tried to optimize too many parameters in the one pass.
On the “Optimization Graph” you see all the pass below and the optimized parameter (profit factor in this example) on the right side.
If you now go to the input parameters you see that these settings are now saved. You see that the Take Profit is still 50, the Stop Loss has changed to 95 and the Inverse Strategy in no on true. Make a normal single backtest with these settings to see all transactions or to visualize the trades in a chart window. (You can now also save the settings to access them later)
If you now take a look at the EA you see that his performance has increased significantly.
Forward testing vs Historical backtesting
After you optimized your Expert Advisor you want to test it now in a forward test. Forward test means to test your EA in a demo or real account with real market conditions. Try it first on a demo account for sometime and then you can start on a real account. When you first test your optimized EA in a forward test you will probably quickly see that you are loosing money in the forward test although the backtest where incredibly good. Let me give you some reasons why this is so and how you can prevent this.
If you optimized an EA you do not only want to have the settings with the most profit but the settings with a profit and a good profit factor (the profit of all winning trades divided by the loss of all loosing trading).
To make sure the strategy tester optimized the EA with a high profit factor go to „Expert properties > Testing > Optimization“ and set the Optimized parameter to Profit Factor.
Before you optimize an EA you have to make sure what Model you have to use.
If you know that you EA only trades on the opening of a Candle and you use only Fixed SL an TP and no Position Management like TrailingStop, you can use the „Open price only“ or „Control points“ model, which are very fast. But for everything else you have to choose the „Every Tick“ Model.
Or you optimize it first once with the Control points and check the result with the Every Tick model. If the results are more or less equal, you can only optimize this EA with the „Control points“.
The first thing is that you tend to over-optimize your EAs. Let me quickly explain what over-optimization (curve fitting [wikipedia]) is:
You have to know that with the mt4 strategy tester you can easily get a really good backtest curve by backtest all input parameters of an EA with a lot of steps. But you aren’t looking for a great backtest result. You want to be successful in forward testing so you have choose less steps. For example if you want to optimize the StopLoss 40 to 160 and the TakeProfit from 20 to 80, do not optimize every single step. Choose a step of 10 for the StopLoss and a step of 5 for the TakeProfit. This way the backtest will be less profitable but less over-optimized. Another advantage, if you take less steps, is that the optimization process is much faster.
So how to make sure your backtest is not over-optimized?
You can do this with a simple trick. If you test 1 year back you want to split off the last third or fourth of the year and optimize only the rest. If you optimized your EA test it on the piece that you just split off. If you get more or less an as good performance, your are good to go. If your EA is over-optimized you will see the curve sloping down and you have to re-optimize the Expert Advisor again.
Do not backtest your Expert Advisor too far in the past. It is no useful if you optimize your EA back to the Year 2000. The markets have changed a lot. You want to optimize you EA based on the latest History (1-3 Years). But for what time period should you test your EA? In my experiences, the backtest time is not essential. You want to have about 200-300 trades in a backtest to make it meaningful regardless what time period you backtest.
Diversity in forward testing
Do not only take one setting for your forward testing. You can make the optimization process for multiple Timeframes and Currency pairs. You can easily forward test many settings on your VPS (Virtual Private Server). Take multiple settings for every Currency Pair and Timeframe. You only have not to forget to set a different MagicNumber on every chart! Also use myfxbook or fxblue to track your account.
Expert Advisors with small SL/TP
If you have an EA that places small StopLoss and TakeProfit its hard to get it good optimized. In the Backtest you do not have Slippage, order open delay and changing Spread. So this 3 thing will have proportional more effect on EA with small StopLoss and TakeProfit. You will barely notice this on an EA with StopLoss and TakeProfit above 15-20pips. The only thing you can do here is to set a fixed Spread (about 2 pips) for your backtest and make a forward test. Not only on a demo but also an a Live account with the minimum of lot.
If forward test your Expert Advisor you want to make sure that you use a spread filter. Sometimes the spread of a broker can rise quickly and if you open a trade with a spread of 10pips you already lost about 8pips.
So, in general, we can bunch it together like this:
Use Every Tick
Use few optimization steps
Look for about 200-300 trades.
Test the EA forward in Demo or Live with small lots.
Use a spread filter
You know now how to optimize an Expert Advisor in the Metatrader 4 strategy tester. If you liked this tutorial feel free to share this page.
Feel free to leave a comment or open a discussion in the forum.