Trading in specified hours

It is often useful to limit the time, when our EA can trade. It can be done in two different ways, but I will show you probably the most elegant and flexible way – that is defining time as a string (it allows to define minutes or even seconds) and having it all in a separate function.

extern string time1Begin="4:00";
extern string time1End="12:00";
extern string time2Begin="16:00";
extern string time2End="20:00";

bool isTradingTime() {

    if(TimeCurrent()>StrToTime(time1Begin) && TimeCurrent()<StrToTime(time1End)) {
        return(true);
    }

    if(TimeCurrent()>StrToTime(time2Begin) && TimeCurrent()<StrToTime(time2End)) {
        return(true);
    }

    return(false);
}

A few notes (as always):

  • This example uses two time periods, but it is possible to add more :)
  • The code checks time according to broker time (what is done by TimeCurrent()), but if someone wants to define time according to the clock of the computer, where the EA is working, then he only needs to change TimeCurrent() to TimeLocal()
  • Because this EA is so universal, you can also put full dates in its time fields, then the EA will trade between specified days.

Share with your friends:

Send us your comments

Name (required)

Email (required)

Message

  • Facebook