Time Filter Code

 

Where can I get the time filter code? And where, within an EA's code, should I place it? Thanks.

 

Thanks newdigital.

 
Yoda_Glenn:
Where can I get the time filter code? And where, within an EA's code, should I place it? Thanks.

Two links about how to set time in timefilter:

- https://www.mql5.com/en/forum/173367/page17

- https://www.mql5.com/en/forum/173367

Timefilters.

For any kinds of timefilter, in the beginning of the code type the following:

extern bool UseHourTrade = True;

extern int FromHourTrade = 8;

extern int ToHourTrade = 17;[/CODE]

1. Timefilter with comments.

After this code

int start()

{[/CODE]

type the following:

if (UseHourTrade){

if((Hour()>=FromHourTrade)&&(Hour()<=ToHourTrade))

Comment("Trading Hours");

else

{

Comment("Non-trading Hours");

return(0);

}

}[/CODE]

2. Timefilter with no comments on the chart.

Some people do not like to see "non-trading hours" comments on the chart because this comments are not refreshing.

Everything is the same with item #1 but after

int start()

{[/CODE]

place the following code:

if (UseHourTrade){

if(!(Hour()>=FromHourTrade&&Hour()<=ToHourTrade)){

return(0);

}

}

3. Anti-Timefilter with comments.

Anti-timefilter is the following: EA will not trade within the hours you specified. During the news hours for example.

Use the following code instead of above mentioned:

[CODE]if (NonTradingHours){

if((Hour()>=FromHourTrade&&Hour()<=ToHourTrade)){

Comment("Non-Trading Hours!");

return(0);

}

}

4. Anti-Timefilter with no comments.

Use this code:

[CODE]if (NonTradingHours){

if((Hour()>=FromHourTrade&&Hour()<=ToHourTrade)){

return(0);

}

}

5. Timefilter with trailing stop.

Sometimes we want that our EAs trade in particular hours (timefilter). I means that EA will not open the new orders. But sometimes we need for EA to modify all already opened orders all the time irrespective of timefilter. For example, timefilter is since 8 till 17. But one order was open within 8 - 17. So we want for our EA to move trailing stop (modify the order) after 8 - 17.

It is timefilter with trailing stop.

Use the following code:

[CODE]if (UseHourTrade){

if (!(Hour()>=FromHourTrade && Hour()<=ToHourTrade)) {

Comment("Time for trade has not come else!");

}

}

And

in the sell and buy condition to open new order you write the following:

[CODE]if (Buy1_1 = Buy2_2 && .... && Hour()>=FromHourTrade && Hour()<=ToHourTrade) Order = SIGNAL_BUY;

if (Sell1_1 > Sell1_2 && Sell2_1 <= Sell2_2 && ... && Hour()>=FromHourTrade && Hour()<=ToHourTrade) Order = SIGNAL_SELL;

You may use any veriations of above mentioned codes (anti-timefilter with trailing stop, anti-timefilter with trailing stop and with no comments and so on).

 

I updated some timefilter codes on the second post.

 

- "Non-Trading Hours" on the screen fixing: example code; now it displays "Trading Hours" during trading hours and "Non-trading Hours" during non-trading hours. Thanks Locutus.

 

Broker's server time indicator https://www.forex-tsd.com/forum/debates-discussions/116-something-interesting-please-post-here/page5#comment_95467 (change the solor in the indicator's settings in input to see the text).

For example it is Alpari broker server time:

Files:
 

How to know the differencies between your broker's server time and GMT?

Look at this thread https://www.mql5.com/en/forum/177102 - our forex-tsd news indicator and news EA are having it written on the chart.

It is news indicator:

And this news indicator is having alert:

News EA can be used to see the time diference between GMT and broker's server time: