How to Optimize an Expert Advisor in MetaTrader

A key feature provided by MetaTrader 4 is that it allows you to optimize the performance of your Expert Advisor as a part of the backtesting process.

This feature allows you to change the parameters of the strategy, such as the stop loss and take profit levels for example and try out these variables against historical market data.

The optimization process automatically works through the parameters you define  in order to find the best combination of settings. This will allow you to find out the settings that produced the highest level performance for the strategy over a selected market and testing period.

There is of course no guarantee that optimized settings will be the most suitable going forward. Market dynamics will change over time, however it can prove a useful exercise to keep your strategies tuned into recent market dynamics to ensure that they are making the best returns for you.

The optimisation process in MetaTrader is much the same as for standard MetaTrader Strategy backtesting which you should be familiar with before you optimize your Expert Advisor.

Optimizing Your EA

To begin optimizing your Expert Advisor, open up the Strategy Tester from the View menu in MT4. Use the drop down menus to select your EA, the market to test against, the period and the date range you want to test across.

optimize an expert advisor

When setting the Model to test against you can select two options here. For a normal backtest it is recommended that you select Every Tick in order to gain the most accurate test results. However given that the optimization process can be incredibly slow, you can also make use of the Control Points Model.

This will provide you with a much faster (though less accurate) optimization of your EA. However it will be sufficient to give you a set of optimized settings which you can then more accurately test with Tick data.

Finally check the box marked Optimization.

Next you need to enter the parameters that you want to optimize the EA against. Click on the Expert Properties button to bring up a new dialogue.

metatrader-optimization-settings

Here we are mainly interested in the Inputs tab.  This is where optimization variables are defined.

Within this tab there are  five columns -

Variable – This is the variable in the EA that you can optimize

Value – The default value for the variable

Start – The start (lowest) value to test against

Step – The incremental ‘step’ to test between the Start and Stop value

Stop – The stop (highest) value to test against

To enable a value for optimization, you will firstly need to select the checkbox next to the variable that you want to optimize. You will then be able to add the range (start/stop) and the ‘step’ that you want the optimizer to check against. For any variables that you leave unchecked, the default Value will be used.

Note that the variables available for optimization will be dependent on what has been exposed in the code of the EA. For commercially released EAs you will generally be able to find an explanation of the available variables in the supplied manual.

In the example above the SL (stop loss) inputs are set for optimization. The stop will be tested from 55 – 75 pips in 10 pip increments (e.g. 55,65,75). Try to use logical increments and bear in mind that the smaller the step level used, the longer it will take for the optimization process to complete.

Once you have entered your settings you are ready to start running the optimization on your EA. Click on the Start button in the Strategy Tester window to begin.

Optmizing an Expert Advisor is a much slower process than straightforward backtesting. The process can take anything from just a few minutes to several hours to complete. The actual time taken will depend on a number  of factors. These include the number of settings you are optimizing, the step levels used, the selected date range and even the processing speed of your computer.

Optimization Data

Once the optimization is complete, the final stage is to analyze the output. To do this click on the Optimization Results tab at the bottom of the Strategy Tester window.

metatrader-optimization-results

Here you will see the results of the process. The two key things you need to take note of are the Profit and Drawdown columns. Sort each column by clicking on the respective header.

What you are looking for is the best combination of the two i.e. the highest level of profit with the lowest level of account Drawdown. Under the Input column you will see the settings that generated these results. Note down the details for the best combinations.

As a final step you will need to validate these settings with a more accurate backtest. Run the EA through the Strategy Tester again with the new Inputs. For this test you will need to use the the Every tick model with 1M(minute) Tick Data. This will generate you the most accurate backtest results from the optimized inputs.

Optimizing an Expert Advisor in MetaTrader won’t guarantee you good performance. However if you undertake this task periodically you should be able to help ‘tune’ the performance of your system to the most recent market dynamics.

Top Of Page

About the Author

FX System Trader is