OnTick() EA Function Skips Ticks

 

Newbie query:

Can anyone help me with my understanding of the EA OnTick Function?

I have developed a simple custom indicator and I access this indicator from within a custom EA I have also developed. I am using the OnTick function to process each tick and assess aganist the indicator values to determine whether the tick price (bid/ask) has crossed a specific value as determined by the indicator. However, I was surprsied to note that when I run the EA against EUR/USD daily history, if the strategy tester is set to process data in fast mode, it skips a great many ticks (minutes or even hours) because the OnTick function simply is not called while it (OnTick) is still busy processing a previous tick. It appears that the Strategy Tester simply continues processing subsequent tick data, while the EA is busy processing its current tick. When OnTick has completed processing a tick, the strategy tester simply passes to the EA the current tick it happens to be processing itself, which is often many ticks later than the one the EA has just completed processing. This results in incorrect backtesting results as the trades as executed later than they should be.

A solution to the problem is simpy to slow down the Strategy Tester to a  speed whicj my local processor can handle, and that's fine. But what worries me is that there is no warning indicating that ticks are being dropped. Whilst that is a serious issue for backtesting, it is perhaps equally important when the EA is trading in a live environment, as the market may have moved quite significantly by the time an actual live trade is triggered, if the market is particulary active. I see also that MT5 gruops ticks together in very active market conditions, so it may not become an issue in the live trading mode.

Is there a way to ensure that the strategy tester does not skip ticks or at least warns a user when it does? Any help appreciated.

 
tbaronetti:

...

OnTick function simply is not called while it (OnTick) is still busy processing a previous tick.

...

Wrong, that never happens within the ST. The MT5 Strategy Tester don't skips any tick. It process ALL ticks provided by the chosen mode. If your EA is "tick" depending you need to use Every tick mode.

Of course if you set "Open prices only" or "1 minute OHLC" you will only get 1 or 4 ticks by candle respectively.

 
Alain Verleyen:

Wrong, that never happens within the ST. The MT5 Strategy Tester don't skips any tick. It process ALL ticks provided by the chosen mode. If your EA is "tick" depending you need to use Every tick mode.

Of course if you set "Open prices only" or "1 minute OHLC" you will only get 1 or 4 ticks by candle respectively.

Thank you for your prompt response, Alain. Firstly, just to be clear, I am not referring to testing an EA, but testing an indicator on historical data. My previous post was incorrect as it was the OnCalculate function I was referring to and not the onTick function. My mistake. However, as I was Printing tick prices to the ST Journal, it is possible that all ticks were indeed processed, but not all were written to the journal. I will continue testing as it may be an issue with my own MQL5 code.

 
tbaronetti:

Thank you for your prompt response, Alain. Firstly, just to be clear, I am not referring to testing an EA, but testing an indicator on historical data. My previous post was incorrect as it was the OnCalculate function I was referring to and not the onTick function. My mistake. However, as I was Printing tick prices to the ST Journal, it is possible that all ticks were indeed processed, but not all were written to the journal. I will continue testing as it may be an issue with my own MQL5 code.

If there is too much output to the logs they don't all appears in the Journal window. You have to open file (or the Viewer) to see the all the logs.
 
tbaronetti:

Newbie query:

Can anyone help me with my understanding of the EA OnTick Function?

I have developed a simple custom indicator and I access this indicator from within a custom EA I have also developed. I am using the OnTick function to process each tick and assess aganist the indicator values to determine whether the tick price (bid/ask) has crossed a specific value as determined by the indicator. However, I was surprsied to note that when I run the EA against EUR/USD daily history, if the strategy tester is set to process data in fast mode, it skips a great many ticks (minutes or even hours) because the OnTick function simply is not called while it (OnTick) is still busy processing a previous tick. It appears that the Strategy Tester simply continues processing subsequent tick data, while the EA is busy processing its current tick. When OnTick has completed processing a tick, the strategy tester simply passes to the EA the current tick it happens to be processing itself, which is often many ticks later than the one the EA has just completed processing. This results in incorrect backtesting results as the trades as executed later than they should be.

A solution to the problem is simpy to slow down the Strategy Tester to a  speed whicj my local processor can handle, and that's fine. But what worries me is that there is no warning indicating that ticks are being dropped. Whilst that is a serious issue for backtesting, it is perhaps equally important when the EA is trading in a live environment, as the market may have moved quite significantly by the time an actual live trade is triggered, if the market is particulary active. I see also that MT5 gruops ticks together in very active market conditions, so it may not become an issue in the live trading mode.

Is there a way to ensure that the strategy tester does not skip ticks or at least warns a user when it does? Any help appreciated.

better make manual not auto like ontick , using manual function would be less cpu cost

if you use onTick, all tick on currencies that you open will trigger OnTick

 
Alain Verleyen #:

Wrong, that never happens within the ST. The MT5 Strategy Tester don't skips any tick. It process ALL ticks provided by the chosen mode. If your EA is "tick" depending you need to use Every tick mode.

Of course if you set "Open prices only" or "1 minute OHLC" you will only get 1 or 4 ticks by candle respectively.

Hello Alain,

i'm encountering the same problem but in the Main windows when i'm testing my trigger conditions. since some OnTicks which should trigger a signal to enter is not being triggered sometimes. I'm not sure if this happens when market is moving too fast or whatever. Is there a way to do more granular than OnTick to ensure prices are all processed and captured? 

btw, i'm using a _Period of 15 min. on my Chart view.  But this should not affect right? since OnTick should process every tick. (again, on the Main window which sends me Alert when trigger is detected, not on ST)

thanks.

Jim

 
JimSingadventure #:

Hello Alain,

i'm encountering the same problem but in the Main windows when i'm testing my trigger conditions. since some OnTicks which should trigger a signal to enter is not being triggered sometimes. I'm not sure if this happens when market is moving too fast or whatever. Is there a way to do more granular than OnTick to ensure prices are all processed and captured? 

btw, i'm using a _Period of 15 min. on my Chart view.  But this should not affect right? since OnTick should process every tick. (again, on the Main window which sends me Alert when trigger is detected, not on ST)

thanks.

Jim

In tester there is no skipping of ticks. On live chart it can skip ticks if Ontick handler is running while a new tick comes in.

 
Enrique Dangeroux #:

In tester there is no skipping of ticks. On live chart it can skip ticks if Ontick handler is running while a new tick comes in.

Thanks Enrique for quick reply. 

you mean in live trading, it skips? is there a way around this problem?

does this mean in live chart trading, it is better not to have a single EA monitor more several different chart instruments?

i mean, we need to create separate EAs to correspond to each chart to minimize OnTick handler becoming too busy? Does it work that way?

 

Depending on the architecture design of the EA there is a work around.

I use this

   TickCount_ = CopyTicksRange(_Symbol,Ticks_,COPY_TICKS_ALL,LastTick_.time_msc+1,LONG_MAX); 

   if (TickCount_==0) 
   {
     return;
   }
   LastTick_ = Ticks_[TickCount_-1];  

   For (int i=0;

The ticks are processed further just like OnTick would with a loop over Ticks_ array.

Needless to say, you need to optimize your code in order to return from OnTick as fast as possible, but still you will miss


You can see here that on average (100 period EMA) it takes 21,2 μs to returm from OnTick for 12 algorythms. It is pretty fast, still you can see it is recorded that there where 14 ticks incomming while OnTick is running. Also note that windows is not a real time system and windows processes can interfere with the execution of threads. 

 
Enrique Dangeroux #:

Depending on the architecture design of the EA there is a work around.

I use this

The ticks are processed further just like OnTick would with a loop over Ticks_ array.

Needless to say, you need to optimize your code in order to return from OnTick as fast as possible, but still you will miss


You can see here that on average (100 period EMA) it takes 21,2 μs to returm from OnTick for 12 algorythms. It is pretty fast, still you can see it is recorded that there where 14 ticks incomming while OnTick is running. Also note that windows is not a real time system and windows processes can interfere with the execution of threads. 

 my coding understanding is lacking so i still need time to learn in order to understand what you suggested. anyway, appreciate your replies.

 
Alain Verleyen #:

Wrong, that never happens within the ST. The MT5 Strategy Tester don't skips any tick. It process ALL ticks provided by the chosen mode. If your EA is "tick" depending you need to use Every tick mode.

Of course if you set "Open prices only" or "1 minute OHLC" you will only get 1 or 4 ticks by candle respectively.

Do you know how to hide ticks from OnTick() or code that prevents trades from occurring expect during open prices only? I used multiple methods but none replicated the results from an open prices

only backtest on real ticks and open prices only itself during another back test. Unless the stop loss and take profit is set when the order is sent during the open prices back test the results are not profitable.

I am stumped what am I missing?

Reason: