OnTimer() has a very large delay. How to solve it? - page 5

 
Longsen Chen #:

Yes. I can use TickCount() to replace TimeLocal() so we can get the delay in millisecond.

When we have delay in millisecond, we can get minimum interval to see if sometimes the scheduler executes sooner.

I can do the same test for MT5, in the same environment as a comparison.

Maybe at the end of this investigation, we have no solution as Windows is a preemptive system. But we can consider Linux.

Linux is as well preemptive, no change. But on linux we can chose the kernel scheduler algorithm. - Might be more responsive under load. But still, its no guarantee.

The problem is, you only know after the delay happend, so there is no way to take action to prevent this from happening.

Actually I tried to use a service to get better results for triggering the timer. - So I had a service running, monitoring time in  a loop, and sending a custom event to the chart. There had been an EA waiting and listening for that event. - It seemed to me even more unreliable, because the event queue is depending on the amount of indicators on the target chart... difficult....

 

We can't control the event queue. The triggering event from the service will be put into the queue, it will be delayed if the other events take long time.

I'm familiar with Linux kernel mode. Linux is much better than Windows in term of real time processing. I can't switch to Linux right now because I have no MT4 and MT5 API.

 

I have made two tests, MT4 and MT5 in the same computer with 10 terminals (8 MT4 and 2 MT5). Here are reports and codes.

#property version   "1.0"
#property copyright "Copyright 2022, Ksana Trading Corp."
#property link      "https://www.ksanatrading.com"

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   if(!EventSetTimer(1))
      return (INIT_FAILED);
   string msg;
   msg = "Monitoring timer delay...";
   Comment(msg);
   Print(msg);
//--- succeed
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   EventKillTimer();   
}
//+------------------------------------------------------------------+

void OnTimer()
{
   static ulong last_call=0;
   static int gap_max=0;
   static int gap_min=INT_MAX;
   static ulong count=0;
   static ulong first_tick=0;
   ulong now = GetTickCount();
   string msg;
   if(first_tick==0)
      first_tick=now;
   if(last_call != now)
   {
      if(last_call>0)
      {
         int gap = (int)(now-last_call);
         bool new_value=false;
         if(gap>gap_max)
         {
            gap_max=gap;
            new_value=true;
         }
         if(gap<gap_min)
         {
            gap_min=gap;
            new_value=true;
         }
         if(new_value)
         {
            msg = StringFormat("Timer interval (Max=%.3f Min=%.3f) seconds, count=%d elapsed %.3f seconds",
                     ((double)gap_max)/1000,
                     ((double)gap_min)/1000,
                     count,
                     ((double)(now-first_tick))/1000);
            Comment(msg);
            Print(msg);
         }
      }
      last_call = now;
   }

   count++;

}

MT4 reports

MT4 journal log:

2023.07.31 20:09:40.716 MQL5.chats: activated for 'gchen2101'

2023.07.31 20:09:40.622 MQL5.community: activated for 'gchen2101', balance: 139.86 (frozen: 125.00)

2023.07.31 20:09:39.029 '1900082876': previous successful authorization performed from 152.32.151.198

2023.07.31 20:09:38.576 '1900082876': login datacenter on ICMarketsSC-Live19 through DC_Vir_L19 (ping: 7.14 ms)

2023.07.31 20:09:38.076 '1900082876': login on ICMarketsSC-Live19 through DC_Vir_L19 (ping: 7.14 ms)

2023.07.31 20:09:33.794 Expert Timer USDJPY,H1: loaded successfully

2023.07.31 20:09:23.013 Data Folder: C:\Users\Administrator\Desktop\Work\MT4\MT4.8

2023.07.31 20:09:23.013 Windows Server 2016 Datacenter x64, IE 11, RDP, UAC, 4 x Common KVM processor, Memory: 5744 / 8191 Mb, Disk: 11 / 39 Gb, GMT+8

