Number of working day between 2 dates

 

Hello everyone,

I'm stuck with a (simple) problem concerning the calculation between 2 dates.

For example :

Today is the 30th of November. I would like to count the number of working days (not Saturday and Sunday) between September 17th and today with MQL4

Any idea ?

Thank you !

 
Hugh:

Hello everyone,

I'm stuck with a (simple) problem concerning the calculation between 2 dates.

For example :

Today is the 30th of November. I would like to count the number of working days (not Saturday and Sunday) between September 17th and today with MQL4

Any idea ?

Thank you !

How would you do it with a pen and paper?

Write the code to do exactly the same.

 
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. 42137: I would like to count the number of working days (not Saturday and Sunday)
    So just count them. What's the problem?
    Not Compiled, not tested.
    #define  SECONDS  uint
    #define  HR2400 86400 // = 24 * 3600 = 1440 * 60 = PeriodSeconds(PERIOD_D1)
    SECONDS     time(datetime when=0){        if(when == 0) when = TimeCurrent();
       return SECONDS(when % HR2400);
    }
    datetime    date(datetime when=0){        if(when == 0) when = TimeCurrent();
       return datetime(when - time(when) );
    }
    uint        count_weekdays(datetime begin, datetime last=0){  // inclusive.
       if(last == 0)  last = TimeCurrent();
       begin = date(begin);    last  = date(last);
       uint  count = 0;
       while(begin <= last){
          ENUM_DAY_OF_WEEK  dow   = (ENUM_DAY_OF_WEEK)TimeDayOfWeek(last);
          if(dow > SUNDAY && dow < SATURDAY)  ++count;
          last -= HR2400;
       }
       return count;
    }
    
    Not Compiled, not tested.
 

You could just count the number of bars on D1:

datetime dfrom=D'17.9.2019';
datetime dtill=TimeCurrent();
int days=iBarShift(NULL,PERIOD_D1,dfrom) - iBarShift(NULL,PERIOD_D1,dtill);

This only counts true workdays.

Documentation on MQL5: Timeseries and Indicators Access / Bars
Documentation on MQL5: Timeseries and Indicators Access / Bars
  • www.mql5.com
If the start_time and stop_time parameters are defined, the function returns the number of bars in the specified time interval, otherwise it returns the total number of bars. If data for the timeseries with specified parameters are not formed in the terminal by the time of the Bars() function call, or data of the timeseries are not synchronized...
 

also

int nbDays = Bars(_Symbol,PERIOD_D1,D'2019.09.17',D'2019.11.30');
 
lippmaje:

You could just count the number of bars on D1:

This only counts true workdays.

  1. On MT4: Unless the current chart is that specific symbols/TFs referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

  2. Not what OP asked for. Fails if your broker's timezone has short Sunday or Saturday trading.
    Chart times are broker times.
 
William Roeder:
  1. On MT4: Unless the current chart is that specific symbols/TFs referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

  2. Not what OP asked for. Fails if your broker's timezone has short Sunday or Saturday trading.

1. Of course, not to be called in OnInit, but a script would do fine.

2. Yes, to avoid the Sunday bars you need to choose a symbol that's not traded on that day. Major index such as Dow should do it. The neat thing about this approach is that you don't need to count public holidays. I guess that's what OP was looking for.

@Paul nice!
 
lippmaje: 1. Of course, not to be called in OnInit, but a script would do fine.
  1. Not unless you "handle 4066/4073 errors before accessing"
  2. Your code breaks depending on the broker. Your code is broken and isn't what OP wanted.
 
William Roeder:
  1. Not unless you "handle 4066/4073 errors before accessing"
  2. Your code breaks depending on the broker. Your code is broken and isn't what OP wanted.

1. yup

2. The topic is about workdays, not weekdays. So until OP comes back and clarifies, we could also say that your code is not what OP was asking for.

 
lippmaje:

1. yup

2. The topic is about workdays, not weekdays. So until OP comes back and clarifies, we could also say that your code is not what OP was asking for.

Hugh:

Hello everyone,

I'm stuck with a (simple) problem concerning the calculation between 2 dates.

For example :

Today is the 30th of November. I would like to count the number of working days (not Saturday and Sunday) between September 17th and today with MQL4

Any idea ?

Thank you !

I think that the OP was clear enough.

 

Hello Everyone,

Thanks for all your answers.

My question was about worksday (So Just Monday, Tuesday, Wednesday,Thursday and Friday only).

Counting the number of bars doesn't work well because of the sunday opening, as William noticed.

I'm working on Forex so maybe by counting the number of bars with an other asset class as suggested lippmaje... I'll check

Reason: