CFDs are complex instruments and come with a high risk of losing money rapidly due to leverage. 64 % of retail investor accounts lose money when trading CFDs with this provider. You should consider whether you understand how CFDs work and whether you can afford to take the high risk of losing your money.

Accuracy of MT4 backtests

I don’t believe in backtesting results of MT4 and MT5, even if I don’t have experience with MT5. There are too many differences to real account trading like handling TP and SL (demo accounts are also different here because there is no slippage) that IMO the result is not reliable for the testing period- how could it then be an indication for the future?

1 Like

It really matters on timeframe you test. If its M15 then differences might be huge. If you do H4 or D1, then result is pretty close to reality. maximum 10-15% deviation on H4 (if tested asset has proper liquidity)


I am sure that if you tune by backtest parameters of a strategy to obtain the worst result as possible that the strategy will not work in the future…

1 Like

It also depends on the strategy and on programming.
For example time related functions are unfortunately not available in the backtester. That’s nearly hidden in the documentation. I have written an EA which does not trade in the startegy tester because of that. Backtesting is not possible, only forward testing on a demo account.

If you’re trading assets with a break overnight or over the weekend with pending orders and the market opens outside your TP, the backtester will give you a trade with full profit (same is with demo accounts) when in reality you would get a losing trade. You can avoid that by defining a maximum slippage value on the order and it will not trade.

These are just two examples for a completely different behaviour - always in favour for the trader while he is testing and he will wake up when he tries the same in reality.

FYI actually for mt4 you can call ontimer() from inside ontick() conditionally at certain times or if certain amount of time has passed as a workaround to get ontimer() to work inside tester.

Not the case if using TDS with correct settings as everyone doing mt4 tests should be. Also (max) slippage variable in an order is ignored by ECN brokers.

1 Like

I replaced ONTIMER with ONTICK and it did not work. I will try it again with ONTICK as wrapper.

The TimeCurrent() function is also used, maybe there is also a problem with the tester.

TimeCurrent() works fine in tester. I had the same problem you described when I based my entire EA on the timer and solved it by defining a small ontick function like bellow.

(istester is set elsewhere and is only true if running in tester.
FREQUENCY is the timer frequency in seconds.)

void OnTick()
      static datetime lasttick;
      datetime curtime=TimeCurrent();

Now it works in tester.


Thx. :pray: I will try that OnTester Routine.

I did’n catch this issue.

May be I am wrong, but if I understand well : an open or close action is done at a price witch never existed because of a gap ? There is no such a bug in MT4.

There a many bugs in MT4 but not this one. It looks like a very old bug many years agos with TradeStation (may be am I wrong…), where a favorable end in a candle was choosen by the tester despite the possible stop reached inside the candle.

1 Like

I had that after Christmas on one or two European indices on a demo account. This index CFD was closed earlier than other markets so these orders couldn’t be deleted in time and the market opened after Christmas with a higher gap. If I find the order again tonight, I can make a screenshot of it from the history record.
The pending order is not visible any more, unfortunately.


To be clear, OnTester() is fired only once at the end of a test. Did you mean to say OnTick Routine?

1 Like

What do you want to acheive with time functions?
Closing a trade after a certain amount o time?

I meant the istester in the ONTICK routine

OK istester is defined globally and this is in the OnInit():


He was using OnTimer() but that doesn’t work in MT4 tester without my suggested workaround.

I use OnTimer() instead of OnTick() to reduce CPU usage and test time. It also makes more sense for multi-symbol strategies in case there are no ticks in the market for which you have place the strategy on.


No really that point, but closing a trade before the market closes and as @bendex pointed out, the main routine works under the OnTimer() function

Unfortunately I don’t have the login for that trading account here :frowning:so we have to wait for the screenshot until the weekend

Thank you so much for your help. It saved hours for me:

We don’t need to wait for the weekend, I have one from backtesting over a opening gap which was opened after market reopened with an unrealistic price:
with great profit:

but only virtual, as this price was never met at this time:

The low of the bar is higher than 50 points over the entry price. That’s 89 EUR profit which cannot be made in reality.

edit: This is the original order # 143 I forgot to document here:

Also the take profit of the result calculation is outside the real traded range.

That helps to understand that backtesting is so nice and the reality so cruel :wink:


There is a problem with the date but not in the price (inside the previous candle):

Your description is not complete imho :
Model tester (ticks, open price, control point) ?
TimeFrame (we saw your M15 candle description, but…) ?
MultiTimeFrame ? IE: asking data from H1 when backtesting M15… ?

Also there are few best practice using MT4 tester (consequently in real too), after a long history of bugs :
Dont call MultiTimeFrame data : if you are in M15 and you want to compute 20 days daily volatility, check new days inside your M15 candles.
Dont use BAR[0] (except handling ticks). If you want to play inside BAR[0], maybe you are not in the right TF regarding the logic of the strategy.

Now I remember an old bug of the tester : using Close[0] in backtest rather than Bid.
It is ok in real (or demo) : Close[0] equal Bid.
But with the tester, MT4 gave the exact Close[0] of the candle history : a data from the future.
It take time to undestand why this EA was so efficient in backtest : using time machine. :smile:

I dont care if this bug is actualy fix. Just a few best pratice need not to be changed.

I dont know why the tester gave you the time of the next candle. Neither why it execute just at the end of a daily candle (buy stop price is reached before).

Got it : you placed an order immediatly executed !


Model is ticks, and time frame was 15 minutes ( besides time frame is Not relevant here).

It also happens on a demo account, not only in the tester.

The same appeared this morning when close price is hanging in the gap, I can upload a nice screenshot tonight from tester.