EventSetTimer not working properly

 

Dear MT-friends,


I programmed a simple clock-indicator that should show the current time in HH:MM:SS-format in the right upper corner of the chart. So far so good.


The problem is that this clock is supposed to update every second (EventSetTimer(1)), but it doesn't: there are many, frequent omissions ranging from one to several seconds. It looks like rather than updating as expected OnTimer (where I placed the code) and thus every second, it updates OnCalculate (i.e. on every tick, which can be more or less than every second).


Why? Any ideas how to make this work on every second?


Thanks


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetTimer(1);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
  string text = TimeToString(TimeLocal(),TIME_SECONDS);
  
//--- reset the error value 
   ResetLastError(); 
//--- create a text label 
   if(!ObjectCreate(0,"time",OBJ_LABEL,0,0,0)) 
     { 
      Print(__FUNCTION__, 
            ": failed to create text label! Error code = ",GetLastError()); 
      //return(false); 
     } 
//--- set label coordinates 
   ObjectSetInteger(0,"time",OBJPROP_XDISTANCE,100); 
   ObjectSetInteger(0,"time",OBJPROP_YDISTANCE,30); 
//--- set the chart's corner, relative to which point coordinates are defined 
   ObjectSetInteger(0,"time",OBJPROP_CORNER,CORNER_RIGHT_UPPER); 
//--- set the text 
   ObjectSetString(0,"time",OBJPROP_TEXT,text); 
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
  }
 
EventSetMillisecondTimer(500);

And don't forget to call

ChartRedraw();

After you modified objects.

 
Marco vd Heijden:

And don't forget to call

After you modified objects.

Thanks for the fast reply, @Marco vd Heijden. Yes, I tried further and also figured out that ChartRedraw() within OnTimer seems to be required, you are absolutely right. Before that I verified that Ontimer() was actually working, so I tried Comment() instead of ObjectCreate(), which also worked OnTimer, so I closed in on the Chart not being updated being the problem. It works now.