How to backtest and optimize an Expert Advisor
Backtesting and optimization of an Expert Advisor in a Strategy Tester
- Run MetaTrader 4 and go to the Strategy Tester
- Specify your Expert Advisor in “Expert Advisor” option
- Select your currency pair, for example, EURUSD
- Select “Every tick” model
- Check “Use date” option
- Specify the date at your own discretion. Check the History Center and update your quotes, if necessary, to avoid mistakes.
- Set up a time period (timeframe).
- Check “Visual mode” option to see a progress of your Expert Advisor operation.
- Press “Start” button and wait for the end of testing process.
- Go to “Report” tab to view testing results.
Optimization of an Expert Advisor
Optimization represents consecutive tests of the same Expert Advisor with various input parameters on the same history data. At that, you can sort out such parameters, which provide the maximum efficiency of the Expert Advisor. The MetaTrader 4 trading terminal has in-built tools that allow automation of the process.
Check “Optimization” option to optimize the parameters. Set a range of values, within which the parameters you want to sort out can be changed, in the Expert Advisor properties, and press the “Start” button.
You will get lots and lots of results after optimization of your Expert Advisor in the Strategy Tester has finished. Afterwards the hardest part will come: you must decide, what result to choose, what kind of input parameters of the Expert Advisor are only a result of curve fitting, and what kind of them will yield profit in the course of real trading.
It easy to use a genetic algorithm in the process of optimization in the MetaTrader and use a balance as an optimized parameter in order to attempt to get the maximum profit from your Expert Advisor. This is not to say that the parameters, which have maximized profits, will guarantee that your Expert Advisor will be able to earn profit in the real market.
You need not only to get the best result, but select the most reliable result, which yields you profit in the future, among all other results during the optimization process. If you filter out testing results that don’t work definitely, it will be easy for you to accomplish this task.
I recommend you to follow the following simple rules that can help you to reduce the number of results of your Expert Advisor optimization.
- Number of trades must not be less than 300. More than 500 trades would be better.
- Profit factor (Gross profit/gross loss) must be more than 1.5. The more its value the better. Don’t forget about other points in the Strategy Tester report.
- If your Expert Advisor has been tested outside of an optimization period, then it must show results matching the results got in the course of the optimization. The first thing you should pay your attention to is a drawdown. The drawdown mustn’t exceed the drawdown over the period of optimization (more about it in the next point).
- The drawdown of your Expert Advisor must be equal to the value that your deposit can sustain. The drawdown of your Expert Advisor is your loss, which you can afford to have without stopping your trading. If you trade by your Expert Advisor on a real account and have a drawdown larger than that hit during tests, then the Expert Advisor must be reviewed and not be allowed to trade. In this case we can discuss about the value of the admissible drawdown for a long time. Let everyone decide by himself, what kind of losses he or she can sacrifice in case of failure.
- Pay attention to the input parameters of the Expert Advisor that have been optimized. Values of the variables got as a result of the optimization must be within reasonable limits and fit a basic idea behind the Expert Advisor.
These simple rules allow filtering out input parameters of the Expert Advisor that don’t work definitely on a real account.
Why does testing with the same input parameters show different results?
There are several options:
– If you use the genetic algorithm during optimization, then the optimization results will nearly always vary, at least, a bit.
– If the Strategy Tester is online, quotes can be uploaded into your History Center and thereby update and change your history data base.
– The Strategy Tester will inherit settings of the dealing center (spread size, swap, stop-level …), which account your trading terminal is currently connected to. For example, quotes have been downloaded from the “Metaquotes” data center, and testing has been performed at the moment, when your trading terminal has been connected to another dealing center’s account.
Finally, I want to share some more tips with you.
Never perform optimization of your Expert Advisor over all available history data. You should always leave a part of the data (about 10%) for out-of-sample tests. It is a very important rule you must follow during optimization. If your Expert Advisor operates for the out-of-sample period in a different way than for the optimization period, then feel free to discard these results of optimization, since they have been got as a result of the curve fitting only.
You should always examine optimizations results carefully. If you monitor how results of your Expert Advisor’s operation vary upon changing one of its input parameters, you can assess an influence of the parameter on the Expert Advisor’s performance. It would probably be better to exclude it from optimization.
Many argue that one can only curve-fit to the past data in the process of optimization and your Expert Advisor will never operate as well as during testing in the Strategy Tester. It is not true. When using historical data, you can always test your Expert Advisor properly and sort out such parameters that will work in the future in the real market. It is a complex task that requires a little more time than many people think, but it is possible.
The more times and the more comprehensively you test your Expert Advisor, the more you will learn about the trading system, and the more money you will make on it.