week of the month - page 2

 
phy wrote >>

What about Oct 5, 2009?

 
Roger wrote >>

What about Oct 5, 2009?

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
  
  int time = D'2009.10.5';
  MessageBox("For " + TimeToStr(time, TIME_DATE) + " Week is " +WeekOfMonth(TimeDay(time)));
  time = D'2009.10.11';
  MessageBox("For " + TimeToStr(time, TIME_DATE) + " Week is " +WeekOfMonth(TimeDay(time)));
  time = D'2009.10.17';
  MessageBox("For " + TimeToStr(time, TIME_DATE) + " Week is " +WeekOfMonth(TimeDay(time)));
  time = D'2009.10.21';
  MessageBox("For " + TimeToStr(time, TIME_DATE) + " Week is " +WeekOfMonth(TimeDay(time)));
  time = D'2009.10.30';
  MessageBox("For " + TimeToStr(time, TIME_DATE) + " Week is " +WeekOfMonth(TimeDay(time)));

   return(0);
  }
//+------------------------------------------------------------------+


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);
}
 

Standard ISO 8601 week (http://en.wikipedia.org/wiki/ISO_8601#Week_dates):


int StdWeek()
   {
   int iDay  = ( DayOfWeek() + 6 ) % 7 + 1,                    // convert day to standard index (1=Mon,...,7=Sun)
       iWeek = ( DayOfYear() - iDay + 10 ) / 7;                // calculate standard week number

   return(iWeek);
   }


Note that if returns 0, then that week is part of previous year. There are 52-53 weeks in a year, depending on the year... See -> http://en.wikipedia.org/wiki/ISO_8601#Week_dates

 
phy wrote >>

In your script Oct 1 and Oct 5 are in the same week.

 

What are we trying to do here?

I didn't consider that the second day of the month might start the second week.

S - S M T W T F S - S M T W T F S - S M T W T F S - S M T W T F S - S M

Does the 31 day month above have 6 weeks?

Overall, it's a goofy topic, like, what's the point?

May as well argue which time zone gives the REAL 4hr bars

 
I think cloudbreaker nailed it right at the start...
 
phy:

Phy,


If date is 07 Aug 2009 what does it show?

Im trying to get the first trading week of the month

(i need to block trade on friday of the first trading week)

 
doshur:

(i need to block trade on friday of the first trading week)

If this effectively means "the first Friday of the month" then a much simpler route would be to check "TimeDayOfWeek(TimeCurrent()) != 5 || TimeDay(TimeCurrent()) > 7"

 

hmmm...


let me try


if(DayOfWeek() == 5 && Day() <= 7) Print("BLOCK");


Is this correct?

but can it run in tester mode?

 
doshur:

if(DayOfWeek == 5 && Day <= 7) Trade == false

Is this correct?

Yes, if that's what you're trying to do...


Unless you want to ignore partial weeks (i.e. only count weeks beginning from the first Monday of the month), then the first Friday of the month must be in the first trading week. And the first Friday of the month has to be the 7th day or earlier of the month.


EDIT: note that there's a difference here between "week" and "trading week" (and you specified the latter above). Using your earlier example, August 7th falls in the first trading week of the month, because August 1st falls on a Saturday when the markets are closed (in most timezones). But, if you're counting something like Sunday-to-Sunday calendar weeks, then August 7th falls in the second week. And, under this definition, there isn't a Friday in the first week of the month for you to block trading on.


but can it run in tester mode?

I haven't tried it, but I can't see any reason why not. The Day() and DayOfWeek() functions return simulated time when backtested, not current real time.