OnTimer() doesn't seem to run as expected

 

My code is supposed to display a clock on the chart. In this case, I am displaying seconds only.

The problem is that I set the loop to run every 40 milliseconds with EventSetMillisecondTimer(40). Considering that 40 milliseconds happens 25 times at every second, the display update should never skip a second. But when the market is slow and not many ticks are coming through, many seconds are skipped, as if the code was running on OnTick() rather than on OnTimer().


What am I missing?


Here is the full code:


#property strict
int OnInit()   {
        EventSetMillisecondTimer(40);
        return(INIT_SUCCEEDED);
}

// FUNCTION drawLabel ------------------------------------------
void drawLabel()  {
   string name = "";
   string text = "";

   int dx  = 10; int dy = 560;
   name = "LabelDebug1";
   text = "seconds: " + Seconds();
   if (ObjectFind(name) != -1) {ObjectDelete(name);}
   ObjectCreate(name, OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSetText(name, text, 20, "Times New Roman", Black);
   ObjectSet(name, OBJPROP_CORNER, 1);
   ObjectSet(name, OBJPROP_XDISTANCE, dx);
   ObjectSet(name, OBJPROP_YDISTANCE, dy);
}

// FUNCTION RunLoop --------------------------------------------------
void RunLoop()    {
   drawLabel();
}

void OnTimer()    {
   RunLoop();
}


TIA

 
The answer is simple, but your previous rudeness put you on my do not help list. Live in ignorance.
 
William Roeder:
The answer is simple, but your previous rudeness put you on my do not help list. Live in ignorance.

Oh yes. Because you are a PARAGON of good manners.

https://www.mql5.com/en/forum/262079

You know how to respect forum members like nobody else.

https://www.mql5.com/en/forum/155887

<Deleted>

Download older MT4 versions?
Download older MT4 versions?
  • 2018.07.01
  • www.mql5.com
Hi all, Due to ESMA and FCA restrictions I was forced to change brokers to keep my 500:1 leverage...
 

Please use ChartRedraw() after creating or modifying objects.

 
Marco vd Heijden:

Please use ChartRedraw() after creating or modifying objects.

That doesn't work.

I've added ChartRedraw() to the end of every function in the code above except Init, and it doesn't make any difference. The problem persists.

 

In that case slower your timer interval.

If you want something like a clock create the object once and modify the time with ObjectSetString().

 
Seconds is updated by new ticks. Use TimeSeconds with gmt or local time.
 
Laszlo Tormasi:
Seconds is updated by new ticks. Use TimeSeconds with gmt or local time.

Ah, yes! That was the problem. It works as expected now.

Many thanks!

Reason: