This article explains the Expert Advisor test and optimization process. It starts with introducing what an Expert Advisor is, reasons to use or not, how to test and optimize, and anything you need.
Disclaimer: We do not guarantee your success if you download and test any Expert Advisor if it is free, or if you buy from someone else. Everything that read in this article is just to help you how to backtest and forward test only.
What Is an Expert Advisor in Forex Trading?
An Expert Advisor (EA), also known as a trading robot, is a small built-in software inside a trading platform. Its job is to recognize trading opportunities and place trades automatically based on given instructions.
An Expert Advisor works as long as your computer works and has an internet connection. So, make sure that you have a reliable computer and internet.
Notice that all Expert Advisors work sometimes and sometimes do not. The reason is that chart trends and economic conditions can change, and so can the profitability of a trading robot. So, for an Expert Advisor to continue to be profitable, it is necessary to re-optimize after a while.
There are a lot of Expert Advisors available for free to test on the internet and in the store of trading software websites. There are also countless Expert Advisors for sale online you can purchase.
If you are a programmer, you can build your Expert Advisor. To write a good robot, you need to be a professional trader because an Expert Advisor is programmed based on traders’ experiences.
If you buy an Expert Advisor, they provide you with some instructions as well. Even if you buy an Expert Advisor, you still need to understand how an Expert Advisor behaves, how much you can expect it to make you money, and when you may need to stop using Expert Advisor.
Reasons to Use or Not Use an Expert Advisor in Trading
There is no guarantee to make money running an Expert Advisor. You may not make money at all. Your personality, trading experiences, knowledge, testing and optimization skills, etc. directly impact your success in the market.
So, let me give you my opinions on why and why not you should run a trading robot.
Reasons to Not Use an Expert Advisor in Trading
- You are emotional: Emotions impact your decision by creating greed (specifically in trading), fear, excitement, and anger. Since money is involved in trading financial assets, emotions can impact directly and instantly, resulting in poor-quality decisions. So, if your emotions affect your decision-making process, running an Expert Advisor can be a good choice. An Expert Advisor makes decisions unemotionally and is free of fear and greed.
- If you are a short-term trader: A short-term trader constantly watches charts and should be. He responds to short-term volatility and makes more decisions than long-term investors. Responding to ups and downs in the market makes a trader emotional, and it will result in poor-quality decisions.
- You can optimize an Expert Advisor: Someone who has built optimization skills and can optimize in less time and more frequently. So, for them, it is a good option to run a Robot.
Reasons to Use an Expert Advisor in Trading
- You are not emotional: If you do not respond to volatility instantly, and you are in control of yourself in the decision-making process, you may not need an Expert Advisor. If you can control your emotions, then you can find the perfect balance between reasons and your decisions that serve you the best in trading.
- You are a long-term investor: A long-term investor analyzes macroeconomic indicators and social changes and does not make decisions based on short-term chart patterns. On the other hand, Expert Advisors place trades based on chart patterns and even candlestick patterns. If you are investing based on broad pictures of the economy and world, running a trading robot may not serve you the best.
- You can not optimize an Expert Advisor: Testing and optimization of EA requires skills. It is also time-consuming. If you don’t know how to test and optimize an Expert Advisor, it is not for you. Fortunately, this article explains in detail how to test and optimize. So, keep reading.
How Do Expert Advisors Make Decisions?
An Expert Advisor is programmed to use the quantitative data and time to make decisions based on orders received from the optimizer.
An Expert Advisor doesn’t make decisions based on fear, greed, or fundamental data and news. It only makes decisions about how it is programmed. To build an Expert Advisor, a programmer needs to be a professional trader so that he/she/they can write a better EA.
Does It Cost to Run an EA?
An Expert Advisor can be costly.
To run an EA, you need to program or purchase it from somewhere else.
Moreover, you need to run on VPS (Virtual Private Server) because your chart needs to be open or let your computer turn on all the time.
Before running an Expert Advisor, you need historical data to smoothly run your EA. Otherwise, it will not yield a good result.
Where Can You Download Historical Data from?
You need every-tick historical data to implement the backtest and forward test. Later, I explain backtest and forwardtest. Now, keep reading.
Generally, brokers limit the number of candles/bars displayed on your chart. It can be a problem if you test your EA using past historical data. To not encounter any problem, Go to the Tools menu>>Options>>Charts. Find “Max bars in history”, and “Max bars in chart”, and type 999999999999999.
You can get historical data from your broker, MetaQoutes, and third parties.
1. Downloading Historical Data from MT4
To download data directly from your platform, which is the best option.
Zoom out the chart as much as possible. And make sure that Auto Scroll in the Charts terminal is off. Now drag the chart from the left side to the right as much as possible. Repeat this process for every single period available on your platform.
Another way to get data from your broker is to check on its website. If available, download which is the best option because there will be no discrepancy between the tested data.
Go to your broker’s website and download the tick history of an instrument that you are willing to test your EA.
You need to import downloaded data into your platform. To import historical data in MT4, go to the Tools menu>>History Center>>find the symbol of the asset that you want to test>>select a timeframe, and finally click on “Import” located at the bottom of the window. Finally, browse on your computer find the downloaded data, and click on OK.
Note: To run smoothly without problems, it is always better to download historical data from your broker’s website or drag it from left to right for every timeframe.
2. Downloading Historical Data from MetaQoutes Inventory
MetaQoutes is the developer of MetaTrader 4 and 5. It allows you to download the historical data directly from the MT4 platform.
To download MetaQoutes historical data, go to Tools>>History Center>> find the symbol you want to test your strategy>> double click on it>> select your timeframe and finally click on download.
You will get a warning that the data provided by MetaQoutes might be different from the data provided by your broker. It can be an issue if you are testing on short periods, such as one minute and two minutes. If you test your strategy on higher time frames such as hourly, daily, and weekly, it will not be a big issue.
3. Downloading Historical Data from Third Parties
Some organizations provide historical data for free. They could be brokers or other organizations.
There are always some differences between ask and bid prices among brokers and it is very likely to differ with your broker’s data. So, to run smoothly and get a proper result, I do not recommend data from third-party.
Moreover, if you download from a third-party data provider, you need to convert them which is a waste of time.
What Are Backtesting and ForwardTesting?
Backtesting and forward testing of trading a financial asset is necessary to test the effectiveness of an Expert Advisor.
Backtests and forward tests are performed using historical data. However, it might not be enough. Traders to make sure that their robot is working well, will test again using a demo account with live data.
To clarify the above terms, we divide the past available data into two parts. The first part is devoted to the backtest, and the second part is for the forward-test. It is recommended the first part be longer than the second part.
First, you test your Expert Advisor on the first part of the data. As an optimizer, change the parameters until you find the best parameters that generate the most profits.
Second, the parameters you derived during backtests, and test them on the remaining data are called forward-test.
If the correlation between backtests and forward-tests results are positive, test them on a demo account.
If the result of backtests and forward-tests on the demo account is positive, you may risk real money.
Both tests are necessary because the result of backtests can be uncorrelated with the forward test and future performance tests. You often see that a set of parameters in your robot results positive in the backtests and negative in the forward tests.
The whole process of testing takes a long time. So, be patient. Additionally, winning parameters may work for a certain period, and you may need to optimize again or even stop trading with the Expert Advisor. Nothing performs well forever.
Introduction to Strategy Tester in the MetaTrader 4
Strategy Tester is a window in the MetaTrader 4 platform where you can test your Expert Advisors.
To open the Tester, go to the Window Menu and click on Strategy Tester. Moreover, you can insert by Ctr+R or by clicking its icon in the Standard Toolbar.
The Strategy Tester window has two tabs, Settings and Journal at the bottom. The settings tab contains parameters that need to be selected, and the Journal shows the result of your activities.
Elements in the Expert Advisor Settings?
In the Settings tab of Strategy Tester, you can see:
- Expert Advisor: The Expert Advisor shows the list of available Expert Advisors. If you have purchased one, open the Data Folder, paste it into the Expert folder, and restart your platform. You will see it in the Expert Advisor drop-down menu.
- Symbol: represents the financial asset or the forex pair you want to test on your strategy. Remember that some EAs are developed for a specific symbol. Choose correctly.
- Model: The model allows you to select the price in a time frame (candlestick). There are three options, and they are: a) Every tick: considers every single price change. It is the most precise and time-consuming way to test an EA. b) Control points: This option considers, open high, low, and close (OHLC) prices. It is the second most accurate way to test. c) Open price only: considers the open price only. This method is the least accurate and least time-consuming option.
- Use data: This allows you to select the range of time that you want to test;
- Visual mode: If checked, you will see an EA’s performance on the chart. You can also skip days that you don’t wish to visualize. If you check Visual mode, it will last longer than not checked.
- Period: represents the timeframe such as the hourly, daily, minute, and so on;
- Spread: Spread is the difference between ask and bid prices. The spread can be high or low depending on the market volatility. It means that during macroeconomic news events, the spread is high and the market more volatile, and during the time that two markets are open, but when there is no macroeconomic news, the spread is lowest.
- Optimization: If checked, it will allow optimizing your Expert Advisor based on parameters you select in the input tab of the Expert Advisor window.
- Expert properties: This option contains the most important elements of an Expert Advisor that are needed to optimize. This option, due to its importance, is explained in detail and separately;
- Symbol properties: This option contains the contract specification of the symbol selected for optimization such as swap, spread, and so on;
- Open chart: It opens the chart above to see the result of performance in the chart;
- Modify expert: This option allows you to change the codes of the Expert Advisor and make a new Expert Advisor;
- Start: Click on it to start the testing process after you set the settings.
Note: If you do not understand the above terms, be patient. I explain each of them in detail.
Testing MACD Sample Expert Advisor As an Example
There are countless Expert Advisors that you can test, paid and free version.
A trader can download Expert Advisors from Market, which you can locate from the Terminal window. There are both free and paid versions. Because some have too many input parameters, and too many parameters do not mean greatness, I avoid downloading from the Market.
Two Expert Advisors also come with the MetaTrader 4. They are the Moving Average Sample and MACD Sample.
In this article, I test the MACD Sample Expert Advisor, made based on the MACD indicator. If you want to know what a MACD indicator is check: MACD Indicator: Calculation & How to Trade It?
You can locate the MACD Sample Expert Advisor from the Naviagation>>Expert Advisors or choose from the drop-down menu of the Tester Window.
MACD Sample Expert Advisor Properties & Example
The “Expert properties” button is at the top right side of the Strategy Tester. As you click on it, a new window opens.
To make this article shorter, I continue the explanation with an example. Look at the below image.
The Expert properties window has three tabs, Testing, Inputs, and Optimization.
In this section, I explain the Testing and Input tabs, and at the end of this article Optimization, because it belongs to the end of our test.
This tab contains:
- The Initial deposit – is the amount of money and the currency symbol. And I wanted to start with 10,000.
- The position – allows you to select the type of your position, which includes “Long-only”, “Short only” and “Long and Short”. The meaning of long is to buy, and the short means to sell an asset. I wanted the MACD sample EA to place both long and short positions.
- The Optimization – section contains parameters that you can optimize. Click on the arrow to see those parameters. In our example, I wanted to optimize the balance.
- Genetic Algorithm – if checked, will ignore useless sets of values during tests, which saves you time. And I checked it.
This tab contains the main parameters you change to get profitable results. And they are the most important factors in backtest and forward tests.
(Loot at the above image.)
Each Expert Advisor has a certain number of rows that make it different from one another. Some have a few and some have many.
The number of columns in every Expert Advisor is the same. It starts from Variable and ends with Stop. Your job is to find the best values that make your Expert Advisor the most effective.
Right now, I explain input parameters and then explain our example. I think it is a better way to explain this section.
- Variable – represents what you can modify, for example, stop loss and take profit points. You should check a variable you want to optimize. Otherwise, it will use the default value. And let us know what these variables in the MACD sample EA are.
- TakeProfit – lets you put take-profit values in points if your broker is a five-digit and in PIPs if your broker is a four-digit.
- Lots – represents your trade size in a lot, which is 100,000 currency units in a standard account.
- TrailingStop – adjusts the stop-loss as the price goes in the direction that makes a profit however, it does not follow the price if it goes against the direction that makes a profit.
- MACDOpenLevel – is a limiter parameter for opening new positions calculated in PIPs. In a new long position, the MACD line and signal should be below the MACDOpenLevel, and in a short position the opposite.
- MACDCloseLevel – is a limiter parameter for closing the opened position in PIPs. In a long position, the MACD line and the signal line should be above the MACDCloseLevel and the opposite in the short position for the order to close.
- MATrendPeriod – is a moving average and another limitation that should be met before trade execution.
- Value – represents the value of variables to place trades. When you are not optimizing numbers in this column are counted. And when you check a variable, during optimization, MetaTrader 4 ignores these values and uses Start, Step, and Stop values to optimize the EA;
- Start: represents the start of the respective variable. In other words, the MetaTrader 4 starts with this value to backtest an EA.
- Step: represents the number of steps to add until it reaches the stop point; and
- Stop: represents the last value that the Expert Advisor considers while testing.
At the bottom, you have five buttons and they are:
- Save: it saves the parameters’ value found profitable
- Load: it loads saved parameters’ value
- Reset: Reset the setting to default
- Cancel: cancel
- OK: Save settings
Example of MACD Sample EA Settings and Inputs Parameters
Let us start with general settings.
I wanted to:
- AUDUSD currency pair symbol;
- Every tick model;
- Use data from 2023.01.05 to 2023.03.05;
- Uncheck the Visual Mode because it is a waste of time;
- Test on hourly-chart;
- The Spread to be Current; and
- Check the Optimization because I want to optimize.
We need to open “Expert properties” from the top-right Tester window, and change default inputs.
If we don’t touch the default values the result will be like the following.
And of course that is not profitable.
I changed parameters in our example as the followings:
- Take-profit starts with 200 points (20 PIPs) and adds by 100 points until it reaches 700 points.
- In our example, I wanted the lot, its default value.
- Trading Stop Loss starts with 200 points and adds 100 points till it reaches 700.
- MACDOpenLevel starts with 20 and adds 4 till it reaches 100.
- MACDCloseLevel starts with 20 and adds 4 till it reaches 100.
- MATrendPeriod starts at 14 and adds by 5 till it reaches 70.
The number of set of parameters that an Expert Advisor will test depend on the number of variables and values in the Sart, Step, and Stop columns. And those variables that are not checked, the MT4 will use their default values.
Suppose you are OK with other default parameters and only want to optimize the Take Profit level. If you put 200 in the start (which is 20 PIPs), 100 in the Step, and 700 in the Stop, your EA will perform these tests: First, it will select 200 points from the order point as take-profit, followed by 300, 400, 500, 600, and 700. If we count sets of parameters, there will be six sets of parameters.
That is not all.
If you do the same with Trailing Stop-Loss, there will be 36 sets of parameters, which are calculated by multiplying the number of sets of parameters each variable creates.
In our example, there will be 190,512 sets of parameters to test if the Genetic Algorithm is unchecked and 6,270 if it is checked.
How to Read Expert Advisor Optimization Results?
The Optimization Results is located next to the Settings.
This tab shows optimization results, which is the outcome of backtesting. Also, we use these results to sort out promising results for forward testing.
For more clarification, let us understand this tab’s columns from the left to the right.
- Pass – shows the number of sets of input parameters.
- Profit – represents the amount of profit or loss in a currency.
- Total trades – represents the number of times that trades were placed.
- Profit factor – is the ratio of gross profit to gross loss.
- Expected payoff – is the average profit expected to make for each trade (Net Profit/Number of Trades).
- Drawdown $ – is the largest potential loss in dollar terms.
- Drawdown % – is the largest potential loss in percentage of account balance.
- Inputs – lists the parameters of the Expert Advisor, and the elements vary based on the EA.
When you right-click on the Optimization Results, you see the following:
- Set Input Parameters – set the selected sets of parameters in the Expert properties windows to test or save it for forward test.
- Copy – allows you to copy a row and paste it somewhere else. I did not find it helpful.
- Copy All – lets you copy all rows.
- Save as Report – allows you to save as an HTML file that contains Optimization Results (excluding the inputs column and the Optimization Graph. Check out the next image.
- Skip Useless Results – ignores unprofitable results. In other words, if this option, unprofitable results will not be listed.
- Show Input Parameters – will show/hide Input Parameters that you can locate on the right side of the Optimization Results tab.
- Auto Arrange – allows you to arrange automatically columns.
- Grid – separates cells. You can check or uncheck to see or not.
Important: you can sort out results by clicking on the arrow on the right side of the parameters. For example, you can sort by Profits, Drawdown $, or any other parameter. Sorting helps you find promising results that may be used in the Forward Test.
What Is Expert Advisor Optimization Graph?
The optimization graph shows the results of sets of parameters performance results.
The horizontal axis shows the series of trades, and the vertical axis shows the balance.
For example, the set of parameters number 854 (look at the below image) shows that this set increased the account balance from 10,000 to 10,471 with parameters of take-profit=600, stop-loss=500, MACDOpenLevel=48, MACDCloseLevel=84, and MATrendPeriod=14.
As you see, several sets of parameters produced acceptable results in our backtest. However, there is no guarantee that these sets of parameters generate the same results in the future.
So, to cut the risks associated with these good sets of parameters, we sort out promising results and start forward testing.
How to Do Forward Test?
The Forward Test starts after the backtest finishes.
A backtest produces 100s or thousands of results. However, most of them are useless because they are not profitable sets of parameters.
To isolate unprofitable sets of parameters from unprofitable sets, an optimizer should handpick promising sets of parameters. And use them for the Forward Test.
How to handpick promising sets of parameters?
There should be some criteria to pick a set of parameters for the forward test.
And I prefer to give more weight to Profit and Drawdown.
Profit refers to the return on total trades performed during the test, and drawdown refers to the biggest balance fall from a pick.
Higher profit and lower drawdown are better. Higher profit means a better return, while a small drawdown translates to less risk.
That is why there should be a balance between profit and drawdown.
For example, if the profit is the largest among sets of parameters and also the drawdown, it is not a preferred set of parameters.
To save a set of parameters, select the one you want to save>>click on Expert properties>>click on save in the inputs tab>>name it and click on save. Continue this process for all promising sets of parameters.
Here is the list of 17 sets of parameters that I thought should be part of the Forward Test. And I sorted by profit.
However, past performance does not guarantee the future success. That is why we need to test in a new time range to find out those sets that fail in a new range.
How to Choose the Best Sets of Parameters in Forward Test?
So, should select one or a few best sets of parameters handpicked earlier to put real money at risk.
To do so, we tested selected sets of parameters in the next three-month time range.
Some of the good results that were performing amazingly, failed to do well during the forward test.
Here is an example
Here is how to test saved sets of parameters.
To find out the best-performing optimization sets of parameters, you should load the handpicked sets you saved earlier.
To load an optimization set, go to Expert Properties>>click on Load in the inputs tab>>select the set>>click Open>>click OK and hit Start.
Some of the handpicked parameters did well, and some failed.
Look at the below image. It made $408 profit in the first three months of our backtest. But turned out to be a disaster in the three months after that. So, it should be ignored.
I continued to test and delete sets of parameters that failed.
Surprisingly most of them (over 50%) did well.
Here are examples (not all) of those parameters that did well.
I did a second round of the Forward Test because there were too many successful sets of parameters. And, testing once more will help to pick the best from the list of successful sets.
Unfortunately, this time most of them failed. Only four sets of parameters turned out to be promising, as shown in the below image.
Before putting real money at risk, test successful results with a demo account. And when you are happy, you may put real money at risk.
Results, Graph, and Report of an EA Performance
Until now, I have not talked about the Results, Graph, and Report tabs. Now, it is time. Check out the below image.
Generally, these tabs show how your Forward Test performed. Notice that they are not related to backtest, but Forward Test or manually select variable values in the inputs tab of Exert properties.
Our article Forex Trade History Report in MT4 & MT5 (A-Z Guide) explains in detail these three tabs. And I encourage you to read it.
What is Optimization?
Optimization means to make the performance of a tool the most possible effective.
There are parameters in Expert Advisors, such as the trade size, Stop Loss level, and Take Profit level used to make an Expert Advisor the most effective by finding the best values. As the values change, the result of performance also changes. Your job is to find the best values and parameters that make your Expert Advisor most effective.
Depending on your EA, the number of limitations can vary which impacts the result.
Optimization becomes more important if drawdown, consecutive losses are daunting. So, you may need or may not need to set limitations.
Look at the below image to see what you can limit.
In our test, because there weren’t too many consecutive losses and the drawdown wasn’t huge, I do limit. And let the default settings run.
Final Words About Expert Advisor Test
Algorithm trading or running an expert advisor is not for all. If you want to run an expert advisor you must know how it works. In other words, you need to know what technical analysis rules your robot follows.
Expert Advisors do not understand fundamentals. They completely ignore what news is coming up. So, during political turmoils frequently check your EA.