Home


Optimisation & Backtesting

    MetaTrader includes a 'Strategy Tester' in MT4 and in MT5. They work slightly differently because with MT5 you can pay to use use 'Agents' which are basically computers in the cloud which share the load and so greatly speed things up. However, backtesting on the Daily charts set to every day of the week can easily be done on your own computer' However, on a weak VPS it will run much slower and you may even get 'out of memory' messages and have to restart your MT5. On a good VPS it is not a problem but you will be far better off running it from the desktop of your own computer as even an old, modest one will be a lot better than a VPS.

    My experience has been that even experts do not know how to run backtesting properly! For example, I know an EA seller who has 4 high powered computers (he uses MT4) running backtests 24 hours a day - yes really. But I had to point out to him that looking for the highest profits was not the best thing to do. After all, which is better - $1,000 profit with $800 drawdown or $500 profit with $200 drawdown? Obviously the later because you can use 2.5 times the Lot size and 2.5 times $500 is more than £1,000.

     Just as important is the smoothness of the profit graph. It's no good having huge gains one month and losses the nest - if you start running the EA with those settings and hit a bad month right away you will stop trading! We need a consistent return, week after week (even if it is less). The last factor to consider is the duration of the backtest. I'm writing at the beginning of April 2024 and if I run a backtest from January to the end of March I can get fabulous results - but run those settings on the last 3 months of 2023 and I get losses because conditions changed. The solution is to run a 6 month backtest from October to the end of March. With difficulty, we can find settings that steadily profit for the whole period (but less profit for the last 3 months).

    So, the bottom line is we must look for a good profit to drawdown ratio, a smooth profit graph and steady growth even under different conditions. Optimise for fantastic profits over the last two months and you may end up with losses when you start live trading. Also, psychologically, it is better to restrict the number of consecutive losses to two or preferably just one. If you hit 3 losses in a row, you will probably stop trading those settings from fear of a fourth loss! This is one reason it is better to use a large Stop Loss. You will be much happier with one 40 pip loss than four ten pip losses.

    Strategy Tester is accessed from the View menu and opens up at the bottom. Drag the window up to make it as big as possible. It should open up with the 'Overview' tab - select 'Genetic Optimisation' which will take you to the Settings tab. Use H1 and select your currency pair. if it doesn't appear, open up the Market Watch window, Right Click inside it and 'Show all'. Close Strategy Tester, restart MT5 and start again. Let's do a test run of the USD/CHF (which I know is not very good) and set the Date to Custom Period, 1st October 2023 to 1st April 2024. We will use $25,000 capital and 1:50 leverage (not really important) and Modelling 'Every tick'. Of course, we must select RichLazyTrendEA at the top!

    This was written when we used H1 and separate days, so now it is much simpler because all the days can be set to True when using D1.

   Note that Genetic Optimisation limits you to 1280 runs so if your test exceeds this and you want to check all the possibilities, select Full Optimisation. However, D1 testing rarely needs even 1,000 runs.

   Now we need to go to the Inputs tab and input suitable starting parameters. I suggest using the ones below and best to save them as TestStart so you can use them again very easily - but be careful, in MT5 saved files also set the instrument and other parameters. So, you may have chosen EUR/GBP and loaded the Test Start only to discover it has changed it to USD/CHF! Note that we have chosen to permutate all the days of the week, which is 2x2x2x2x2x2 or 64 times more test runs than if we had just allowed all the days. You can see the total runs needed is 3,200.

   Of course, it's not necessary to select all the days of the week. You may only be looking for one day or a few specific days or even any days except a certain one...

    Press Start bottom right and on my slow VPS it tells us that it will take about 9 minutes  (after downloading all the data and doing one run). Note that the 'Genetic' optimisation uses intelligent guesswork to reduce the number of passes usually to 1,280 if the real number is large. In fact, it finished in a couple of minutes and should open the Results tab and we see this.

   We are only interested in the results with lowest drawdown - in this case the ones around 5%. You can see we have wildly different results for the top few places (typical of unsuitable pairs). Next we would Right Click on the ones we want to examine further and 'Run Single Test' to see how smooth the profit graph is. Note that the top one has a whopping over $5,000 profit but a very small number of trades and just on Wednesday! In fact, the graph is not to bad and after checking the others, I would choose that one. You should also check the percentage win rate which ideally should be 80% or higher.

    We note that the TP is right at the high end so suspect it could be even higher. The 'Run Single Test' will have set the inputs as shown but now we need to optimise even further. Days of the week can stay at just Wednesday - so untick all the Days of the Week. The Length is off screen at 12 but remember the 'Step' was set at 2 - so we must click the Genetic Optimisation again (it was changed to Single Run) and then change the Length range from 11 to 13 with a Step of 1. Change the SL from 27 to 34 with a step of 1 and the TP to 29 to 34 again with a step of 1. This will give us our final settings and a reduced run of only 48.

    In fact, I messed this up because i had the trading times set from 10 to 14 but in fact ended up with a very good result of £7,450 profit using a 34TP and a 29SL. Now, to allow for variables, I would change this to a 33TP and a 30SL to make it less 'on the limit' and more likely to succeed in the real world. Lastly, we would make sure our times are optimum by doing another set of runs with just the time as a variable. For example Start time of 8 to 11 and and end time of 12 to 18. Doing this gave me another $1,000 profit trading from 9 to 14, so pretty good results from a generally bad pair.

    However, with only 21 trades in 6 months (21 out of a possible 26) does not inspire great confidence because of the low number. Better to either have more days or more than one trade a day so we have a large enough number to be more meaningful.

   Be aware than volatile pairs such as the GBP/AUD will need larger SL and TO (maybe 20 to 40 or 45) and commodities and particularly Indices may need different TPs and SLs altogether - for example, 2000 to 6000). Backtesting is an art acquired through experience but always remember we need a smooth graph and low drawdown. For Prop trading this needs to be around 5% or preferable 3% and for normal trading around 10% or less. Ideally, you need to find two good unrelated pairs to trade every day of the week. This spreads the risk but you also need to keep the Lot size down a bit to reduce the pain from two losses on the same day!

    Of course, you can check different times, for example 17 to 20 in the US session but after most news releases can be good but sometimes these stay open overnight and even all the next day, so be careful. Also, from 2- 5 in the Asian session may be productive but this will intrude on the usual 9 - 13 trades.

    Once you have found a good setting, click on the Settings tab and again on the Settings icon (a gear) next to the IDE button) and Save to your desktop for use in your real trading. The IDE button is only for use with source files to access the code.

 


.