Download MetaTrader 5
To add comments, please log in or register
The entire MQL5.community history on a single page!
Joan
22
Joan 2015.07.13 08:12 

I have a good MT4 EA, but the timefilter does not work as supposed.

We want it to stop trade at friday evening, and close all orders late friday evening. -  This does work correctly,   -    but it also does not trade from 0:00 - 03:00 friday morning.

Then it is not suppoed to open any orders before after monday  start trading (11:30) - But it does trade from 00:00 - 03:00, after that it waits to 11:30
Also we do not want it to trade in mornings from 9:00 to 11:30, which works perfectly.

Broker is Tickmill

Can anyone please help to see my mistake in coding. 

 if(CloseDeals())CloseAllPositions("Close All Now"); 
int start()
{
if(GoodTime()== true)
{
//Trading code
bool GoodTime()
  {
   datetime pauseStartTime, pauseEndTime, currentTime, fridayStopTime, mondayStartTime;
   pauseStartTime = StrToTime(Trading_Pause_Start);
   pauseEndTime = StrToTime(Trading_Pause_End);
   fridayStopTime = StrToTime(Friday_Stop_Trading);
   mondayStartTime = StrToTime(Monday_Start_Trading);
   currentTime = TimeCurrent();
   
   if ((currentTime<pauseStartTime || currentTime>=pauseEndTime) &&
      (DayOfWeek()!=0) &&
      (DayOfWeek()!=5 || currentTime<fridayStopTime) &&
      (DayOfWeek()!=1 || currentTime>=mondayStartTime)) 
      {
      return true;
      } else {
      return false;
   }
  
}

bool CloseDeals()
 {
   datetime currentTime, fridayCloseTime;
   currentTime = TimeCurrent();
   fridayCloseTime = StrToTime(Friday_Close_Deals);
   
   if (Use_Friday_Close && DayOfWeek()==5 && currentTime>=fridayCloseTime)
     {
      return true;
      } else {
      return false;
     }
 }
  
whroeder1
13619
whroeder1 2015.07.13 12:15  
  1. There is no need to write if(bool) (do true) else (do false)
       if (Use_Friday_Close && DayOfWeek()==5 && currentTime>=fridayCloseTime)
         {
          return true;
          } else {
          return false;
         }
    Just write (do bool)
    return Use_Friday_Close && DayOfWeek()==5 && currentTime>=fridayCloseTime;
    
    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  2. but it also does not trade from 0:00 - 03:00 friday morning. Then it is not suppoed to open any orders before after monday  start trading (11:30) - But it does trade from 00:00 - 03:00, after that it waits to 11:30 Also we do not want it to trade in mornings from 9:00 to 11:30,
    Too complicated to understand, to complicated to code.
       if ((currentTime<pauseStartTime || currentTime>=pauseEndTime) &&
          (DayOfWeek()!=0) &&
          (DayOfWeek()!=5 || currentTime<fridayStopTime) &&
          (DayOfWeek()!=1 || currentTime>=mondayStartTime)) 
          {
          return true;
          } else {
          return false;
       }
    Simplify
    if (DayOfWeek()==0)                                    return false; // Not Sunday
    if (DayOfWeek()==5 && currentTime > fridayStopTime)    return false; // Late Friday
    if (DayOfWeek()==1 && currentTime < mondayStartTime)   return false; // Early Monday
    return currentTime <= pauseStartTime || currentTime >= pauseEndTime; // good except pauseTime.
       }
    x
Joan
22
Joan 2015.07.13 13:59  

Thanks.
I will try that, but I had to make a small shange, just if any body else has same question.

 

   if (DayOfWeek()==0)                                    return false; // Not Sunday
   if (DayOfWeek()==5 && currentTime > fridayStopTime)    return false; // Late Friday 
   if (DayOfWeek()==1 && currentTime < mondayStartTime)   return false; // Early Monday  
   return currentTime <= pauseStartTime || currentTime >= pauseEndTime; // good except pauseTime.
  }  
/
To add comments, please log in or register