Download MetaTrader 5

Need help with time filter

To add comments, please log in or register
Publish your article and get a reward!
Mo
65
Mo 2014.01.27 16:15 
Hi all,

Having an issue closing orders .

Here is the code for filtering:
if((Hour()>=0 && Hour()<=4 || Hour()>=8  && Hour()<=24))return(false);

This is working fine, new trades are not opening. The issue is getting out of the trade. Ie. trade is placed at 7:45am gmt and exit is signaled at 8:15am. Nothing will happen, no tp, breakeven etc..

This is how I normally get out of a trade:

if  (abc>0 ) {

  close=true;

}
And have tried:
if (abc >0 || Hour()>=7 && Minute()>=50 ) {

close=true;

}


The above basically closes the trade exactly at 7:50am. Not what I am looking from. I want to exit the trade at my normal exit above bypassing the time filter.

Alain Verleyen
Moderator
30736
Alain Verleyen 2014.01.27 16:50  
zzdream:
Hi all,

Having an issue closing orders .

Here is the code for filtering:

This is working fine, new trades are not opening. The issue is getting out of the trade. Ie. trade is placed at 7:45am gmt and exit is signaled at 8:15am. Nothing will happen, no tp, breakeven etc..

This is how I normally get out of a trade:

And have tried:


The above basically closes the trade exactly at 7:50am. Not what I am looking from. I want to exit the trade at my normal exit above bypassing the time filter.

Apply only your filter to your opening then.

...

if(Opening_condition)
{
   if( ! (Hour()>=0 && Hour()<=4 || Hour()>=8  && Hour()<=24))
   {
     // Open your trade...
   }
}

// Close...
See also these posts about mixing of && and ||.
Mo
65
Mo 2014.01.27 17:52  
Thanks for the reply. Makes sense :)
Mo
65
Mo 2014.01.30 14:10  
Question:

Why is condition being evaluated as true:
if (abc>0 && hour()>=4 && hour()<=5) {

buy=true;

} 
Testing this, and trying to only trade from 4am gmt to 5am gmt. Trades are being opened at 6:05am.
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.01.30 14:18  
zzdream:
Question:

Why is condition being evaluated as true: Testing this, and trying to only trade from 4am gmt to 5am gmt. Trades are being opened at 6:05am.

Because it is true . . . Print() hour() and see what you get . . . better still . . .

Print("Hour() returns: ", Hour(), " current time is : ", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES) );
Mo
65
Mo 2014.01.30 14:42  
Thanks for replying,

I'll test the above when I get a chance. Can you please explain why ? Please excuse my ignornance ..totally new to this stuff..however find it extremely fascinating. I thought the code above will build a block around the two hours 4 & 5, nothing above or below.
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.01.30 14:49  
zzdream:
Thanks for replying,

I'll test the above when I get a chance. Can you please explain why ? Please excuse my ignornance ..totally new to this stuff..however find it extremely fascinating. I thought the code above will build a block around the two hours 4 & 5, nothing above or below.
I have seen Hour() not work as many people expect, others have reported that it works just fine . . . how is it working for you ? and . . . when you say between 4 & 5 you actually mean 4 and 5:59 . . . 5:59 has an Hour() of 5
Mo
65
Mo 2014.01.30 15:29  
Right so 4 and 5:59 is ok,but 6:05 should return false..I'm confused.
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.01.30 16:48  
zzdream:
Right so 4 and 5:59 is ok,but 6:05 should return false..I'm confused.
So test it and remove your confusion . . . it's very simple, test and see what is wrong.
Mo
65
Mo 2014.01.31 01:38  
Thanks Raptor, printing out really helps . Still playing around with different time filter combinations.
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.01.31 09:24  
zzdream:
Thanks Raptor, printing out really helps . Still playing around with different time filter combinations.
So was Hour() correct ?
12
To add comments, please log in or register