Code required to find the second monday of each month

 

Hi Guys

I can't figure out how to calculate the second Monday of each month. I have an ea that performs a check on the second Monday of each month. I want to run this ea at any time during the month, before or after the second Monday but it must perform a certain check on the second Monday.

What would be the best method to work out the day of the second Monday of each month?

 

It is a little bit long but it works.

 

bool Is2ndMonday(datetime dt)
{
   MqlDateTime dtStr;
   TimeToStruct(dt,dtStr);
   
   bool bResult = false;
   
   int countMonday = 0;
   
   for (int d = 1; d <= dtStr.day; d++)
   {
      string testStr = dtStr.year + "." + dtStr.mon + "." + IntegerToString(d);
      datetime testDate = StringToTime(testStr);
      
      MqlDateTime testDateStr;
      TimeToStruct(testDate,testDateStr);
      
      // Note: Day of week (0-Sunday, 1-Monday, ... ,6-Saturday)
      if (testDateStr.day_of_week == 1) countMonday++;
   }
   
   if (countMonday == 2) bResult = true;
   
   return (bResult);
}
 
How about first Friday of the month for NFP?
 
dtk-forex:

Hi Guys

I can't figure out how to calculate the second Monday of each month. I have an ea that performs a check on the second Monday of each month. I want to run this ea at any time during the month, before or after the second Monday but it must perform a certain check on the second Monday.

What would be the best method to work out the day of the second Monday of each month?

bool IsSecondMondayOfTheMonth(datetime _date_)
  {
   MqlDateTime dt;
   TimeToStruct(_date_,dt);
   if(dt.day_of_week==1 && dt.day>7 && dt.day<15)
      return(true);
   else
      return(false);
  }
 
doshur:
How about first Friday of the month for NFP?
bool IsFirstFridayOfTheMonth(datetime _date_)
  {
   MqlDateTime dt;
   TimeToStruct(_date_,dt);
   if(dt.day_of_week==5 && dt.day<8)
      return(true);
   else
      return(false);
  }
 
angevoyageur:
Nice and short. ;-)
 
ROMAN5:
Nice and short. ;-)
Thank you. Also it can easily be generalized for each Xth weekday.
 
angevoyageur:
Thank you. I try that.
 

Thanks guys but this will not work if the day is say the 25th of the month and I want to be able to find out on what day of the month that the second Monday of this month was on...

 
dtk-forex:

Thanks guys but this will not work if the day is say the 25th of the month and I want to be able to find out on what day of the month that the second Monday of this month was on...

This is not what you ask in your OP.

Your EA is always running at a given date, you can then check if this date is the second Monday of the month. Let your EA run all time, and each new day check if it's 2nd Monday...

Trying to know the date of the second Monday of a month is an other problem.

Reason: