The great part about MetaTrader 4 is that you can create automated trading strategies called Expert Advisors (EAs) and backtest them in the built-in Strategy Tester. If a strategy works well after thorough testing, the next step is to start testing it in a demo account to see if it works in real-time market conditions.
But using the Strategy Tester can be confusing when you use it for the first time. The first time I used it, I thought it was broken 🙂
So in this video, I'll show you how to go from beginner to pro, when it comes to backtesting automated trading strategies in MetaTrader 4. Learn how to do a quick test, use the strategy optimizer and use visual mode.
The text version of the video is provided below the video.
How to Setup the MT4 Strategy Tester
Before you do anything in Strategy Tester, make sure that the EA you want to test is installed. You can do this by going to: File > Open Data Folder.
From there, open this folder: MQL4 > Experts. Then drag your EA file into that folder.
You can also check to see if the EA is already installed.
Once your EA is installed, restart MT4.
Now that your EA is installed, open MT4 and click on the Strategy Tester button. This feature is NOT available in the mobile version of MT4.
The Strategy Tester window will appear at the bottom of your screen. You are now ready to do your first test of your EA.
How to do Your First Backtest in the Strategy Tester
Here is an explanation of the settings:
- In the first box, select your EA from the dropdown list.
- Symbol: Select the symbol that you want to backtest on.
- Model: Use the default Every tick setting.
- Use Date: Leave this box checked and select the From and To dates that you want to use for this test.
- Visual mode: Leave this box unchecked for now. You can also ignore all of the settings immediately to the right of this box. They will be grayed out anyway.
- Period: Select the timeframe chart that you want to test from the dropdown list.
- Spread: Leave as Current.
- Expert properties button: Click on this button to change the settings of the EA.
- Symbol properties button: You won't have to use this button in this tutorial.
- Open chart: Only click this button after a test has been completed.
- Modify expert: This is only used to modify the source code of the indicator. You won't need this for your first test.
- Optimization: Uncheck this box. We will get into optimization later in this post.
Once everything is setup, then click on the Start button.
Alright, this is where things may get confusing…
When you first start the test, the green bar will move from left to right. This first pass shows that the data is being loaded.
Then the green bar will reset and start moving from left to right again. This is when it's actually testing.
You won't see anything happening on the chart. MetaTrader 4 is testing in the background.
This is the fastest way to backtest because it doesn't have to constantly update the chart. If you want to see the results in real-time, go to the Graph tab at the bottom of the screen. That will show you the results of every trade, as it happens.
You can also look on the Report tab to see the stats on your test.
The Results tab will show you all the trades that were taken.
Once the test is completed, you can click on the Open chart button and you will see of the trades that were taken.
This chart can help you understand if your trades are being executed as you expected.
When you double click on a trade in the Results tab, and you have the chart marked with your trades open above, the chart will jump to that trade.
That's the fastest way to backtest your EA.
Another way to backtest is to use visual mode…
How to Use Visual Mode in Strategy Tester
Visual mode will open a chart and show you the execution of every single trade.
In order to use visual mode, simply check the Visual mode box before you click the Start button on your test.
This is what you'll see on your chart.
Once the data loads, you'll see the chart and the price will start changing. In order to speed up the process, move the slider next to the visual mode box all the way to the right.
Even at “full speed” the chart will move pretty slowly. So you might wonder what the benefit is to using this mode.
The primary benefit is seeing how your strategy executes in almost real-time.
When you look a the code or to a standard backtest, it can be difficult to see where the EA is making mistakes or how your settings are affecting the results.
But sometimes when you see trades executing as price moves, the issues become very clear.
Therefore, this method of testing is not geared for speed, but it's a way to visually spot issues or optimizations.
MT4 Strategy Tester Optimizer
Now that you have a good feel for how the Strategy Tester works, you can start optimizing some of your parameters.
It would be great if there was some sort of fancy AI powered optimization algorithm, but unfortunately there isn't.
Strategy Tester uses a very simple iterative process of trying different EA settings and showing you the results.
But that can be a good thing. When you use an AI, you don't quite know what it's doing in the background.
With the MT4 optimization process however, you know exactly what's happening and identifying the best settings is a straightforward process.
To use the optimizer, click the Optimization box before you start the test.
The way that MT4 optimizes your EA settings is by allowing you to set a starting, ending, and step up value for each of your EA settings. For example, here are the settings that are available for the EA that I'm using in this example.
For each setting, you can set the following:
- Checkbox: The parameters that are checked are the ones that will be optimized.
- Value: The value used for normal testing.
- Start: The start value used for optimization. This value is not actually tested.
- Step: The increments that are used in each of the tests. In this example the step is set to 10, so the value for this input will increase by 10 on each successive test.
- Stop: This is the final value that will be tested during optimization.
Just to clarify how this works…
If you use a starting value of 10, a step value of 10 and stop value of 30, MetaTrader 4 will test 20 and 30 only. It will not test the starting value of 10.
Also keep in mind that the more inputs you test, the longer your optimization tests will take.
Therefore, it helps to start off by testing one input at a time and use larger step intervals. Once you get an idea of what works, you can start to fine tune the settings.
After you get results that you like, be sure to do one last test with the normal Strategy Tester settings to make sure that all of your new settings work well together.
Where to Get MT4 EAs
There are many places that you can get MT4 EAs.
You can start by taking a look at the MetaTrader EA repository. This will give you a good idea of what is out there.
However, you'll quickly see that there are a ton of EAs, but you don't know how most of them trade. If you don't know how they work, how will you know when they stop working?
Therefore, the best thing to do is to learn a trading strategy, test it, then create your own EA.
Even if you don't know how to program in MT4, you can always find a programmer to create an EA for you.
I've used several programmers over the years and they were able to create exactly what I wanted.
To see the programmers I use, go here.
The Difference Between .MQ4 and .EX4 Files
You may notice that there are two different types of EA files. This section will explain the difference.
There are two different types of files because there is one file that humans can read and one file that that MetaTrader can read.
The file that human programmers can read (the .mq4 file) is not very efficient in terms of processing speed, so it's converted into a version that the computer can use efficiently (the .ex4 file).
These files are also known by the following names:
- .ex4 = compiled, program or executable file
- .mq4 = source code file
When you add a .mq4 file to MT4, it will automatically be converted to a .ex4 file, when you open MetaTrader 4.
If you purchase an EA on the internet, it will usually come in .ex4 format. This is because the .ex4 file hides the developer's programming and doesn't allow others to copy the code and resell it.
However, if you create an EA yourself, or you work with a programmer to create an EA from scratch, the EA will be in .mq4 format.
One final note on these files…before working with a programmer, be sure that they will give you the .mq4 (source code) version. That shouldn't be a problem since you're paying them.
But double check before you start working with them.
To get help with finding a MetaTrader 4 programmer, I have created this guide and list of programmers.
…and no, you cannot use MT4 files with MT5, and vice versa. This is a common question.
A Final Word of Caution
Finding an EA that will make you money while you sleep is a sexy idea. But remember that there are no free lunches in trading.
You can still screw up an EA that has been profitable in backtesting.
Here are a couple of ways that you can do that.
First of all, you have to decide when to turn on your EA.
That may sound obvious, but when an EA hits a losing streak, it can be very tempting to turn it off to stop the bleeding.
…and when you turn it off is usually when it starts working again…or so it seems.
So remember that you have to understand the statistical characteristics of your EA, so you know when it is doing something that's out of the ordinary.
For example, if your backtesting shows that you should only have a maximum of 5 losses in a row, but your EA has currently lost 10 trades in a row, then it's probably time to turn it off and evaluate what's going on.
However, you certainly shouldn't turn it off after 6 losses in a row, no matter how mentally painful that is. Based on your stats, that's usually the time when it should start winning again.
Second, many new traders don't test their strategy enough and jump into live trading too quickly.
There are many variables that can affect the performance of an EA in live trading, so be sure to “stress test” them all before you risk real money. Test it with a wide spread, multiple pairs and use as much historical data as possible. Six months of data is not a valid test.
The more robust your testing, the more likely your EA will work in live trading.
Now get to work testing 🙂