Time Filter for indicator ( Not EA )

 

Hello

How to add time filter for an indicator ? Can anyone help me.  For example begin time: 23:00 , end time: 03:00. between 23:00 and 03:00 indicator should not work.

Can't use TimeCurrent() like Expert advisers and have to user Time[] array instead. But cant use StrToTime too because it always return today date + time. that what is why bellow code not working . right? 


I need to remove date and keep hour:minute from  "Time[] " or add date before "23:00" for each bar ?  How should I do it?


   if (((Time[i]<StrToTime("23:00"))&&(Time[i]>StrToTime("03:00"))))
   	{
   //indicator codes
   	}



Thanks for help

 
KSforex:
 if (((Time[i]<StrToTime("23:00"))&&(Time[i]>StrToTime("03:00"))))
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. That fails since StringToTime is about bar zero, not bar i.
  3. Try
    #define  SECONDS  uint
    #define  HR2400 (PERIOD_D1 * 60) // 86400 = 24 * 3600 = 1440 * 60
    SECONDS     time(datetime when=0){        if(when == 0) when = TimeCurrent();
       return SECONDS(when % HR2400);
    }
    datetime    date(datetime when=0){        if(when == 0) when = TimeCurrent();
       return datetime(when - time(when) );
    }
    :
             #define HR2300 82800
             #define HR0300 10800
             datetime now=Time[i];
          int nowTime=time(now);       
       if(nowTime < HR2300 && nowTime > HR0300)
 

I found this way :

It works fine but the only problem is  Beginning should be greater that End ( or Beginning and End should be in same day it means can't use from 2300-today to 0200-tomorrow). I could not solve this problem. I have to use this command twice for 2300 - 0000 and 0000-0300

extern string Beginning = "00:00";
extern string End = "03:00";

string Date_Array = StringSubstr(TimeToStr(Time[i],TIME_DATE|TIME_SECONDS),0,StringLen(TimeToStr(Time[i-1],TIME_DATE|TIME_SECONDS))-8);

   if !( (Time[i-1]>StrToTime(Date_Array + " " + Beginning))&&(Time[i-1]<StrToTime(Date_Array + " " + End)) )
   
   {
   }



I'm trying to understand your code. would you please explain this lines :

#define  SECONDS  uint

and

SECONDS     time(datetime when=0){        if(when == 0) when = TimeCurrent();
   return SECONDS(when % HR2400);
}
 

did you try TimeHour() 

int hour = TimeHour(Time[i]);
bool timeWindow = (hour>=23 && hour<3);

if (!timeWindow) { 
      // codes
}
 
KSforex: I'm trying to understand your code. would you please explain this lines :
#define  SECONDS  uint
<mtx doesn't="" have="" a="" working="" MTx doesn't have a working typedef, so you have to use a #define.
          typedef in C
Reason: