X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Ffinancial-0.4.0%2Fthirdwednesday.m;fp=octave_packages%2Ffinancial-0.4.0%2Fthirdwednesday.m;h=18ce73dd24012436e2cf204ac55be40b7b8a3285;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/financial-0.4.0/thirdwednesday.m b/octave_packages/financial-0.4.0/thirdwednesday.m new file mode 100644 index 0000000..18ce73d --- /dev/null +++ b/octave_packages/financial-0.4.0/thirdwednesday.m @@ -0,0 +1,68 @@ +## Copyright (C) 2008 Bill Denney +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 3 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {[begindate, enddate]} = thirdwednesday (month, year) +## +## Find the third Wednesday of the month specified by the @var{month} +## and @var{year}. The @var{begindate} is the third Wednesday of the +## month, and the @var{enddate} is three months after that. Outputs are +## in the form of datenums. +## +## The third Wednesday is used for Eurodollar futures. +## +## @seealso{nweekdate, datenum} +## @end deftypefn + +function [wednesdays, enddate] = thirdwednesday (month, year) + + if nargin ~= 2 + print_usage (); + elseif ~ ((numel(year) == 1) || + (numel(month) == 1) || + ~isequal (size (month), size (year))) + error("month and year must have the same size or one of them must be a scalar") + endif + + if numel (year) == 1 + sz = size (month); + else + sz = size (year); + endif + + wednesdays = nweekdate (3, 4, year, month); + dates = datevec (wednesdays); + ## adjust the year when the date will wrap + dates(:,1) += dates (:,2) > 9; + ## adjust the month by three + dates(:,2) = mod (dates(:,2) + 2, 12) + 1; + enddate = reshape (datenum (dates), sz); + +endfunction + +## Tests +%!shared m, y, bt, et +%! m = (1:12)'; +%! y = 2008; +%! bt = datenum(2008, m, [16;20;19;16;21;18;16;20;17;15;19;17]); +%! 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]); +%!test +%! [b e] = thirdwednesday (m, y); +%! assert(b, bt) +%! assert(e, et) +%!test +%! [b e] = thirdwednesday (m', y); +%! assert(b, bt') +%! assert(e, et')