Filter Hours of trading

Back to topics list  | 1 2 To post a new topic, please log in or register
avatar
36
I_Need_Money 2011.09.05 19:20 #
 

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.

 
avatar
14167
RaptorUK 2011.09.05 20:10 #
 
What value of Hour would make this true ? (Hour()<=0 && Hour()>=5)
avatar
13006
WHRoeder 2011.09.05 20:48 #
 
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);
 
avatar
36
I_Need_Money 2011.09.05 23:33 #
 
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
 
avatar
36
I_Need_Money 2011.09.05 23:36 #
 
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.
avatar
1815
SDC 2011.09.06 06:18 #
 

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

 
avatar
14167
RaptorUK 2011.09.06 12:28 #
 
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.
avatar
1
chanqinghuang 2013.05.13 15:06 #
 
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);
   }
}

avatar
3
yeders 2016.05.17 17:53 #
 

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? 

Back to topics list   | 1 2  

To add comments, please log in or register