bool flags

 

hi guys... I am writing an Ea and have a couple of questions that I hope someone is able to help me with.


I want me EA to not trade on a Sunday or a Monday (my broker has Sunday candles) so I used a bool flag like this :


if(DayOfWeek()==0 || DayOfWeek()==1)trade_allowed=false;

then i use the trade_allowed flag in the trading criteria...

When I run the Ea on tester tho it trades on a date that is a Saturday on the calender... so I am thinking that maybe my start of week candle on my platform is set wrong

can any one tell me please how to set the which candle is the start of my week on my platform? iv looked and the options and don't see a setting for it.


The other question I have is ...

I use a special function to determine if there is a new bar opened as I want it to trade once a day I use code like this :

bool NewBar()
{
   static datetime lastbar = 0;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return (true);
   }
   else
   {
      return(false);
   }
} 

then I use in the trade criteria if(NewBar()==true) {trade etc}

but when I run it in tester it skips many candles and trades only once in a while missing many profitable trades.... My question is if thats a problem with the data in the tester?

I am going to run it forward testing from the new week but am concerned about it skipping so many candles when there should be a trade everyday.


I hope someone is able to help me with these questions...sorry for the long post.

many thanx

 

mqlearner:

can any one tell me please how to set the which candle is the start of my week on my platform? iv looked and the options and don't see a setting for it.

The candles are dependent on the broker. So you need to work out from past candles where the start is. The start is obvious - it is just after where there is a weekend gap. So you need to work out the time gap between 2 candles and where the gap is biggest - that's your weekend. I prefer to use TimeLocal () for the calculations. The data will not be perfect, so you need to make some allowances.

bool NewBar()
{
   static datetime lastbar = 0;
   datetime curbar = Time[0];

Don't use Time[0] - always use iTime(NULL,PERIOD_D1,0).

Time[0] will stuff things up completely as soon as you change chart timeframe, though this doesn't quite explain the skipping of candles which is probably due to issues with other parts of your code.

Edit: One possibility - if you are sitting on the W1 or MN chart, then you will skip many many candles.

 

mqlearner:

I want me EA to not trade on a Sunday or a Monday (my broker has Sunday candles) so I used a bool flag like this :

When I run the Ea on tester tho it trades on a date that is a Saturday on the calender...
  1. if(DayOfWeek()==0 || DayOfWeek()==1)trade_allowed=false;
    Someone once reported that DOW only reported 5 in the tester. So I only use
    datetime now = Time[0],
    int      dow = TimeDayOfWeek(now);
    

  2. Are you resetting trade_allowed. I'd only use positive logic
    trade_allowed=false;
    if(dow!=0 && dow !=1) trade_allowed=true;
    
    or combined
    trade_allowed= dow!=0 && dow !=1;
    trade_allowed= dow > 1;// simplified
    

  3. To allow optimization of days, I use a bit mask
        /*++++ Day/Time allowed to open*/{
        datetime    now = TimeGMT();
        int         DOW = TimeDayOfWeek(now),   /* https://www.mql5.com/en/forum/127483
        // 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*/}
    

 

thanks to both of you for your great answers, as i am a beginner coder i will use this weekend to digest your excellent answers and try to implement them in my code...


thanx again for your courteous answers and you kind help :)

Reason: