]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/busdate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / busdate.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} {b =} busdate (refdate)
18 ## @deftypefnx {Function File} {b =} busdate (refdate, direction)
19 ## @deftypefnx {Function File} {b =} busdate (refdate, direction, holiday)
20 ## @deftypefnx {Function File} {b =} busdate (refdate, direction, holiday, weekend)
21 ##
22 ## Return the datenum of the next or previous business day from
23 ## @var{refdate}. @var{direction} indicates the next day (default) if 1
24 ## and the previous day if -1.  @var{holiday} is a vector of datenums
25 ## that defines the holidays observed (the holidays function is used if
26 ## not given).  @var{weekend} defines the days of the week that should
27 ## be considered weekends; [1 0 0 0 0 0 1] (default) indicates that
28 ## Sunday and Saturday are holidays.
29 ##
30 ## If any of the optional inputs (@var{direction}, @var{holiday},
31 ## @var{weekend}) are empty, then the default is used.
32 ##
33 ## @seealso{holidays, lbusdate, isbusday, fbusdate}
34 ## @end deftypefn
35
36 function rd = busdate (rd, d, hol, wkend)
37
38   if ~isnumeric (rd)
39         rd = datenum ( rd);
40   endif
41   if nargin < 2 || isempty (d)
42         d = 1;
43   elseif ~ all (abs (d) == 1)
44         ## People could use other numbers to skip days, but that is not
45         ## supported.
46         error ("directions must all be either 1 or -1.")
47   endif
48   if nargin < 3
49         hol = [];
50   end
51   if nargin < 4
52         wkend = [];
53   elseif nargin > 4
54         print_usage ();
55   endif
56
57   rd += d;
58   mask = ~isbusday (rd, hol, wkend);
59   while any (mask)
60         ## Only recompute for the days that are not yet business days
61         if isscalar (d)
62           rd(mask) += d;
63         else
64           rd(mask) += d(mask);
65         endif
66         mask(mask) = ~isbusday (rd(mask), hol, wkend);
67   endwhile
68
69 endfunction
70
71 ## Tests
72 ## A normal day
73 %!assert(busdate(datenum(2008,1,2)), datenum(2008,1,3))
74 ## A holiday
75 %!assert(busdate(datenum(2007,12,31)), datenum(2008,1,2))
76 ## Go over a weekend and start in a weekend
77 %!assert(busdate(datenum(2007,1,5)), datenum(2007,1,8))
78 %!assert(busdate(datenum(2007,1,6)), datenum(2007,1,8))
79 ## Backward
80 %!assert(busdate(datenum(2008,1,3), -1), datenum(2008,1,2))
81 ## Backward holiday
82 %!assert(busdate(datenum(2008,1,2), -1), datenum(2007,12,31))
83 ## Backward with alternate holidays
84 %!assert(busdate(datenum(2008,1,2), -1, datenum(2007,1,1):datenum(2008,1,1)), datenum(2006,12,29))
85 ## Multiple dates in both orientations
86 %!assert(busdate([datenum(2008,1,2) datenum(2007,1,1)]), [datenum(2008,1,3) datenum(2007,1,2)])
87 %!assert(busdate([datenum(2008,1,2) datenum(2007,1,1)], [1 1]), [datenum(2008,1,3) datenum(2007,1,2)])
88 %!assert(busdate([datenum(2008,1,2) datenum(2007,1,1)], 1), [datenum(2008,1,3) datenum(2007,1,2)])
89 %!assert(busdate([datenum(2008,1,2);datenum(2007,1,1)], [1;1]), [datenum(2008,1,3);datenum(2007,1,2)])
90 ## Multiple dates with opposite directions holidays and weekends
91 %!assert(busdate([datenum(2008,1,2);datenum(2007,1,2)], [1;-1]), [datenum(2008,1,3);datenum(2006,12,29)])
92 ## Alternate weekends
93 %!assert(busdate(datenum(2008,1,2), 1, holidays(datenum(2008,1,1), datenum(2008,1,31)), [1 0 0 0 0 0 0]), datenum(2008,1,3))
94 %!assert(busdate(datenum(2008,1,4), 1, holidays(datenum(2008,1,1), datenum(2008,1,31)), [1 0 0 0 0 0 0]), datenum(2008,1,5))
95 %!assert(busdate(datenum(2008,1,5), 1, holidays(datenum(2008,1,1), datenum(2008,1,31)), [1 0 0 0 0 0 0]), datenum(2008,1,7))
96 %!assert(busdate(datenum(2008,1,6), 1, holidays(datenum(2008,1,1), datenum(2008,1,31)), [1 0 0 0 0 0 0]), datenum(2008,1,7))
97 %!assert(busdate(datenum(2008,1,1), 1, holidays(datenum(2008,1,1), datenum(2008,1,31)), [1 1 1 1 1 1 0]), datenum(2008,1,5))