This is a short guide on how to properly backtest EAs on Metatrader4 using Tick Data and price based charts such as RangeBars, MedianRenko, Renko and PointO.
One of the most common mistakes when backtesting strategies on price based charts is not understanding how these very specific charts work and thus incorrectly performing backtests, which may lead to frustration when later finding out that actual trading results are not even close to what one may expect after witnessing a flawless backtest – even using Tick Data and obtaining a 99% modeling quality reading in MT4.
First of all, you should know that constant range price charts such as RangeBars, Median Renko, Renko and PointO are all modeled charts, which in general means that the Open & Close prices for a vast number of bars are virtual. They are modeled to obtain a chart where individual bars have the same ranges (RangeBars) or the same body sizes (Median Renko, Renko and PointO).
Such modeling is not a bad thing! It is what makes these price charts easier to trade, but on the other hand harder to backtest correctly using MT4’s backtester.
For example, Median Renko charts have the ability to smooth out indicator readings, which in return gives us clearer signals and less noise on the charts. However when backtesting such charts one should know that the Open price of every bar is virtual (a single tick that almost never existed), because it is shifted back to the middle of the previous bar’s body. However, the next tick is real and this is where EAs would place trades when live trading. Therefore, live trading will not be influenced in a negative way (even when using an EA). Backtesting MedianRenko charts is a different story – when done improperly, will give a false backtest. That is because EAs will have no problems opening trades using the virtual open price value of a new bar.
Here are two examples of a simple stop & reverse strategy backtest performed on a 20 pip Median Renko chart of EURUSD (01/2012 – 11/2012):
Notice the first backtest was performed using high quality TickData. However, the EA did not take into consideration the specific design of MedianRenko charts.
This second backtest was done correctly. It also utilizes the same strategy, same charts, same tick data, same signals and the same number of trades, but this time trade execution was aware of the specific design of MedianRenko charts.
What happened? Where does the difference in performance come from?
Let’s take a closer look at the charts from both backtests. As you can see below, the charts are the same and the same number of trades got executed (454 is the last ticket number)
On this chart, trades were taken at non-existing prices (the virtual open price of a MedianRenko bar). This gives us an extra 10 pips on all profitable trades and a 10 pip smaller loss on all losing trades.
Here trades were executed at real market prices (not the virtual open prices), which gives us results closer to the real deal*.
*) Live trades would only differ by any possible slippage in trade execution at the broker’s side.
Same principle applies to backtesting Renko, Range bars & Point Original charts.
OK, so what do you need to correctly perform backtesting on custom price based charts?
- Good quality tick data, which you can download from various sources in CVS format.
- A professional price based charting plug-in for MT4 (such as RangeBars, Median Renko, Renko or PointO)
- A script to convert tick data into MT4’s data format using the corresponding price based charting plug-in (see individual plug-in pages: RangeBars, Median Renko, Renko, PointO) for details).
- Tools to utilize tick data in MT4’s backtester.
- An MQL code snippet, which will enable you to correctly backtest your strategy on price based charts.
The CustomChartingBacktest.mqh code snippet is part of the PRO versions of RangeBars, Renko, Median Renko & PointO plug-ins. It is already placed in the correct folder of your MT4 installation when you install any one of the plug-ins mentioned above.
You need to add the following two lines of code at the beginning of your EA source file. Somewhere at the top of the file include the code snippet into your EA by calling:
and at the very top of the void OnTick() function add the following function call: