]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/thirdwednesday.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / thirdwednesday.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} {[begindate, enddate]} = thirdwednesday (month, year)
18 ##
19 ## Find the third Wednesday of the month specified by the @var{month}
20 ## and @var{year}.  The @var{begindate} is the third Wednesday of the
21 ## month, and the @var{enddate} is three months after that.  Outputs are
22 ## in the form of datenums.
23 ##
24 ## The third Wednesday is used for Eurodollar futures.
25 ##
26 ## @seealso{nweekdate, datenum}
27 ## @end deftypefn
28
29 function [wednesdays, enddate] = thirdwednesday (month, year)
30
31   if nargin ~= 2
32         print_usage ();
33   elseif ~ ((numel(year) == 1) ||
34             (numel(month) == 1) ||
35             ~isequal (size (month), size (year)))
36     error("month and year must have the same size or one of them must be a scalar")
37   endif
38
39   if numel (year) == 1
40     sz = size (month);
41   else
42     sz = size (year);
43   endif
44
45   wednesdays = nweekdate (3, 4, year, month);
46   dates = datevec (wednesdays);
47   ## adjust the year when the date will wrap
48   dates(:,1) += dates (:,2) > 9;
49   ## adjust the month by three
50   dates(:,2) = mod (dates(:,2) + 2, 12) + 1;
51   enddate = reshape (datenum (dates), sz);
52
53 endfunction
54
55 ## Tests
56 %!shared m, y, bt, et
57 %! m = (1:12)';
58 %! y = 2008;
59 %! bt = datenum(2008, m, [16;20;19;16;21;18;16;20;17;15;19;17]);
60 %! et = datenum([2008*ones(9,1);2009*ones(3,1)], [4:12 1:3]', [16;20;19;16;21;18;16;20;17;15;19;17]);
61 %!test
62 %! [b e] = thirdwednesday (m, y);
63 %! assert(b, bt)
64 %! assert(e, et)
65 %!test
66 %! [b e] = thirdwednesday (m', y);
67 %! assert(b, bt')
68 %! assert(e, et')