on tick current time 00 seconds

 

Hi All, 


If I run the onTick function to tell me CurrentTime, how can I filter the EA to do actions only when the time is :00 seconds?


Thanks for the answers

 
Richards Laudurgs: Hi All, If I run the onTick function to tell me CurrentTime, how can I filter the EA to do actions only when the time is :00 seconds? Thanks for the answers

There is no guarantee that there will be a time with zero seconds.

The TimeCurrent is reported as the trade server time and it is only updated when a new tick arrives, so there is no guarantee that a tick will arrive at exactly that time with zero seconds.

In fact, a tick could arrive only after several minutes, or even several hours or even days (weekend).

So all you can do, is detect when the TimeCurrent has changed in respect to the minutes and ignoring the seconds.

void OnTick() {
   static int  nMinutesCurrent  = WRONG_VALUE;
          int  nMinutesPrevious = nMinutesCurrent;
               nMinutesCurrent  = TimeCurrent() / 60;
          bool bMinutesNew      = nMinutesCurrent != nMinutesPrevious;
          
   if( bMinutesNew ) {
      // A new minute was detected
   };
};
 
@Enrique Enguix #: To filter the EA to perform actions only when the time is at :00 seconds, you can use the TimeSeconds() function in the MQL5 language. The TimeSeconds() function returns the current time in seconds. Here's an example of how you can use this function in the onTick() function to perform actions only when the time is at :00 seconds: In the example above, the currentTime % 60 expression returns the remainder of currentTime divided by 60. If the remainder is zero, it means that the current time is at :00 seconds.

I know of no such function called "TimeSeconds()". There is nothing about it in the documentation. In fact TimeCurrent() is already in seconds.

Also, please edit your post and use "</>" or Alt-S for the code.

 
Fernando Carreiro #:

I know of no such function called "TimeSeconds()". There is nothing about it in the documentation. In fact TimeCurrent() is already in seconds.

Also, please edit your post and use "</>" or Alt-S for the code.

I'm a bit confused lately, mixing both languages.  There is hardly any code, that's why it was not added with <>
 
Richards Laudurgs:

Hi All, 


If I run the onTick function to tell me CurrentTime, how can I filter the EA to do actions only when the time is :00 seconds?


Thanks for the answers

Hi , Binary options ? you would have to anticipate the "hypothetical" start of the next bar assuming you are in sync with the BO platform .

The following is just a showcase of counting down to the next bar .

#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
/* the countdown string assembler
*/
string return_two_digits(int value){
if(value<10){return("0"+IntegerToString(value));}
return(IntegerToString(value));
}
string countdown_text(datetime next_bar,
                      datetime time_now){
//difference in seconds 
  int secs=(int)next_bar-(int)time_now;
//days
      //how many full days fit in the seconds difference 
  int days=(int)MathFloor(((double)secs)/86400);
  //remove the seconds of the full days from the seconds difference
  secs-=days*86400;
//hours
  int hours=(int)MathFloor(((double)secs)/3600);
  secs-=hours*3600;
//minutes
  int minutes=(int)MathFloor(((double)secs)/60);
  secs-=minutes*60;
//format 
  return("D:"+return_two_digits(days)+"|H:"+return_two_digits(hours)+"|M:"+return_two_digits(minutes)+"|S:"+return_two_digits(secs));
}
ENUM_TIMEFRAMES tfs[]={PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1};
string          tf_texts[]={"M1","M5","M15","M30","H1","H4","D1","W1","MN1"};
int OnInit()
  {
  while(!EventSetTimer(1)){
  Sleep(24);
  } 
  return(INIT_SUCCEEDED);
  }
  void OnTimer()
  {
//---
  string comm="";
  datetime now=0,server=TimeTradeServer();
  
  for(int i=0;i<ArraySize(tfs);i++){
    ResetLastError();
    now=iTime(_Symbol,tfs[i],0);
    if(GetLastError()==0&&(server-now)<=PeriodSeconds(tfs[i])){
    comm+=tf_texts[i]+":"+countdown_text((datetime)(now+PeriodSeconds(tfs[i])),TimeCurrent())+"\n";
    }  
  }
  Comment(comm);

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


 

 
Fernando Carreiro #:

There is no guarantee that there will be a time with zero seconds.

The TimeCurrent is reported as the trade server time and it is only updated when a new tick arrives, so there is no guarantee that a tick will arrive at exactly that time with zero seconds.

In fact, a tick could arrive only after several minutes, or even several hours or even days (weekend).

So all you can do, is detect when the TimeCurrent has changed in respect to the minutes and ignoring the seconds.

Yes, a tick could arrive later, but in the time period I'm going to select, the time in Market Watch 99.9% of the time changes from :59 -> :00 -> :01, so can the EA detect the change in Market Watch clock and perform action when the time on Market watch is :00?

 
@Enrique Enguix #: I'm a bit confused lately, mixing both languages.  There is hardly any code, that's why it was not added with <>

The function "TimeSeconds()" in MQL4 has a different functionality and it returns only the seconds part of the time, not the time in seconds. TimeCurrent() already returns the time in seconds.

PS! My solution above works on both MQL5 and MQL4.

 
Richards Laudurgs #: Yes, a tick could arrive later, but in the time period I'm going to select, the time in Market Watch 99.9% of the time changes from :59 -> :00 -> :01, so can the EA detect the change in Market Watch clock and perform action when the time on Market watch is :00?
Yes, my solution, provided above, works even if there are ticks every second, so it will synchronise to ":00" in that case.
 
Fernando Carreiro #:
Yes, my solution provided above works even if there are ticks every second, so it will synchronise to ":00" in that case.

Great, thanks a lot