Download MetaTrader 5

Filter Hours of trading - page 2

To add comments, please log in or register
I_Need_Money
96
I_Need_Money  

Boy….. Talk about hard headed.

serpentsnoir & bkpleng pointed to it, but I didn't see it.

//NOTE the origial code had return(0); if((Hour()<=StartTime || Hour()>EndTime))return(0); //Preferd Trading Hours

Which worked great when trading from 6-16

These are the ones that seems to work when looking to trade from 6-10 & 13-16

if((Hour()<=0 && Hour()>=5) || (Hour()<=11 && Hour()>=12) || (Hour()<=17 && Hour()>=23))return(0);

if(Hour()==6 || Hour()==7 || Hour()==8 || Hour()==9 || Hour()==10 || Hour()==13 || Hour()==14 || Hour()==15 || Hour()==16)

if((Hour()>=6 && Hour()<=10) || (Hour()>=13 && Hour()<=16))

Thank you all for your help.

Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
What value of Hour would make this true ? (Hour()<=0 && Hour()>=5)
whroeder1
15577
whroeder1  
I_Need_Money:

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

hour 0 .. 7 (true || false) || (true || false) = (true) || (true) = true = return

hour 8, 9 (false || false) || (true || false) = (false) || (true) = true = return

hour 10 .. 14 (false || true) || (true || false) = (true) || (true) = true = return

hour 15 .. 17 (false || true) || (false || false) = (true) || (false) = true = return

hour 18 .. 23 (false || true) || (false || true) = (true) || (true) = true = return


Instead, document what you actually mean, no thinking required:

bool morningHours   = (Hour() >  7 && Hour() < 10),
     afternoonHours =  Hour() > 14 && Hour() < 18,
     tradingHours   = morningHours || afternoonHours;
if (!tradingHours) return(0);
I_Need_Money
96
I_Need_Money  
RaptorUK:
What value of Hour would make this true ? (Hour()<=0 && Hour()>=5)

This sample has "return(0); " so looking to trade hours not listed. So the EA will not trade during 00 same as 24 & 05:00 hours
I_Need_Money
96
I_Need_Money  
WHRoeder:

hour 0 .. 7 (true || false) || (true || false) = (true) || (true) = true = return

hour 8, 9 (false || false) || (true || false) = (false) || (true) = true = return

hour 10 .. 14 (false || true) || (true || false) = (true) || (true) = true = return

hour 15 .. 17 (false || true) || (false || false) = (true) || (false) = true = return

hour 18 .. 23 (false || true) || (false || true) = (true) || (true) = true = return


Instead, document what you actually mean, no thinking required:


bool morningHours   = (Hour() >  7 && Hour() < 10),
     afternoonHours =  Hour() > 14 && Hour() < 18,
     tradingHours   = morningHours || afternoonHours;
if (!tradingHours) return(0);
Nice and very clean, also a fan of no thinking required.
Ian Venner
2584
Ian Venner  

I dont think that would work correctly.

int Hour( )
Returns the hour (0,1,2,..23) of the last known server time by the moment of the program start (this value will not change within the time of the program execution).

Simon Gniadkowski
Moderator
18024
Simon Gniadkowski  
I_Need_Money:

This sample has "return(0); " so looking to trade hours not listed. So the EA will not trade during 00 same as 24 & 05:00 hours
You didn't answer my question . . but never mind.
Chan Qing Huang
5
Chan Qing Huang  
This is my first attempt at coding a function to handle opening hours. it takes into account hours and minutes. If you find use for it, good!
extern bool UseTradingHours = true;
extern int OpenHour = 08;
extern int OpenMin = 30;
extern int CloseHour = 17;
extern int CloseMin = 30;

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

 if (UseTradingHours == true)
{
      if (TradingHours() == true)
      execute();
}

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


bool TradingHours()
{
   if(CloseHour>OpenHour) //within the day
   {
      if (OpenHour < TimeHour(TimeCurrent()) && TimeHour(TimeCurrent()) < CloseHour)
         {
         Comment("Open For Trading");
         return(true);
         }
      if (OpenHour == TimeHour(TimeCurrent()))
      {
         if(OpenMin<=TimeMinute(TimeCurrent()))
         {
         Comment("Open For Trading");
         return(true);
         }
         return(false);
      }
      
      if (CloseHour == TimeHour(TimeCurrent()))
      {
         if(CloseMin>=TimeMinute(TimeCurrent()))
         {
         Comment("Open For Trading");
         return(true);
         }
         return(false);
      }
      Comment("Closed");
      return(false);
   }
   if(OpenHour>CloseHour)  //Spanning two days
   {
      if (CloseHour < TimeHour(TimeCurrent()) && TimeHour(TimeCurrent()) < OpenHour)
         {
         Comment("Closed");
         return(false);
         }
      if (OpenHour == TimeHour(TimeCurrent()))
      {
         if(OpenMin<=TimeMinute(TimeCurrent()))
         {
         Comment("Open For Trading");
         return(true);
         }
         return(false);
      }
      if (CloseHour == TimeHour(TimeCurrent()))
      {
         if(CloseMin>=TimeMinute(TimeCurrent()))
         {
         Comment("Open For Trading");
         return(true);
         }
         return(false);
      }
      Comment("Open For Trading");
      return(true);
   }
}
Jeremy Roach
2347
Jeremy Roach  

hi there,

I'm keen to do this; but I would require: 

Allow trades between the minutes of: 05->25 and 40->55 

 

bool firsthalf   = (Minute() >  5 && Minute() < 25),
     secondhalf =  Minutes() > 35 && Minutes() < 55,
     tradingHours   = firsthalf || secondhalf; 
if (!tradingHours) return(0); 

 

I imagine this would work? 

12
To add comments, please log in or register