Filter Hours of trading - page 3

 
Hi Guys please help me i would like my system to trade only between 7am-5pm. how do i work with this coding. the person that made it for me does not respond anymore.

string  separator1          =  "------ GMT Trading Days ------";
      int     ppi_GMT_Offset = 0;//Your Broker Trade Server time offset relativly to GMT
input int     ppi_Sunday     = 0;
input int     ppi_Monday     = 1;
input int     ppi_Tuesday    = 1;
input int     ppi_Wednesday  = 1;
input int     ppi_Thursday   = 1;
input int     ppi_Friday     = 1;
input int     ppi_Saturday   = 0;

input bool    pb_TradeSession1        = true;//Enable trade in session1
input int     pi_StartHoursSession1   = 0;//Trade session  1 start hours,trade server time 
input int     pi_StartMinutesSession1 = 5;//Trade session  1 start minutes,trade server time 
input int     pi_StopHoursSession1    = 23;//Trade session 1 stop hours,trade server time 
input int     pi_StopMinutesSession1  = 59;//Trade session  1 stop minutes,trade server time 

input bool    pb_TradeSession2        = false;//Enable trade in session2
input int     pi_StartHoursSession2   = 16;//Trade session 2 start hours,trade server time 
input int     pi_StartMinutesSession2 = 20;//Trade session  2 start minutes,trade server time 
input int     pi_StopHoursSession2    = 0;//Trade session 2 stop hours,trade server time 
input int     pi_StopMinutesSession2  = 0;//Trade session  2 stop minutes,trade server time 

//+---------------------------------------------------------------------------+
      string  separator2                 =  "------ GMT Trading Hours ------";
      string  pps_StartTimeMon               = "00:00";
      string  pps_StopTimeMon                = "23:00";

      string  pps_StartTimeTue               = "00:00";
      string  pps_StopTimeTue                = "23:00";

      string  pps_StartTimeWed               = "00:00";
      string  pps_StopTimeWed                = "23:00";

      string  pps_StartTimeThu               = "00:00";
      string  pps_StopTimeThu                = "23:00";

      string  pps_StartTimeFri               = "00:00";
      string  pps_StopTimeFri                = "23:00";

      string  pps_StartTimeSat               = "00:00";
      string  pps_StopTimeSat                = "23:00";

      string  pps_StartTimeSun               = "00:00";
      string  pps_StopTimeSun                = "23:00";
//+------------------------------------------------------------------+
 

//+------------------------------------------------------------------+
 
ZCI-FX: i would like my system to trade only between 7am-5pm. how do i work with this coding.
  1. Remove your duplicate post.

  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3. input bool    pb_TradeSession1        = true;//Enable trade in session1
    input int     pi_StartHoursSession1   = 0;//Trade session  1 start hours,trade server time 
    input int     pi_StartMinutesSession1 = 5;//Trade session  1 start minutes,trade server time 
    input int     pi_StopHoursSession1    = 23;//Trade session 1 stop hours,trade server time 
    input int     pi_StopMinutesSession1  = 59;//Trade session  1 stop minutes,trade server time 
    All you've posted are inputs. If a trading times function has been implemented, just change the values to what you want. Otherwise, you have only four choices:
    1. Search for it,
    2. Beg at
    3. learn to code it. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
    4. or pay (Freelance) someone to code it.
    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
              No free help
              urgent help.
 


First, I'd follow William Roeder's requests (remove duplicate post, etc.) Then I'd simply use his morninghours/afternoonhours code from 2011 found on the previous page. Of course it needs a little tweaking to make it work in MQL5. Something like changing the Hour() functions to TimeHourMQL4() found here.

ZCI-FX:
Hi Guys please help me i would like my system to trade only between 7am-5pm. how do i work with this coding. the person that made it for me does not respond anymore.

ZCI-FX
ZCI-FX
  • www.mql5.com
Trader's profile
 
I wrote this function.
bool goodtimetotrade()
  {

   string badtimes[] = {"08:16-10:30","15:25-16:15"};
   string sep="-";
   ushort u_sep=StringGetCharacter(sep,0);
   string result[];
   int size = ArraySize(badtimes);

   int time=TimeLocal();

   for(int i=0; i<size; i++)
     {

      StringSplit(badtimes[i],u_sep,result);
      int timefrom = StrToTime(result[0]);
      int timeto = StrToTime(result[1]);
      if(time>timefrom && time<timeto)
        {
         return false;   // not a good time
        }
     }

  return true;   // time is fine


  }
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
I_Need_Money:

This is currently working as expected. If is outside this time range the EA does not trade.
if((Hour()<=StartTime || Hour()>EndTime))return(0); //Preferd Trading Hours

From an older EA, which I believed tested successfully but is not working.
if((Hour()<=7 || Hour()>=10) || (Hour()<=14 || Hour()>=18))return(0); //Prefer Trading Hours

Any Ideas why the second option is not working?

I am assuming I can configure & test the following, but was looking for a cleaner solution.
if(Hour()==7 || Hour()==8 || Hour()==9 || Hour()==10 || Hour()==14 || Hour()==15 || Hour()==16 || Hour()==17 || Hour()==18)return(0); //Prefer Trading Hours

Thank you.


I'm decades late to this chain but I wondered the same thing and saw @Simon Gniadkowski's function which works for a single time frame. With a little tweak it works for the 2 time frames letting the user specify. Just tested it with the EA I'm building.


bool AllowTradesByTime(int Start_Time, int Finish_Time, int Start_Time2, int Finish_Time2)
   {
      datetime Current_Time = TimeHour(TimeCurrent());
      if (Start_Time == 0) Start_Time = 24; if (Finish_Time == 0) Finish_Time = 24; if (Current_Time == 0) Current_Time = 24;
      if (Start_Time2 == 0) Start_Time2 = 24; if (Finish_Time2 == 0) Finish_Time2 = 24; if (Current_Time == 0) Current_Time = 24;
      
      if (Start_Time < Finish_Time && Start_Time2 < Finish_Time2)
         if ((Current_Time < Start_Time) || (Current_Time >= Finish_Time && Current_Time < Start_Time2) || (Current_Time >= Finish_Time2)) return(false);
         
      if (Start_Time > Finish_Time && Start_Time2 > Finish_Time2)
         if ((Current_Time < Start_Time) || (Current_Time >= Finish_Time && Current_Time < Start_Time2) || (Current_Time >= Finish_Time2)) return(false);
         
      return(true);
   }