]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/holidays.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / holidays.m
1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {h =} holidays (startdate, enddate)
18 ##
19 ## Return a vector of datenums that were holidays between
20 ## @var{startdate} and @var{enddate}, inclusive.  These holidays are
21 ## trading holidays observed by the NYSE according to its rule 51.10. It
22 ## does not take into account the exceptions for "unusual business
23 ## conditions" or for additional days that have been called as holidays
24 ## for one-time purposes.
25 ##
26 ## The complete list can be found at
27 ## http://www.chronos-st.org/NYSE_Observed_Holidays-1885-Present.html
28 ##
29 ## @seealso{busdate, lbusdate, isbusday, fbusdate}
30 ## @end deftypefn
31
32 function hol = holidays (sd, ed)
33
34   sd = datenum (datevec (sd));
35   ed = datenum (datevec (ed));
36
37   ## just get the start and end years and generate all holidays in that range
38   yrs = year(sd):year(ed);
39
40   hol = [];
41   ## New Year's Day
42   tmphol = datenum (yrs, 1, 1);
43   hol = [hol; tmphol(:)];
44   ## Martin Luther King Day, the third Monday in January
45   tmphol = nweekdate (3, 2, yrs, 1);
46   hol = [hol; tmphol(:)];
47   ## Washington's Birthday, the third Monday in February
48   tmphol = nweekdate (3, 2, yrs, 2);
49   hol = [hol; tmphol(:)];
50   ## Good Friday
51   tmphol = easter (yrs) - 2;
52   hol = [hol; tmphol(:)];
53   ## Memorial Day, the last Monday in May
54   tmphol = lweekdate (2, yrs, 5);
55   hol = [hol; tmphol(:)];
56   ## Independence Day, July 4
57   tmphol = datenum (yrs, 7, 4);
58   hol = [hol; tmphol(:)];
59   ## Labor Day, the first Monday in September
60   tmphol = nweekdate (1, 2, yrs, 9);
61   hol = [hol; tmphol(:)];
62   ## Thanksgiving Day, the fourth Thursday in November
63   tmphol = nweekdate (4, 5, yrs, 11);
64   hol = [hol; tmphol(:)];
65   ## Christmas Day
66   tmphol = datenum (yrs, 12, 25);
67   hol = [hol; tmphol(:)];
68
69   ## Adjust for Saturdays and Sundays
70   wd = weekday (hol);
71   if any (wd == 1)
72     hol(wd == 1) = hol(wd == 1) + 1;
73   endif
74   if any (wd == 7)
75     hol(wd == 7) = hol(wd == 7) - 1;
76   endif
77
78   ## Trim out the days that are not in the date range
79   hol(hol > ed | hol < sd) = [];
80   hol = sort (hol);
81
82 endfunction
83
84 ## Tests
85 %!assert(holidays(datenum(2008,1,1), datenum(2008,12,31)), datenum(2008*ones(9,1), [1;1;2;3;5;7;9;11;12], [1;21;18;21;26;4;1;27;25]))
86 ## Test Independence day observing on a Monday (July 5) and Christmas
87 ## observing on a Friday (Dec 24)
88 %!assert(holidays(datenum(2004,1,1), datenum(2004,12,31)), datenum(2004*ones(9,1), [1;1;2;4;5;7;9;11;12], [1;19;16;9;31;5;6;25;24]))
89 %!assert(holidays(datenum(2008,3,5), datenum(2008,3,8)), zeros(0,1))
90 %!assert(holidays(datenum(2008,3,5), datenum(2008,3,5)), zeros(0,1))
91 %!assert(holidays(datenum(2008,1,1), datenum(2008,1,1)), datenum(2008,1,1))