No Friday trading

 

I'm having problems with my back testing to debug this code. I want my EA to stop trading Thursday night a couple hours before Friday starts and not trade on Fridays. In visual mode I can see it is not working. It will some time quit at 22:00 Thursday and resume at 23:00 and then continue to trade Friday. The object is to allow it to attend a trade that is open even on Fridays but if it closes not to open another new trade. What is wrong with this code? If I only use if (DayOfWeek()==5) It will not trade Friday. But this does not give me the additional 2 hours I want. I have tried reversing the order and that did not help either. HELP!

extern bool Trade_Friday = FALSE;

if (OpenOrders<1)
{
if (!Trade_Friday)
{//check for Friday
if (Hour()>=22 && DayOfWeek()==4 || DayOfWeek()==5)
{Comment("No trading on Friday!");
return(0);//End of Friday check
}
}//continues on to process more things related to if (OpenOrders<1)

 
Bamcoti:

I'm having problems with my back testing to debug this code. I want my EA to stop trading Thursday night a couple hours before Friday starts and not trade on Fridays. In visual mode I can see it is not working. It will some time quit at 22:00 Thursday and resume at 23:00 and then continue to trade Friday. The object is to allow it to attend a trade that is open even on Fridays but if it closes not to open another new trade. What is wrong with this code? If I only use if (DayOfWeek()==5) It will not trade Friday. But this does not give me the additional 2 hours I want. I have tried reversing the order and that did not help either. HELP!


extern bool Trade_Friday = FALSE;

if (OpenOrders<1) 
{
if (!Trade_Friday)
{//check for Friday
if ((Hour()>=22 && DayOfWeek()==4) || DayOfWeek()==5)
{Comment("No trading on Friday!");
return(0);//End of Friday check 
}
}//continues on to process more things related to if (OpenOrders<1) 


Logic is the problem, from the first glance!

 
c0d3 wrote >>

extern bool Trade_Friday = FALSE;

if (OpenOrders<1)
{
if (!Trade_Friday)
{//check for Friday
if ((Hour()>=22 && DayOfWeek()==4) || DayOfWeek()==5)
{Comment("No trading on Friday!");
return(0);//End of Friday check
}
}//continues on to process more things related to if (OpenOrders<1)

Logic is the problem, from the first glance!

Thanks but I had already given that a try with no sucess.

Thanks for looking at the code I was having problems with. But I figured it out. I guess MT4 does not like you to access Hour() and DayOfWeek() multiple times in an if statement. I added a couple variables and now it works like a jewel.

v/r

MIKE

Here is what I used:

extern bool Trade_Friday = FALSE;

//-----------Time and Friday check--------------------

bool Thursday=FALSE;
bool Friday=FALSE;
if (Hour()>21 && DayOfWeek()==4)Thursday=TRUE;//--------- for some reason MT4 did not like >=22 so >21 does the same thing---------
if (DayOfWeek()==5)Friday=TRUE;


if (OpenOrders<1)
{

if (!Trade_Friday)
{//-----------check for Friday------------
if (Thursday || Friday)
{Comment("No trading on Friday!");
return(0);//--------End of Friday check-----------
}
}//-------------- More stuff for if (OpenOrders<1)---------------

 
That is interesting I have seen a similar problem on another time filter I use, There are 3 options available and one has the 22h doesn't work properly but the other two do. Obviously a problem with day and time together. Nice fix
 
Bamcoti wrote >>

I'm having problems with my back testing to debug this code. I want my EA to stop trading Thursday night a couple hours before Friday starts and not trade on Fridays. In visual mode I can see it is not working. It will some time quit at 22:00 Thursday and resume at 23:00 and then continue to trade Friday. The object is to allow it to attend a trade that is open even on Fridays but if it closes not to open another new trade. What is wrong with this code? If I only use if (DayOfWeek()==5) It will not trade Friday. But this does not give me the additional 2 hours I want. I have tried reversing the order and that did not help either. HELP!

extern bool Trade_Friday = FALSE;

if (OpenOrders<1)
{
if (!Trade_Friday)
{//check for Friday
if (Hour()>=22 && DayOfWeek()==4 || DayOfWeek()==5)
{Comment("No trading on Friday!");
return(0);//End of Friday check
}
}//continues on to process more things related to if (OpenOrders<1)

this is working in our EA's, we have different settings for "when to start on the beginning of the week", when to stop trading before weekend and also when we close all orders before weekend (CloseHour/CloseMinute)

extern string e="Weekend";
extern int StartDay=1;
extern int StartHour=3;
extern int EndDay=5;
extern int EndHour=21;
extern int CloseHour=21;
extern int CloseMinute=50;

bool Weekend=false;
bool CloseAll=false;

int start()
{
//----

if (DayOfWeek()==StartDay)
{
if (Hour()>=StartHour)
{
Weekend=false;
CloseAll=false;
}
}

if (DayOfWeek()==EndDay)
{
if (Hour()>=EndHour)
{
Weekend=true;
}
}

if (DayOfWeek()==EndDay&&CloseAll==false)
{
if (Hour()>=CloseHour&&Minute()>=CloseMinute)
{
Print("Close Orders before Weekend!");
CloseAllBuy("");
CloseAllSell("");
CloseAll=true;
}
}


if (Weekend==true)
{Comment("Wochenende -keine Trades"); return(0);}

... your stuff here

 
EADeveloper wrote >>

this is working in our EA's, we have different settings for "when to start on the beginning of the week", when to stop trading before weekend and also when we close all orders before weekend (CloseHour/CloseMinute)

extern string e="Weekend";
extern int StartDay=1;
extern int StartHour=3;
extern int EndDay=5;
extern int EndHour=21;
extern int CloseHour=21;
extern int CloseMinute=50;

bool Weekend=false;
bool CloseAll=false;

int start()
{
//----

if (DayOfWeek()==StartDay)
{
if (Hour()>=StartHour)
{
Weekend=false;
CloseAll=false;
}
}

if (DayOfWeek()==EndDay)
{
if (Hour()>=EndHour)
{
Weekend=true;
}
}

if (DayOfWeek()==EndDay&&CloseAll==false)
{
if (Hour()>=CloseHour&&Minute()>=CloseMinute)
{
Print("Close Orders before Weekend!");
CloseAllBuy("");
CloseAllSell("");
CloseAll=true;
}
}


if (Weekend==true)
{Comment("Wochenende -keine Trades"); return(0);}

... your stuff here

Nice clean aproach. Thanks for the code. It does give more options. Do not need it now but will save it for future posibilities.