Download MetaTrader 5
To add comments, please log in or register
Find out how your rating can be improved
Chidrios
17
Chidrios 2014.04.23 06:38 

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.


Can someone help please and adjust the code? Thank you:)

Eric Venturi-Bloxs
51
Eric Venturi-Bloxs 2014.04.23 07:00  

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;

Chidrios
17
Chidrios 2014.04.23 07:13  
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.
Eric Venturi-Bloxs
51
Eric Venturi-Bloxs 2014.04.23 08:52  

You must define your trading session. Example :


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 ...

Keith Watford
Moderator
8016
Keith Watford 2014.04.23 08:57  
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);
  }
Chidrios
17
Chidrios 2014.04.23 15:09  
Thanks a lot, much appreciated. However, what is "Endl", that variable is not declared.
Chidrios
17
Chidrios 2014.04.23 19:51  

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);
}
Keith Watford
Moderator
8016
Keith Watford 2014.04.23 20:59  
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.
Chidrios
17
Chidrios 2014.04.23 22:54  
Thanks a lot, both solutions do work now:)
/
To add comments, please log in or register