week of the month

 
how can i get the week of the month?
 

Tell her you're going out with your mates.

If the answer is "have a lovely time dear", then its week 2 or 3.

If the answer is "really? we'll see about that", then its week 1 or 4.


CB

 

int week_of_month=MathFloor(Day()/7);

or something similar ....

very difficult calculation ...

 
meikel:

int week_of_month=MathFloor(Day()/7);

or something similar ....

very difficult calculation ...


i have no luck trying to calculate getting week of the month

i notice my EA is more profitable towards end on the month therefore try to see if i can code week of the month and run some backtesting to support my claims.

 
doshur:


i have no luck trying to calculate getting week of the month

i notice my EA is more profitable towards end on the month therefore try to see if i can code week of the month and run some backtesting to support my claims.

??? are you sure that you want to bet your money on your programming skills ?????

besides that, programming has nothing to do with luck...

extern int first_day_of_trading=21; //for optimisation 1-31


start()

{

if (Day()<first_day_of_trading) return();

}

 
//+------------------------------------------------------------------+
//| Function..: TimeWeekOfMonth()
//| Parameters: tTime - Datetime is the number of seconds elapsed
//| since midnight (00:00:00), January 1, 1970.
//| Returns...: iWeek - Week index position in the month (1=1st,
//| 2=2nd, 3=3rd, 4=4th, 5=last) of the day of
//| week of parameter <tTime>.
//| Sample....: int i=TimeWeekOfMonth(D'2009.01.08') // 2nd Thursday
//+------------------------------------------------------------------+
int TimeWeekOfMonth(datetime t) {
int iDOW=TimeDayOfWeek(t), iMonth=TimeMonth(t), iWeek=1;
while(TimeMonth(t) == iMonth) {
t=t-PERIOD_W1*60;
iWeek++;
}
return(iWeek);
}
 
sxTed:
//+------------------------------------------------------------------+
//| Function..: TimeWeekOfMonth()
//| Parameters: tTime - Datetime is the number of seconds elapsed
//| since midnight (00:00:00), January 1, 1970.
//| Returns...: iWeek - Week index position in the month (1=1st,
//| 2=2nd, 3=3rd, 4=4th, 5=last) of the day of
//| week of parameter <tTime>.
//| Sample....: int i=TimeWeekOfMonth(D'2009.01.08') // 2nd Thursday
//+------------------------------------------------------------------+
int TimeWeekOfMonth(datetime t) {
int iDOW=TimeDayOfWeek(t), iMonth=TimeMonth(t), iWeek=1;
while(TimeMonth(t) == iMonth) {
t=t-PERIOD_W1*60;
iWeek++;
}
return(iWeek);
}


thanks
 

oops! I thought it was too easy!

#property show_inputs

extern string date = "2009.11.21";
extern bool FixedWeek = true;
extern int DayOfWeekWhenWeekStarts = 1;

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
  Print("date=",date," FixedWeek=",FixedWeek," DayOfWeekWhenWeekStarts=",DayOfWeekWhenWeekStarts," TimeWeekOfMonth=",TimeWeekOfMonth(StrToTime(date),FixedWeek,DayOfWeekWhenWeekStarts));
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Function..: TimeWeekOfMonth()                                    |
//| Parameters: tTime  - Datetime is the number of seconds elapsed   |
//|                      since midnight (00:00:00), January 1, 1970. |
//|             iType  - Set to 0 (zero) for Dynamic Week Number to  |
//|                      be returned. A dynamic week means that the  |
//|                      week starts on the first day of the month.  |
//|                      Sometimes there may be 6 weeks in the month |
//|                      when using a dynamic week, for example: the |
//|                      month's of March, August and November 2009. | 
//|                      Set to 1 (one) for the function to return   |
//|                      the Fixed Week Number. Fixed week means that|
//|                      the week can be set from Monday to Sunday or|
//|                      Saturday to Friday or Sunday to Saturday.   |
//|             iStart - The Day (0-Sunday,1,2,3,4,5,6) when the     |
//|                      Dynamic or Fixed Week starts.               |
//| Purpose...: Obtain the Current Week Number of the Month.         |
//| Returns...: iWeek - Week index position in the month (1=1st,     |
//|                     2=2nd, 3=3rd, 4=4th, 5=last) of the day of   |
//|                     week of parameter <tTime>.                   |
//| Samples...: int i=TimeWeekOfMonth(D'2009.01.08')       // 2      |
//|             int i=TimeWeekOfMonth(D'2009.01.08', 1, 1) // 1      |
//+------------------------------------------------------------------+
int TimeWeekOfMonth(datetime t, int iType=0, int iStart=1) {
  int iMonth=TimeMonth(t), iWeek, iDaysInWeek1, iDaysLeft;
  
  if(iType==0) {
    datetime t1 = StrToTime(""+TimeYear(t)+"."+iMonth+".1");
    iWeek=1;
    while(TimeDayOfWeek(t1) != iStart) {
      iDaysInWeek1++;
      t1=t1+PERIOD_D1*60;
    }
    if(TimeDay(t) <= iDaysInWeek1) return(iWeek);
    iDaysLeft = TimeDay(t) - iDaysInWeek1;
    while(iDaysLeft > 0) {
      iDaysLeft-=7;
      if(iDaysLeft > 0) iWeek++;
    }
    return(iWeek+1);
  }
  else {
    while(TimeMonth(t) == iMonth) {
      if(TimeDayOfWeek(t) == iStart) iWeek++;
      t=t-PERIOD_D1*60;
    }
  }
  return(iWeek);
}
 
sxTed:

oops! I thought it was too easy!


Thanks

I have not tested the code yet but I really appreciate. the help

 

after some thoughts

this is the code i came up with

i have not tested whether it works or not

//+------------------------------------------------------------------+
//| Week Of Month |
//+------------------------------------------------------------------+
int WeekOfMonth()
{
int WeekOfMonth = 1;
int Total_Days = Day() - DayOfWeek();

while(Total_Days > 0)
{
WeekOfMonth++;
Total_Days -= 7;
}

return(WeekOfMonth);
}

 
// call
// WeekOfMonth(TimeDay()):

int WeekOfMonth(int timeDay){
   if(timeDay >= 28) return(5);
   if(timeDay >= 21) return(4);
   if(timeDay >= 14) return(3);
   if(timeDay >= 7)  return(2);
   if(timeDay >= 0)  return(1);
}