Time entry/exit filter?

 

Hi All,

Just a quick question, anyone know a neater way of building a filter to restrict/allow new positions based on the day of week, hour and minute?

my logic wont work properly in the backtester. It allows trades after the cutoff time,(not good) but it closes all open positions on 'time exit', which is what I want.

Help????

int CloseDay = 5;

int CloseHour = 19;

int CloseMin = 43;

int EndDay = 5;

int EndHour = 17;

int EndMin = 59;

datetime CurrentTime = TimeCurrent();

bool TimeCloseFilter = false;

bool RestrictEntryFilter = false;

int CurrentDay = TimeDayOfWeek(CurrentTime);

int CurrentHour = TimeHour(CurrentTime);

int CurrentMin = TimeMinute(CurrentTime);

if(CurrentDay>=CloseDay && CurrentHour>=CloseHour && CurrentMin>=CloseMin)

{

TimeCloseFilter = true;

}

if(CurrentDay>=EndDay && CurrentHour>=EndHour && CurrentMin>=EndMin)

{

RestrictEntryFilter = true;

}

TimeCloseFilter will close all trades at (Friday,19:43) if true. RestrictEntryFilter will prevent the opening of new positions after (Friday, 17:59). I call these in the entry/exit conditions.

 
AmIBroke:


my logic wont work properly in the backtester. It allows trades after the cutoff time,(not good) but it closes all open positions on 'time exit', which is what I want.

Help????

I call these in the entry/exit conditions.

Your code looks OK, I suspect your issue is not in this code but where you check if RestrictEntryFilter == false before placing an order. Maybe you are checking if it's true by mistake ?

 
AmIBroke:
anyone know a neater way of building a filter to restrict/allow new positions based on the day of week, hour and minute?
    /*++++ Day/Time allowed to open*/{
    datetime    now = TimeGMT();
    int         DOW = TimeDayOfWeek(now),   /* forum.mql4.com/33851
    // reports DayOfWeek() always returns 5 in the tester. No refresh?*/
                DayMask = 1 << DOW;
    //                      #define DAYS_MAX    0x3F// 1<<6-1=63. (S-F)
    //extern int      Days.Mask               =  55;      // Not Wed
    if (Days.Mask & DayMask == 0){  StrApnd(EA.status," Day=",DOW); return; }
    //extern double   TradeHr.UTC.Start   =   7.3;    // London-1
    //extern double   TradeHr.UTC.End     =  12.9;    // NY open
    int secStart    = 3600*TradeHr.UTC.Start,
        secEnd      = 3600*TradeHr.UTC.End,
        hrBeg       = (now-secStart+86400)%86400,
        hrEnd       = (now-secEnd  +86400)%86400;
    if (hrBeg > hrEnd){ double Tminus=hrBeg/3600.-24;
                StrApnd(EA.status," HR", DoubleToStr(Tminus,2));    return; }
    /*---- Day/Time allowed to open*/}
 
RaptorUK:

Your code looks OK, I suspect your issue is not in this code but where you check if RestrictEntryFilter == false before placing an order. Maybe you are checking if it's true by mistake ?


Thanks for the replies, much appreciated. The problem may be in the backtester??? I tested the Ea at the weekend, and part way through the last (15min) bar on friday, the ask price crossed my buy level and triggered a buy. The order was placed and the TimeCloseFilter closed the position on the next tick. This happened on every tick until market closed.

I ran the Ea again on Tuesday, in the backtester, and the results were ok. Not sure what is happening here, but I'm using every tick as a setting in the backtester, so perhaps the problem is in there somewhere...

Anyway, I'll run it on demo this friday, and see if it works on streaming data.

Cheers, Chris.

 
WHRoeder:


Cool, Thanks. I'll try this!!!
 
RaptorUK:

Your code looks OK, I suspect your issue is not in this code but where you check if RestrictEntryFilter == false before placing an order. Maybe you are checking if it's true by mistake ?


if(CurrentDay>=EndDay && CurrentHour>=EndHour && CurrentMin>=EndMin)

{

RestrictEntryFilter = true;

}

I changed the value of EndMin to 45 (instead of 59), and it works fine now????? Perhaps testing >59 minutes, as a condition is not a good idea???

 
AmIBroke:

I changed the value of EndMin to 45 (instead of 59), and it works fine now????? Perhaps testing >59 minutes, as a condition is not a good idea???

Without seeing the rest of your code it's hard to say . . .

How do you set this variable RestrictEntryFilter back to false ?

 
WHRoeder:


All over the net there's this same issue: some expert coder cuts a piece of code out of an EA and pastes it in a posting somewhere on a forum (like here). When someone new to coding (like me) puts this
code into his EA, of course it won't work, because there's always some variables not declared error - and if one finds the way to declare this specific variable, then it turns out that it gets its value from somewhere
in the posters EA-code and that's end of story. After all his effort, there's only one thing left to do: go look for somebody's code, who understands that not everyone is an expert like himself (otherwise they wouldn't
need to ask for help on these forums to begin with) and sees to it that the code he presents is ready to be 'cut and pasted' by 'newbies'; i.e. variables separate from the rest of the code and some explanation as to
where everything has to be pasted to...

Anyway, cheers for trying to help others, but it would be better if you either did it right (put yourself in the place of someone who knows little or nothing about mql4 coding) or don't do it at all.

 
ronald123:


After all his effort, there's only one thing left to do:

Yes you are correct, learn how to code.
Reason: