Are OnTick and OnTimer really not conflicting in the queue???

 

Dear people,

There is a lot of "literature" in the forum about this, one above all the clarification offered by William Roeder here: https://www.mql5.com/en/forum/321335.

Thus, please, can anybody explain this?

//+----------------------------------------------------------------------------+
//|                                                   TestConcurrentEvents.mq5 |                                          
//| Simple EA for testing concurrent events.                                   |
//| This example demonstrates that NATIVE MQL5 is NOT able to concurrently     |
//| handle two events incoming at the same time.                               |
//| By outcommenting the Sleep(1000) statement, the EA will produce, on a      |
//| backtest operated on 1M OHLC, a regular sequence of Tick events (every 20  |
//| seconds) and a regular sequence of Trade events, one every 5 minutes.      |
//| But as soon as the OnTicke event spends 1000 ms in making something (in    |
//| this case sleeping), the OnTimer event generated in the meanwhile is       |
//| completely lost, besides a few cases in which the "sleeping" is a bit      |
//| faster or, likely, the Timer event is generated some ms before the         |
//| generation of the Tick event.                                              |   
//+----------------------------------------------------------------------------+

int OnInit()
{
   EventSetTimer(300);
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   EventKillTimer();   
}

void OnTick()
{
    printf("Tick");
    Sleep(1000);
   
}

void OnTimer()
{
    printf("Timer");  
}

Log file generated without the Sleep(1000) statement: regular call of OnTimer every five minutes:

        2010.01.04 00:04:59   Tick
QP      0       15:47:27.116    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:05:00   Tick
KQ      0       15:47:27.116    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:05:00   Timer
HK      0       15:47:27.133    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:05:20   Tick
MQ      0       15:47:27.150    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:05:40   Tick
EO      0       15:47:27.167    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:05:59   Tick
ND      0       15:47:27.183    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:06:00   Tick
HR      0       15:47:27.200    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:06:20   Tick
KH      0       15:47:27.216    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:06:40   Tick
GF      0       15:47:27.234    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:06:59   Tick
IS      0       15:47:27.251    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:07:00   Tick
HI      0       15:47:27.267    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:07:20   Tick
HG      0       15:47:27.283    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:07:40   Tick
EM      0       15:47:27.301    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:07:59   Tick
MJ      0       15:47:27.317    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:08:00   Tick
MP      0       15:47:27.333    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:08:20   Tick
PN      0       15:47:27.349    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:08:40   Tick
RD      0       15:47:27.365    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:08:59   Tick
JQ      0       15:47:27.382    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:09:00   Tick
MO      0       15:47:27.398    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:09:20   Tick
EE      0       15:47:27.415    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:09:40   Tick
KS      0       15:47:27.431    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:09:59   Tick
QH      0       15:47:27.448    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:10:00   Tick
KI      0       15:47:27.448    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:10:00   Timer
KL      0       15:47:27.464    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:10:20   Tick
DJ      0       15:47:27.481    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:10:40   Tick

Log file generated on backtester 1M OHLC with Sleep(1000) activated (I screened for finding one instance in which, by chance, the Timer handler is activated:

        2010.01.04 00:24:40   Tick
ON      0       15:23:03.021    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:24:59   Tick
RK      0       15:23:03.021    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:25:00   Timer
HQ      0       15:23:03.033    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:25:01   Tick
CO      0       15:23:03.046    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:25:20   Tick
ME      0       15:23:03.046    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:25:40   Tick
JS      0       15:23:03.056    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:25:59   Tick
JH      0       15:23:03.066    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:26:00   Tick
CF      0       15:23:03.076    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:26:20   Tick
GL      0       15:23:03.087    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:26:40   Tick
JJ      0       15:23:03.088    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:26:59   Tick
KG      0       15:23:03.097    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:27:00   Tick
DM      0       15:23:03.100    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:27:20   Tick
PK      0       15:23:03.106    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:27:40   Tick
HQ      0       15:23:03.113    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:27:59   Tick
CN      0       15:23:03.119    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:28:00   Tick
MD      0       15:23:03.126    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:28:20   Tick
HR      0       15:23:03.132    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:28:40   Tick
FH      0       15:23:03.138    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:28:59   Tick
OE      0       15:23:03.141    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:29:00   Tick
QS      0       15:23:03.145    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:29:20   Tick
CI      0       15:23:03.150    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:29:40   Tick
KG      0       15:23:03.154    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:29:59   Tick
PL      0       15:23:03.159    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:30:00   Tick
HJ      0       15:23:03.164    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:30:20   Tick
FP      0       15:23:03.168    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:30:40   Tick
HN      0       15:23:03.173    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:30:59   Tick
RK      0       15:23:03.178    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:31:00   Tick
OQ      0       15:23:03.182    TestConcurrentEvents (EURGBP_0s,D1)     2010.01.04 00:31:20   Tick

Any explanation for this? How to solve it?

In my hands it becomes now impossible to manage properly the two event handlers in a unique EA, where I wanted to assign precise functions of a trading strategy to either one or the other handlers.

Thank you so much in advance.

What happens if OnTimer triggers while OnTick is running, or vice versa?
What happens if OnTimer triggers while OnTick is running, or vice versa?
  • 2019.09.01
  • www.mql5.com
In OnTick documentation, it says a new tick is ignored if OnTick is still running. Is OnTimer ignored too...
 
Fab:

Dear people,

There is a lot of "literature" in the forum about this, one above all the clarification offered by William Roeder here: https://www.mql5.com/en/forum/321335.

Thus, please, can anybody explain this?

Log file generated without the Sleep(1000) statement: regular call of OnTimer every five minutes:

Log file generated on backtester 1M OHLC with Sleep(1000) activated (I screened for finding one instance in which, by chance, the Timer handler is activated:

Any explanation for this? How to solve it?

In my hands it becomes now impossible to manage properly the two event handlers in a unique EA, where I wanted to assign precise functions of a trading strategy to either one or the other handlers.

Thank you so much in advance.

Seems like a bug using Sleep() and a timer with the Strategy Tester.

If you don't use Sleep() there is not such problem.

 
Alain Verleyen #:

Seems like a bug using Sleep() and a timer with the Strategy Tester.

If you don't use Sleep() there is not such problem.

Thank you. Very useful.

Reason: