How to get current month working day in MQL4?

 

Hi,

How to get current month working day in MQL4?

int CurrentWorkDay()
{

  int WorkDay = 0;
  for (int i=Day(); i >= 1; i--) {
     if( TimeDayOfWeek(TimeToStr(TimeCurrent() - 60 * 60 * 24 * i,TIME_DATE)) >= 1 && TimeDayOfWeek(TimeToStr(TimeCurrent() - 60 * 60 * 24 * i,TIME_DATE)) <= 5 ) {
         WorkDay++;

         Print("WorkDay = " + Day()  + " = " + TimeToStr(TimeCurrent() - 60 * 60 * 24 * i,TIME_DATE) + " = " + TimeDayOfWeek(TimeToStr(TimeCurrent() - 60 * 60 * 24 * i,TIME_DATE))  + " = " + WorkDay );
     }
  }
             
   return WorkDay;      
} 

Print result:


It should be 7 working day (not 9 as Print result above) and should be from 2022.09.01 (not from 2022.08.31) 

How to fix it?

Please help

 
wieb: How to get current month working day in MQL4?
  1. Day() should be nine (9) as last trading date, Friday, was Sep 9.

  2.      if( TimeDayOfWeek(TimeToStr(TimeCurrent() - 60 * 60 * 24 * i,TIME_DATE)) >= 1 && TimeDayOfWeek(TimeToStr(TimeCurrent() - 60 * 60 * 24 * i,TIME_DATE)) <= 5 ) {
             WorkDay++;
    
             Print("WorkDay = " + Day()  + " = " + TimeToStr(TimeCurrent() - 60 * 60 * 24 * i,TIME_DATE) + " = " + TimeDayOfWeek(TimeToStr(TimeCurrent() - 60 * 60 * 24 * i,TIME_DATE))  + " = " + WorkDay );
    

    Are your books one column but two feet wide? No because that is unreadable. They are 6 inches, sometimes two columns, so you can read it easily. So should be your code. I'm not going to go scrolling (or moving my eyes) back and forth trying to read it. Don't copy and paste code, write self documenting code.

  3. Don't copy and paste code. Call your functions once and save the result in variables.

  4. Why are you converting to a string. TimeDayOfWeek doesn't take a string as an parameter.

  5. You have to count today also.

  6.   int WorkDay = 0;
         datetime now=TimeCurrent();
      for (int i=Day(); i >= 1; i--) {
         datetime then=now - 86400*(i-1);
         int dow=TimeDayOfWeek(then);
         if( dow >= 1 && dow <= 5 ) {
             WorkDay++;
    
             Print("WorkDay = " + Day()  + " = " + (string)then + " = " + dow  + " = " + WorkDay );
         }
      }
    
  7. wieb: It should be
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.09 23:58:02 = 5 = 7
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.08 23:58:02 = 4 = 6
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.07 23:58:02 = 3 = 5
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.06 23:58:02 = 2 = 4
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.05 23:58:02 = 1 = 3
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.02 23:58:02 = 5 = 2
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.01 23:58:02 = 4 = 1
 

1. Day() function return 9 as last date on MT4 Terminal was Friday, September 9 2022

2. I am not copy and paste code, that is myself code. Sorry if it looks like copy paste, but I am just beginner

3. Until September 9 2022 should be 7 working day, not 9 working day

4. I am trying to get how many working day on current month which is should be 7 working day (not 9)

5. Sorry if you have scroll to right corner. Edit the code by removing the Print command

int CurrentWorkDay()
{

  int WorkDay = 0;

  for (int i=Day(); i >= 1; i--) {

     if( TimeDayOfWeek(TimeToStr(TimeCurrent() - 60 * 60 * 24 * i,TIME_DATE)) >= 1 
		&& TimeDayOfWeek(TimeToStr(TimeCurrent() - 60 * 60 * 24 * i,TIME_DATE)) <= 5 ) {

         WorkDay++;
     }

  }
             
   return WorkDay;      
} 


Please help how to fix the code

 
wieb #: Please help how to fix the code

I was still editing my post (#1).

 
William Roeder #:
  1. Day() should be nine (9) as last trading date, Friday, was Sep 9.

  2. 2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.09 23:58:02 = 5 = 7
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.08 23:58:02 = 4 = 6
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.07 23:58:02 = 3 = 5
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.06 23:58:02 = 2 = 4
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.05 23:58:02 = 1 = 3
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.02 23:58:02 = 5 = 2
    2022.09.10 08:24:12.848    testscr USDSGD,Daily: WorkDay = 9 = 2022.09.01 23:58:02 = 4 = 1


It works perfectly

Thank a lot @William Roeder for the help and kindness