[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re: Recognising a new month starting



PureBytes Links

Trading Reference Links

Adrian,

>Same problem as I mentioned to Bob....ok for backtesting, but doesn't
>work for a Paintbar showing in advance that the nxt bar wil be a new
>trading month.  

This isn't hard to do but the code isn't trivial either.  On any
given day, you need to determine if the next calendar day is a valid
trading day, taking into account leap years in February, as well as
holidays.

One problem is, most of the trading holidays have floating dates,
such as Martin Luther King Day (3rd Monday in January), President's
Day (3rd monday in February), Good Friday (Friday before the 3rd
Sunday after the first New Moon after Christmas, or something
complicated like that), Memorial Day (last Monday in May), Labor
Day (first Monday in September), and Thanksgiving (4th Thursday in
November).  Even New Year's Day, which has a fixed date, is observed
on 2 January if 1 January falls on a Sunday.

I'm going to write the following off the top of my head.  The logic
is sound and it should work, but be warned it's untested, and it
ignores holidays.

=======================================================================
{Function: NextBarMonth - return the month of the next bar.
 by Alex Matulich 6 Jan 2005, Unicorn Research Corporation
 This functions assumes the bar resolution is daily.
 Weekends are accounted for but holidays are ignored.}

var: yr(0), mo(0), dy(0), lastday(0), leap(0);
array: days[12](0);

if currentbar = 1 then begin
    days[1] = 31;  days[2] = 28;  days[3] = 31;  days[4] = 30;
    days[5] = 31;  days[6] = 30;  days[7] = 31;  days[8] = 31;
    days[9] = 30;  days[10] = 31; days[11] = 30; days[12] = 31;
end;

yr = 1900 + year(date);

if mod(yr,400) = 0 or (mod(yr,4) = 0 and mod(yr,100) <> 0)
 then leap=1 else leap=0;

yr = yr - 1900;
mo = month(date);
dy = DayOfMonth(date) + 1;                         {incr to tomorrow}
lastday = iff(mo = 2, days[2]+leap, days[mo]);
if dy > lastday then  {check if last day of month}
   mo = mod(mo,12)+1
else if DayOfWeek(yr*10000+mo*100+dy)>5 then begin {if saturday}
   dy = dy + 1;                                    {incr to sunday}
   if dy >= lastday then
      mo = mod(mo,12)+1
   else begin
      dy = dy + 1;                                 {incr to monday}
      if dy >= lastday then
         mo = mod(mo,12)+1;
   end;
end;
NextBarMonth = mo;
=======================================================================

-- 
  ,|___    Alex Matulich -- alex@xxxxxxxxxxxxxx
 // +__>   Director of Research and Development
 //  \ 
 // __)    Unicorn Research Corporation -- http://unicorn.us.com