# Checking Trading Hours Range > 23:00

17

Hi :)

I have a check for trading hours in my EA that I want to extend. Currently it´s working like this:

```extern int StartHour=20;

extern int EndHour=23;

if (Hour() >= StartHour && Hour() <= EndHour)

{

.......

}```

This check works great, however, only if EndHour is not extending beyond "23". But I want to be able to defne a range of for example: 20 to 2, in which case the above approach will not work correctly as you see. It will work again if using 0 to 2, but not for 20 to 2.

1068

What you have to do is to shift the start and end time in some cases and if your start if after your end (from 20:00 to 02:00).

Example if the current time is before the start, start must be shifted back by 24 hours.

Example if the current time is after the start, end must be shifted forward by 24 hours.

Example how you can code it:

if(start_time > end_time && TimeCurrent() < start_time) start_time -= 24*60*60;
else if(start_time > end_time && TimeCurrent() > start_time) end_time += 24*60*60;

17

Thanks a lot Eric, I think I understand what you mean, but could you give an example how I would implement this in my code above? I am not getting it completely right now, haha :) Thanks.
1068

start_time = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE) + " " + Time_To_Start);
end_time = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE) + " " +Time_To_Stop);

if(start_time > end_time && TimeCurrent() < start_time) start_time -= 24*60*60;
else if(start_time > end_time && TimeCurrent() > start_time) end_time += 24*60*60;

To be tested. That's just a quick piece of code for the example ...

Moderator
10652

```input double  SessionStart    =22.00;
input double  SessionEnd      =05.00;

//-----------------------------------------

if(Session())
{

}

//-----------------------------------------
bool Session()
{
double Servertime=Hour()+(Minute()*0.01);
if((SessionEnd-SessionStart)<0 && (Servertime<SessionEnd || Servertime>=SessionStart))
return (true);
if((SessionEnd-SessionStart)>0 && Servertime<SessionEnd && Servertime>=SessionStart)
return (true);
if(SessionStart+SessionEnd==0)
return (true);
return (false);
}```
17

Thanks a lot, much appreciated. However, what is "Endl", that variable is not declared.
17

I think I got it now and it´s working fine it seems. Anyone seeing a problem with this that I might have missed?

```bool IsTradingSession(int FuncStartHour, int FuncEndHour)
{
if (FuncStartHour < FuncEndHour)
{
if (Hour() >= FuncStartHour && Hour() <= FuncEndHour)
return(TRUE);
return(FALSE);
}

if (FuncStartHour > FuncEndHour)
{
if (Hour() >= FuncStartHour && Hour() <= 23)
return(TRUE);
if (Hour() <= FuncEndHour)
return(TRUE);
return(FALSE);
}

if (Hour() == FuncStartHour)
return(TRUE);
return(FALSE);
}```
Moderator
10652

chidrios:
Thanks a lot, much appreciated. However, what is "Endl", that variable is not declared.
Sorry, that was a quick copy and paste with some alterations, I corrected it now.
17

Thanks a lot, both solutions do work now:)