Download MetaTrader 5

Filter Hours of trading

To add comments, please log in or register
I_Need_Money
96
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.

noonehastherighttojudgeanother
235
noonehastherighttojudgeanother  

so, for example, you do not want to trade at 13, right? Well, this code will. Because 13 is greater or equal to 10. With all those ORs something is more likely to true.

if((Hour()<=7 || Hour()>=10) || (Hour()<=14 || Hour()>=18))return(0); //Prefer Trading Hours

You might want to code it like this, perhaps:

if((Hour()>=7 && Hour()<=10) || (Hour()>=14 && Hour()<=18))return(0); //Prefer Trading Hours

sn

Simon Gniadkowski
Moderator
18018
Simon Gniadkowski  

Or like this . . .

bool AllowTradesByTime()
   {
   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_Time < Finish_Time )
      if ( (Current_Time < Start_Time) || (Current_Time >= Finish_Time) ) return(false);
      
   if ( Start_Time > Finish_Time )
      if ( (Current_Time < Start_Time) && (Current_Time >= Finish_Time) ) return(false);
      
   return(true);
   }
I_Need_Money
96
I_Need_Money  
serpentsnoir:

so, for example, you do not want to trade at 13, right? Well, this code will. Because 13 is greater or equal to 10. With all those ORs something is more likely to true.

if((Hour()<=7 || Hour()>=10) || (Hour()<=14 || Hour()>=18))return(0); //Prefer Trading Hours

You might want to code it like this, perhaps:

if((Hour()>=7 && Hour()<=10) || (Hour()>=14 && Hour()<=18))return(0); //Prefer Trading Hours

sn

Thank you, but I did try the code with && and the EA was actually trading outside the selected time.

I_Need_Money
96
I_Need_Money  
RaptorUK:

Or like this . . .


Thank you,

I can handle simple coading, but need some help with this one. I don't see how it would apply both range of time. Where I would apply both ranges?

Simon Gniadkowski
Moderator
18018
Simon Gniadkowski  
I_Need_Money:


Thank you,

I can handle simple coading, but need some help with this one. I don't see how it would apply both range of time. Where I would apply both ranges?

To use my code add it after the end of your existing code . . . then you call it, maybe like this . . .

if (!AllowTradesByTime()) return(0);

Do you mean Start_Time and Finish_Time ? they are globally defined variables up at the start of the code . . .

extern int Start_Time = 1;          // Time to allow trading to start ( hours of 24 hr clock ) 0 for both disables
extern int Finish_Time = 23;        // Time to stop trading ( hours of 24 hr clock ) 0 for both disables
I_Need_Money
96
I_Need_Money  
RaptorUK:

To use my code add it after the end of your existing code . . . then you call it, maybe like this . . .

Do you mean Start_Time and Finish_Time ? they are globally defined variables up at the start of the code . . .


Thank you.


Well... this is similar to what I have working now.
Global setting
extern int StartTime = 6; // Time to allow trading to start
extern int EndTime = 16; // Time to stop trading

Apply before it looks for an open order
if((Hour()<=StartTime || Hour()>EndTime))return(0); //Preferd Trading Hours


I was looking to set two seperate time ranges, for example 6-10 & 13-16.

Simon Gniadkowski
Moderator
18018
Simon Gniadkowski  
I_Need_Money:


Thank you.


Well... this is similar to what I have working now.
Global setting
extern int StartTime = 6; // Time to allow trading to start
extern int EndTime = 16; // Time to stop trading

Apply before it looks for an open order
if((Hour()<=StartTime || Hour()>EndTime))return(0); //Preferd Trading Hours


I was looking to set two seperate time ranges, for example 6-10 & 13-16.

What if you move to Australia and want to start trading at 15:00 and end at 07:00 the following day ?
I_Need_Money
96
I_Need_Money  
RaptorUK:
What if you move to Australia and want to start trading at 15:00 and end at 07:00 the following day ?


Well first thing frist, I would need to change the type of beer I drink.

The time is taken from the MetaTrader Market watch and I don't trade the AUD much. But if I needed to, it will take me back to what I am looking for. i am trying to trade two time ranges and if looking to tade the AUD i would need to trade 0-7 & 15-23.

Simon Gniadkowski
Moderator
18018
Simon Gniadkowski  
I_Need_Money:


Well first thing frist, I would need to change the type of beer I drink.

The time is taken from the MetaTrader Market watch and I don't trade the AUD much. But if I needed to, it will take me back to what I am looking for. i am trying to trade two time ranges and if looking to tade the AUD i would need to trade 0-7 & 15-23.

Or you could simply use something like my solution which will already cope with trading from 15:00 to 07:00
bkpleng
5
bkpleng  
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.

Your second option is not working because the hours are overlapping, all times are >10 or <14. You need to breakup the ranges. I would suggest:

if( !( (Hour()>=7 && Hour()<=10) || (Hour()>=14 && Hour()<=18) ) )return(0) /Preferred Trading Hours

12
To add comments, please log in or register