Let's trade using computer science!


How to backtest & optimize in MT4




Just like me, many of you are using the MetaTrader platform for daily trading using Experts Advisors. However, not many people know how to backtest in MT4 properly. In this short blog I will try to explain you the logic behind the EA optimization process on a MetaTrader 4 or 5 platform. If you do not know how to run the strategy tester on your MT4 platform then there are many places to go like YouTube to learn from. This article explains only the optimizing steps needed to get the best results during the optimization.

So here we go:

  1. How to approach mt4 strategy tester optimization
  2. Is 99% tick data accuracy one big lie?
  3. What level of tick data accuracy is needed for backtesting?
  4. Putting it all together

1. How to approach mt4 strategy tester optimization?

I will explain it on a simple example. Imagine we have a given trading system with the following trading results.
Backtesting in mt4
Now imagine today is the point where we have drawn a red vertical line. Of course we can not predict the future and the only thing that we can see are the backtested results from the past. So how can we make sure the optimized EA results are not curve-fitted and the optimized EA parameters have any chances of being profitable in the future? Let’s split our optimization process in two steps.
Strategy optimization steps
STEP 1:
Optimize a given trading strategy on a small historical data fragment, from a given date in the past (Start) until today (End). It is extremely important that the number of trades in the selected period is representative for the given trading system. You need to optimize using  at least 10% of the total number of trades which have occurred within history data range.

STEP 2:
Test if the optimized parameters found in STEP 1 are not curve-fitted! Yes, this is my big secret magic trick, that I use in my all trading systems, so read and think about the following sentence for a while, let it sink in:

If you can predict the past = most probably you will be able to predict the future!

Basically, if I see nice results on a backtest using history data which was never used during the optimization (so called out-of-sample period), then it gives me confidence, that selected set of parameters could also be profitable in the feature.

2. The 99% tick data accuracy is one big lie!

In my early days when I was just starting my adventure in automated trading, everybody was saying (and still is) “You need to use 99% tick data to backtest properly, 90% is garbage etc..etc…”. I was like: ohh man..I need always to have 99% modelling quality! I was obsessed by tick data accuracy. I spent hundreds of dollars on tools which gave me 99% accurate tick data….but then I’ve discovered the following:

The 99% accuracy visible after a backtest is
just a number in the FXT file header!
tick data modeling accuracy
And moreover what does it mean to have 99% of accuracy? The answer is: it doesn’t mean anything! It is just representation of tick modeling quality for the broker where the tick data comes from and in almost all cases it is Ducascopy tick data.
Furthermore in most cases people will use different broker with different tick prices, spread, latency etc…So 99% backtest data modeling is a myth, but 99% data is not entirely useless, read the flowing point.

3. What level of tick data accuracy is needed for backtesting?

There is one good answer: in best-case scenario you want to use the real broker live account data ticks. However this is only possible if:
  • You are using MT5 platform and run your optimization using: “Every tick based on real ticks” mode. (Even then, most brokers provide slightly “adjusted” tick data, so no, they will not let you win that easy, hehe)
  • Your broker will provide you the real recorded tickdata from a live account. (It does not happen a lot)
  • You know how to record live tick data by your self and know how to export it to strategy tester. (Too difficult for most people)
If you can not use one of above mentioned options, then a simple alternative, is to simply test your history tick data for accuracy. The key word is: results correlation.

In order to test your historical data for accuracy just run the following simple test:
  1. Run your (or any other) EA on a live account on a selected broker. To minimize the potential losses minimize the lotsizes of open positions.
  2. Collect the live trading results for at least few days (or even weeks) in order to capture different market moods and events.
  3. After that, backtest the same EA using exactly the same EA settings and using the same time period! (In this step you can use 99% modeling or the tick data you currently have)
  4. See how big are the differences in trading results between live and backtested data.
  5. If the difference in all critical parameters such as: number of trades (very important), drawdown and profit etc..are not that big, then your history tick data is sufficient to be used in optimization and backtesting. If the differences are significant, then you need to test it on a different broker or using different source of tick data.

4. Putting it all together

Trading strategy optimization and accurate result prediction is not an easy task, however it can really be achieved with a little bit of effort and practice. In order to achieve some level of confidence in your backtest, you need to make sure:
  1. Your EA settings are not curve fitted
  2. Trading results are also profitable outside your optimization period (in the past or the future)
  3. There is a good correlation between results from backtesting and optimization and the your live account
*** Bonus tip: The most important characteristic I’m using during selection of the best EA settings is the trading curve-shape! Ideally I want to see a straight line in upwards direction with as many trades as possible. If resulting trading curve has many dips (DrawDowns), then it means the strategy is risky since theoretically you could start your live trading precisely on a top before an upcoming DrawDown period and so blow up your account before making any profit. See a simple comparison between two different results below:


Greets,
Chris

P.S.: do not forget to like or share this article!

 

8 Responses to How to backtest in MT4

  • Tried several times to use an EA, but always met with bad eeather. May be my poor knowledge or it was just a phenomenon. Any way lost confidence.

  • I think you got something there that might help the dump money traders .Keep up the Good Work. Daniel .Cheers

  • Your graphs in section “Bonus tip” resemble what I use to test. First phase I look at Graphs for downdraw locate DD events and then go roughly 1 month before when input time period to test to test for worst case scenarios … this can require several hours to test when going back years and looking for DD events and Second phase is once I identify dates of DD events I utilize Strategy Tester to change inputs to handle difficult event and then examine results for reasonableness to determine if I want to use EA or abandon it if it doesn’t provide good return on my investment. If I do find useable results then “hopefully” they’ll provide a good hedge against future events and yet still provide a decent relatively safe profit. (never know about future events because they’re inherently “unforeseeable” like Chf event awhile back … many traders/brokerages went under because of those a**holes not announcing what they were going to do … and I’m sure a select few benefited). I do this with a few currency pairs EA was supposedly designed for and evaluate.

    I try to pick a time period where balance might not cover DD to change inputs or change balance to test feasibility study on EA and sensitivity to various inputs and balances.

    I use TDS and am aware of the header … they showed me how to change the text in an older version and it was interesting … can make it 100% if I want to … I did change to 99.99% to show a friend. Even if you change TSD options such as “slippage” and try to account for latency and use Ducascopy data there can be significant differences between “virtual” simulation and reality but I still consider it a useful tool and closer to reality to test visually the way the EA works and gain better insight into the logic of the EA to decide if it has good potential or not using my years of experience (good and bad) as a guide.

    • Thanks for sharing your feedback, yes I am also familiar with the work of our mutual friend Birt 😉 But some time ago I’ve switched to TickStory and then to tick data recording and now almost only MT5 which comes as close as it get to simulating with “real ticks”.

  • Hi,
    How can I baccktest CoensioRecoveryEaV03F?. I tried but it does not backtestest.
    Regards.

Leave a Reply to Javier Cancel reply

Your email address will not be published. Required fields are marked *

Short about me

Welcome to my Blog!

My name is Chris, I'm a Dutch engineer with Polish roots. By day I'm an engineer, forex trader and an internet entrepreneur, and by night I'm fighting internet crime;) I hope you will enjoy my website and my free MT4 software. Do not forget to check out my blog.
B.t.w: 'coensio' (Latin) = assessing, assessment, census, estimating, opinion, rating, taxing.

Ads
ArrowHunterEA project
My StrategyQuant Portfolio
StrategyQuant strategy builder:
Follow me on Facebook!

My newsletter

Subscribe to my newsletter

Ads