2023.07.31 20:09:23.013 Doo Prime MT4 Terminal build 1382 started (Doo Prime Limited)

MT4 Experts log:

2023.07.31 20:33:45.189 Timer USDJPY,H1: Timer interval (Max=1.375 Min=0.656) seconds, count=1436 elapsed 1443.594 seconds

2023.07.31 20:33:44.529 Timer USDJPY,H1: Timer interval (Max=1.375 Min=0.750) seconds, count=1435 elapsed 1442.938 seconds

2023.07.31 20:23:45.084 Timer USDJPY,H1: Timer interval (Max=1.266 Min=0.750) seconds, count=839 elapsed 843.485 seconds

2023.07.31 20:23:44.326 Timer USDJPY,H1: Timer interval (Max=1.266 Min=0.781) seconds, count=838 elapsed 842.735 seconds

2023.07.31 20:14:59.263 Timer USDJPY,H1: Timer interval (Max=1.219 Min=0.781) seconds, count=316 elapsed 317.672 seconds

2023.07.31 20:14:58.482 Timer USDJPY,H1: Timer interval (Max=1.219 Min=0.812) seconds, count=315 elapsed 316.891 seconds

2023.07.31 20:11:20.357 Timer USDJPY,H1: Timer interval (Max=1.203 Min=0.812) seconds, count=98 elapsed 98.766 seconds

2023.07.31 20:11:01.060 Timer USDJPY,H1: Timer interval (Max=1.188 Min=0.812) seconds, count=79 elapsed 79.469 seconds

2023.07.31 20:11:00.247 Timer USDJPY,H1: Timer interval (Max=1.188 Min=0.906) seconds, count=78 elapsed 78.657 seconds

2023.07.31 20:10:45.935 Timer USDJPY,H1: Timer interval (Max=1.125 Min=0.906) seconds, count=64 elapsed 64.344 seconds

2023.07.31 20:10:45.032 Timer USDJPY,H1: Timer interval (Max=1.125 Min=0.953) seconds, count=63 elapsed 63.438 seconds

2023.07.31 20:09:48.607 Timer USDJPY,H1: Timer interval (Max=1.063 Min=0.953) seconds, count=7 elapsed 7.016 seconds

2023.07.31 20:09:47.654 Timer USDJPY,H1: Timer interval (Max=1.063 Min=1.000) seconds, count=6 elapsed 6.063 seconds

2023.07.31 20:09:42.591 Timer USDJPY,H1: Timer interval (Max=1.000 Min=1.000) seconds, count=1 elapsed 1.000 seconds

2023.07.31 20:09:40.607 Timer USDJPY,H1: initialized

2023.07.31 20:09:40.607 Timer USDJPY,H1: Monitoring timer delay...

2023.07.31 20:09:33.794 Expert Timer USDJPY,H1: loaded successfully


 

MT5 reports:

Journal logs:

2023.07.31 20:09:32.845 Terminal Doo Group MetaTrader 5 Terminal x64 build 3815 started for Doo Holding Group Limited

2023.07.31 20:09:32.845 Terminal Windows Server 2016 build 14393 on KVM, 4 x Common KVM processor, 5 / 7 Gb memory, 11 / 39 Gb disk, RDP, UAC, admin, GMT+8

2023.07.31 20:09:32.845 Terminal C:\Users\Administrator\Desktop\Work\MT5\MT5.3

2023.07.31 20:09:40.713 Experts expert Timer (NZDCAD,H4) loaded successfully

2023.07.31 20:09:41.029 Network '5330425': authorized on ICMarketsSC-MT5 through Access Server - VIR (ping: 7.48 ms, build 3802)

2023.07.31 20:09:41.029 Network '5330425': previous successful authorization performed from 152.32.151.198 on 2023.07.31 14:59:49

2023.07.31 20:09:41.906 Network '5330425': terminal synchronized with Raw Trading Ltd: 0 positions, 0 orders, 2194 symbols, 0 spreads

