]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/posvolidx.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / posvolidx.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} {@var{pvi} =} posvolidx (@var{closeprice}, @var{vol})
18 ## @deftypefnx {Function File} {@var{pvi} =} posvolidx ([@var{closeprice} @var{vol}])
19 ## @deftypefnx {Function File} {@var{pvi} =} posvolidx (@var{closeprice}, @var{vol}, @var{initpvi})
20 ## @deftypefnx {Function File} {@var{pvi} =} posvolidx ([@var{closeprice} @var{vol}], @var{initpvi})
21 ##
22 ## Compute the positive volume index of a security based on its closing
23 ## price (@var{closeprice}) and @var{vol}ume.  They may be given as
24 ## separate arguments or as an nx2 matrix.  If given, the @var{initpvi}
25 ## is the starting value of the pvi (default: 100).
26 ##
27 ## The @var{pvi} will always be a column vector.
28 ##
29 ## @seealso{onbalvol, negvolidx}
30 ## @end deftypefn
31
32 function pvi = posvolidx (c, vol, initpvi)
33
34   default_pvi = 100;
35   pvi         = zeros (length (c), 1);
36   if isvector (c)
37     if nargin < 2
38       ## a closing price was given without a volume
39       print_usage ();
40     elseif isscalar (vol)
41       ## probably initpvi was given as the second argument
42       print_usage ();
43     elseif !isvector (vol)
44       print_usage ();
45     elseif length (c) != length (vol)
46       error ("closeprice and vol must be the same length");
47     endif
48     c = [c(:) vol(:)];
49     if nargin < 3
50       pvi(1) = default_pvi;
51     else
52       pvi(1) = initpvi;
53     endif
54   elseif size (c, 2) != 2
55     error ("If given as a matrix, c must have exactly two columns.")
56   elseif size (c, 2) == 2
57     if nargin == 2
58       pvi(1) = vol;
59     else
60       pvi(1) = default_pvi;
61     endif
62   else
63     print_usage ();
64   endif
65
66   ## Start doing the work
67   for i = 2:size (c, 1)
68     pvi(i) = pvi(i-1) + (c(i,2) > c(i-1,2))*pvi(i-1)*(c(i,1)-c(i-1,1))/c(i-1,1);
69   endfor
70
71 endfunction
72
73 ## Tests
74 %!shared c, v, pvia, pvib
75 %! c = [22.44 22.61 22.67 22.88 23.36 23.23 23.08 22.86 23.17 23.69 23.77 23.84 24.32 24.8 24.16 24.1 23.37 23.61 23.21];
76 %! v = [10 12 23 25 34 12 32 15 15 34 54 12 86 45 32 76 89 13 28];
77 %! pvia = [100 100.7575758 101.0249554 101.9607843 104.0998217 104.0998217 103.4276318 103.4276318 103.4276318 105.7488389 106.1059477 106.1059477 108.242309 108.242309 108.242309 107.9734953 104.7029289 104.7029289 102.9290546]';
78 %! pvib = [5 5.037878788 5.051247772 5.098039216 5.204991087 5.204991087 5.171381588 5.171381588 5.171381588 5.287441943 5.305297383 5.305297383 5.412115451 5.412115451 5.412115451 5.398674767 5.235146444 5.235146444 5.14645273]';
79 %!assert(posvolidx(c, v), pvia, 1e-5)
80 %!assert(posvolidx([c' v']), pvia, 1e-5)
81 %!assert(posvolidx(c, v, 5), pvib, 1e-5)
82 %!assert(posvolidx([c' v'], 5), pvib, 1e-5)