Strategy Optimization
Strategy Optimization
MetaTrader 5 Help
MetaTrader 5 Help → Algorithmic Trading, Trading Robots → Strategy Optimization
Expert Advisors and Custom Indicators Where to Find Trading Robots and Indicators
Strategy Optimization
The Strategy Tester allows you to test and optimize trading strategies (Expert Advisors) before
using them for live trading. During testing, an Expert Advisor with initial parameters is once run
on history data. During optimization, a trading strategy is run several times with different sets of
parameters which allows selecting the most appropriate combination thereof.
The Strategy Tester is a multi-currency tool for testing and optimizing strategies trading multiple
financial instruments. The tester automatically processes information of all symbols that are used
in the trading strategy, so you do not need to manually specify the list of symbols for
testing/optimization.
The Strategy Tester is multi-threaded, thus allowing to use all available computer resources.
Testing and optimization are carried out using special computing agents that are installed as
services on the user's computer. Agents work independently and allow parallel processing of
optimization passes.
An unlimited number of remote agents can be connected to the Strategy Tester. In addition, the
Strategy Tester can access the MQL5 Cloud Network. It brings together thousands of agents
around the world, and this computational power is available to any user of the trading platform.
In addition to Expert Advisor testing and optimization, you can use the Strategy Tester to test the
operation of custom indicators in the visual mode. This feature allows to easily test the operation
of demo versions of indicators downloaded from the Market.
How to Optimize
Optimization means multiple runs of an Expert Advisor using history data with different sets of
parameters, aimed at finding their best combination. During multiple runs, different
combinations of the input parameters of an Expert Advisor are tested to find the best ones.
Watch the video: How to test Expert Advisors and Indicators before
purchase
Watch the video to learn how to test a trading robot before you
purchase it from the Market. Every product on the Market is provided
with a free demo version, which can be tested in the Strategy Tester.
Please watch the video for further details.
Some of the major strategy testing and optimization tasks are presented in the start page. In
addition, one of the previously performed tasks can be restarted from this page. If you have run a
lot of tasks and they do not fit into the start page, use the search bar. You can find a test by any
parameter: program name, symbol, timeframe, modeling mode, etc.
After selecting a task, the user proceeds to further testing parameters setup: selection of an
Expert Advisor, symbol, testing period, etc. All irrelevant parameters which are not required for
the selected tasks are hidden from the setup page. For example, if mathematical calculations are
selected, only two parameters should be specified: selection of a program to be tested and the
optimization mode. Testing period, delay and tick generation settings will be hidden.
Before you start optimization of a multi-currency Expert Advisor, enable the symbols required for
testing in the Market Watch. In the context menu, click " Symbols" and enable the required
instruments.
The price data of all necessary symbols are automatically downloaded from the server
during testing and optimization.
Testing starts and ends at 00hr.00m.00s. of the specified dates. Thus the start date of
testing/optimization is included in the testing period, while the end date is not included.
Testing ends on the last tick of the previous date. Also you cannot specify the end date,
which is greater than the current one. In such case, the testing anyway will be performed
to the current date (not including it).
The quick optimization based on the genetic algorithm is enabled by selecting optimization
criteria in the field located to the right. This field sets the parameter, based on which the most
successful Expert Advisor runs are selected. The larger the value of a selected parameter, the
better the result.
After setting all the parameters click "Start". This launches the process of testing and
optimization.
The settings of the strategy tester are memorized as testing/optimization is started.
In case of a regular optimization stop (when you press the Stop button) all the previously
calculated runs are saved. When the optimization process is resumed, it continues from
the last calculated run.
Optimization is testing different values and combinations of input parameters to obtain the best
result.
To enable the optimization of a parameter, mark the appropriate checkbox. Next set the start
and end of the range of values, as well as the step for testing. You can select one or more
parameters. The total number of possible combinations is displayed beneath the list of
parameters.
Parameter sets. You can at any time return to the current settings of your MQL5 program by
saving a set of its parameters using a context menu:
To save the parameters as a set-file on your computer, click "Save". These files can be
moved between platforms on different computers or sent to other users.
To save parameters for future use in the current platform, click "Save Version". These
saved presets will be available then in the "Load Version" sub-menu. They can be applied
at any time by selecting an appropriate version from the list.
Common
In this section, you can set the maximum number of open orders and positions, which can
simultaneously exist on the account. Additionally, you can configure sessions during which the
program is not allowed to trade.
Margin
The section allows configuration of margin reserving rules and position accounting systems to be
used in testing:
Commission
This section provides control over commissions charged on all trading operations:
Commission may be single-level and multilevel, i.e. be equal regardless of the deal
volume/turnover or can depend on their size.
Commission can be charged immediately upon deal execution or at the end of a trading
day/month.
Separate commissions can be charged depending on deal direction: entry, exit or both
operation types.
Commission can be charged per lot or deal.
Commission can be calculated in money, percentage or points.
To apply commission settings of the current trading account, enable the option "Use predefined
commissions".
Custom Testing Symbol Settings
You can overwrite settings of the main trading instrument, for which testing/optimization is
performed. Almost all specification parameters can be overwritten: volumes, trading modes,
margin requirements, execution mode and other settings. Thus, if you need to check an Expert
Advisor under different conditions, there is no need to create a separate custom symbol and
download its history. This can be done by changing standard symbol settings.
If the symbol specification is customized, the gear icon and the symbol icon are marked with an
asterisk. This shows that custom parameters are used for the current test.
Optimization Launch
To start optimization, click "Start" on the "Settings" tab. The optimization progress is displayed to
the left.
Where to View the Optimization Results
Detailed results of each optimization run are displayed on the "Optimization" tab. The tab
contains general testing results, including profit and the number of trades, as well as many
statistical values to help assess the performance of the trading robot.
The optimization report can be sorted by any parameter by clicking on the column header. Use
sorting to find the most profitable combination of parameters and run a single test for a detailed
report.
Using context menu commands you can show/hide some of the above columns. For
convenience, check the "Switch to Optimization Results" option: once the optimization process is
complete, the Strategy Tester will automatically switch to the Results tab. The same command is
available in the context menu of the Journal tab.
The table with optimization results is colored as follows to enable a more efficient visual analysis:
Balance: values greater than the initial deposit are colored in blue, and those less than the
initial deposit are shown in red.
Profit: blue is used for values greater than zero, and red is used for values less than zero.
Expected Payoff: blue is used for values greater than zero, and red is used for values less
than zero.
Drawdown: from green (0-5%) to red (greater than 30%).
Sharpe Ratio: from green (greater than 2) to red (less than 0).
Recovery Factor: from green (greater than 2) to red (less than 1).
If optimization includes forward testing, this tab also contains the corresponding values of
the optimization parameter (optimization criterion) for the back and forward tests. You can
switch between results of back and forward testing using the context menu.
A double click on one of the optimization results starts Expert Advisor testing with the
parameters of this run (provided that the optimization is over).
During genetic optimization one of the test runs (a population member) can have the
same parameters (genes) as the previous test run. In this case, this run is not displayed on
the results tab, because it has the same testing result. However, the optimization graph
display all test runs to visualize the process of searching for the best result.
If a line of an optimization run has the red background, it means that an error occurred
during Expert Advisor operation. An appropriate message is also added to the tester log
("tested with error").
Optimization Cache
The cache stores data about previously calculated optimization passes. The strategy tester stores
the data to enable resuming of optimization after a pause and to avoid recalculation of already
calculated test passes.
Cache files allow viewing results of previous optimizations. Open the "Optimization results" tab,
select an Expert Advisor and a file with the desired optimization cache:
The list contains all Expert Advisor optimization cache files available on the disk Optimization
date, testing settings (symbol, timeframe and interval) and input parameters are shown for each
file. You can additionally filter optimization results by the trade server.
From the result viewing mode, you can also change the optimization criterion, which you selected
at the start of optimization. It is displayed in the Results tab and determines the quality of a
tested set of input parameters. The higher the value of the optimization criterion, the better the
testing pass is considered to be. Select the desired criterion from the list at the top of the tab,
and the tester will automatically recalculate all values in the "Result" column.
To analyze results in third-party programs, for example, Office Excel, optimization report can be
saved as a file through the " Export to XML" command of the context menu. Also, the context
menu features commands for exporting and importing cache files. Use these commands to
transfer optimization results between different platforms.
For optimize disk space usage, cache files are automatically deleted if they are not
accessed within 30 days.
During the genetic optimization, intermediate results are saved in the cache after the
calculation of each generation (in a file platform_data_folder/tester/cache/*.gen). Thus the
optimization process can be interrupted at any time. Even if the process of genetic
optimization is interrupted as a result of an external factor (for example, a black out), the
optimization will be automatically continued from the last calculated generation once you
restart it. The genetic optimization cache is stored until the optimization settings are
changed or the optimization process is completed.
In case of a regular optimization stop (when you press the Stop button) all the previously
calculated runs are saved. When the optimization process is resumed, it continues from
the last calculated run.
The linear chart (1D) shows the variation of the parameter selected as the optimization criterion
(vertical axis) depending on one of the optimization parameters selected for the horizontal axis.
To select a parameter for the horizontal axis, use the "X Axis" command in the context menu.
In the three-dimensional visualization mode, changes of the selected parameters used for
optimization are shown on the X and Y axes. Variation of the optimization criterion is displayed
on the vertical Z axis and using a color gradient.
To select a parameters for the horizontal and vertical axes, use commands "X Axis" and "Y Axis" in
the context menu.
To move a chart, grab its central part using the left mouse button and move the cursor.
To rotate a chart around its vertical axis, grab it outside the center and move the cursor.
To rotate a chart around its horizontal axis, rotate the mouse wheel holding down the
"Ctrl" key.
To zoom in/out a chart, press "Ctrl" and move the mouse cursor vertically in the central
part of the chart holding down the left mouse button.
To move the zero plane, press "Ctrl" and move the mouse cursor vertically outside the
central part of the chart holding down the left mouse button.
To return to the initial position of the chart, double click in its central part.
Action Keys
The camera moves down (the chart moves up). Down Arrow
The camera moves to the right (the chart moves to the left). Right Arrow
The camera moves to the left (the chart moves to the right). Left Arrow
Rotate the graph around the vertical axis in a clockwise direction. Page Down
Moving the zero plane to the maximum value of the graph. Ctrl+Home
Moving the zero plane to the minimum value of the graph. Ctrl+End
Setting the maximum transparency of the zero plane (it disappears). Ctrl+Right Arrow
Setting the minimum transparency of the zero plane (it becomes Ctrl+Left Arrow
nontransparent).
To start the forward testing, in the Forward field of the Settings tab select the part of the total
period for it:
No — forward testing is not used;
1/2 — half of the specified period is used for the forward test;
1/3 — one third of the specified period is used for the forward test;
1/4 — a quarter of the specified period is used for the forward test;
Custom — specify the forward test start day manually.
The second (latest) part of the total period is always taken for the forward testing.
The forward test start date is displayed as a vertical line on the optimization graph.
The selected part is separated from the period specified in the "Date" field. The first part is the
period of back testing, and the second one is the period of forward testing.
The full optimization (slow or fast) of the Expert Advisor is conducted on the back testing period.
After that 10% (in the full search) or 25% (in the genetic algorithm) of best runs are selected and
then tested on the forward period.
There is a lower limit for the number of passes of forward testing. If the number of best runs
is less than 256, the additional best runs are used for forward testing until their number
reaches 256. If the number of all runs is less than 256, all of them participate in forward
testing.
Results of back and forward testing can be compared on the "Optimization Results" (select
"Forward testing results" in the context menu) and "Forward Results" tabs. The better the results
coincide, the more likely it is that the Expert Advisor will show good results in real trading.
The visualization of optimization results on the forward period is available on the "Forward
optimization graph" tab. To compare these results with the backtest, switch between them using
the context menu.
For details about testing results please read sections "Where to view the optimization results"
and "Visualization of optimization results".
Three types of agents are available: local, remote and cloud (MQL5 Cloud Network). Local agents
are installed automatically when you install the trading platform. Their number is equal to the
number of logical cores of the computer.
Remote and cloud agents run on other computers. For detailed information about agents, please
read "How to speed up optimization using a local farm of agents" and "How to speed up
optimization using MQL5 Cloud Network".
Open the "Agents" section in the Strategy Tester and select the type of agents you want to use for
optimization.
Tips and features:
To conserve the laptop battery, you can disable local agents and use only the remote and
cloud ones.
If testing/optimization is not finished manually (neither by pressing the Stop button at the
settings tab nor by closing the trading platform), the processes of used local agents are
not unloaded from the computer memory for 5 minutes. This feature allows avoiding
delays connected with preparing the price history and starting the agent processes when
re-testing/re-optimizing the same Expert Advisor at the same symbol, timeframe and time
period.
Only local agents are installed together with the platform installation. They are only used
in the Strategy Tester of the local platform. Remote agents that can also be connected to
the global MQL5 Cloud Network can be installed only manually.
Select the number of agents to be installed. They are installed based on the number of
logical cores.
Enter the password for connecting to the agent.
Select a range of ports for connection.
Click Add.
After installation, the agents are available for use from other computers on the local network.
messages of Expert Advisors (Print() function) and messages about trade operations are
not added to the Journal;
DLL call is prohibited on remote agents.
How to Connect Agents
Open the Strategy Tester. On tab "Agents", select "Local Network Farm" and click "Add" in the
context menu.
The easiest and fastest way is to automatically scan the local network for a range of IP addresses
and ports. Select them and enter the agent connection password that was specified during
installation.
Click "Finish" and all the found agents become available for testing.
Add agents (by IP address or domain name) — specify the IP address or domain name of a
server where agents are installed, as well as the range of ports and password for
connecting to the agents.
Import from file *.mt5 — select this option, click "Next" and specify the *.mt5 file from
which you want to import agents.
Agents installed on the computer using MetaTester 5 Agents Manager, can be connected as
remote on the same computer. If the processor cores have extra computation power during
calculations, they can take a higher load to use all the computing capacity.
Settings of different agents are separated from each other with line breaks.
The genetic optimization mode is automatically enabled when the total number of
optimization steps exceeds 100 million.
In addition to using the MQL5 Cloud Network, you can provide your CPU computing
power in the network. To install the remote agents and include them into the network, use
a special utility MetaTester.
Read more about the MQL5 Cloud Network on the official site.
To use MQL5 Cloud Network a user needs to have at least 1 US dollar on the
MQL5.community account. Tasks are passed in packages to several access points
simultaneously, and the user must be able to pay for completion of that tasks. The
Network is not able to calculate the exact cost as the time and resources required for
calculations cannot be estimated precisely before the start of calculations.
If you do not specify the details of your MQL5.community account before enabling the MQL5
Cloud Network agents, you will be offered to do this.
If you have not registered on the website, use the new account creation link.
Testing Features →
Trading Platform
Mobile Trading
Market
Signals
Automated Trading
Download
For Brokers
Contacts
Legal Information
MetaQuotes is a software development company and does not provide investment or brokerage services