2023.07.31 20:09:41.906 Network '5330425': trading has been enabled - hedging mode

Experts logs:

2023.07.31 20:09:42.126 Timer (NZDCAD,H4) Monitoring timer delay...

2023.07.31 20:09:44.139 Timer (NZDCAD,H4) Timer interval (Max=1.015 Min=1.015) seconds, count=1 elapsed 1.015 seconds

2023.07.31 20:09:45.139 Timer (NZDCAD,H4) Timer interval (Max=1.015 Min=1.000) seconds, count=2 elapsed 2.015 seconds

2023.07.31 20:09:48.125 Timer (NZDCAD,H4) Timer interval (Max=1.015 Min=0.985) seconds, count=5 elapsed 5.000 seconds

2023.07.31 20:09:55.168 Timer (NZDCAD,H4) Timer interval (Max=1.032 Min=0.985) seconds, count=12 elapsed 12.047 seconds

2023.07.31 20:09:56.130 Timer (NZDCAD,H4) Timer interval (Max=1.032 Min=0.953) seconds, count=13 elapsed 13.000 seconds

2023.07.31 20:10:38.191 Timer (NZDCAD,H4) Timer interval (Max=1.047 Min=0.953) seconds, count=55 elapsed 55.062 seconds

2023.07.31 21:01:27.209 Timer (NZDCAD,H4) Timer interval (Max=1.063 Min=0.953) seconds, count=3104 elapsed 3104.078 seconds

2023.07.31 21:01:28.128 Timer (NZDCAD,H4) Timer interval (Max=1.063 Min=0.922) seconds, count=3105 elapsed 3105.000 seconds


 

These two tests are interesting because I find out although the interval can be larger or smaller than 1 second but the total timer count tends to be the same as the elapsed time from the begining.

The deviation in MT5 is smaller than in MT4.

 
Longsen Chen #:

These two tests are interesting because I find out although the interval can be larger or smaller than 1 second but the total timer count tends to be the same as the elapsed time from the begining.

The deviation in MT5 is smaller than in MT4.

Interesting issue.

I will improve the testing code to have more precise data and also check MT5 as you did.

 
Longsen Chen:

I setup a one-second timer. I expect I can receive a signal on OnTimer() every second. But the truth is that there could be a delay. Sometimes the delay can be very large. The recent record is 849 seconds delay.

I'm very surprised with the delay. 

Is there any way to get a reliable timer?

I have attach the codes and screen shot.

The timer in the indicators is very unreliable, and highly dependent on events. The EA's timer is reliable. If you are using indicators in an EA, then this may be the reason.

It is also worth noting that OnChartEvent() is very unreliable in MT4, the operation of which is blocked for several tens of milliseconds if a certain threshold of calculations from it is exceeded (by about 10 milliseconds). In this case, calculations from OnChartEvent() must be transferred to the timer.

 
Nikolai Semko #:

The timer in the indicators is very unreliable, and highly dependent on events. The EA's timer is reliable. If you are using indicators in an EA, then this may be the reason.

It is also worth noting that OnChartEvent() is very unreliable in MT4, the operation of which is blocked for several tens of milliseconds if a certain threshold of calculations from it is exceeded (by about 10 milliseconds). In this case, calculations from OnChartEvent() must be transferred to the timer.

This topic is about timer in an EA.

Please stay on topic.

 
Alain Verleyen #:

This topic is about timer in an EA.

Please stay on topic.

Nikolai Semko #:

The timer in the indicators is very unreliable, and highly dependent on events. The EA's timer is reliable. If you are using indicators in an EA, then this may be the reason.

It is also worth noting that OnChartEvent() is very unreliable in MT4, the operation of which is blocked for several tens of milliseconds if a certain threshold of calculations from it is exceeded (by about 10 milliseconds). In this case, calculations from OnChartEvent() must be transferred to the timer.


 
Nikolai Semko #:


Nicolai. The tests show delays when using a timer in an EA without using any indicator.