1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
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
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
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/>.
17 ## @deftypefn {Function File} {@var{nvi} =} negvolidx (@var{closeprice}, @var{vol})
18 ## @deftypefnx {Function File} {@var{nvi} =} negvolidx ([@var{closeprice} @var{vol}])
19 ## @deftypefnx {Function File} {@var{nvi} =} negvolidx (@var{closeprice}, @var{vol}, @var{initnvi})
20 ## @deftypefnx {Function File} {@var{nvi} =} negvolidx ([@var{closeprice} @var{vol}], @var{initnvi})
22 ## Compute the negative 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{initnvi}
25 ## is the starting value of the nvi (default: 100).
27 ## The @var{nvi} will always be a column vector.
29 ## @seealso{onbalvol, posvolidx}
32 function nvi = negvolidx (c, vol, initnvi)
35 nvi = zeros (length (c), 1);
38 ## a closing price was given without a volume
41 ## probably initnvi was given as the second argument
43 elseif !isvector (vol)
45 elseif length (c) != length (vol)
46 error ("closeprice and vol must be the same length");
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
66 ## Start doing the work
68 nvi(i) = nvi(i-1) + (c(i,2) < c(i-1,2))*nvi(i-1)*(c(i,1)-c(i-1,1))/c(i-1,1);
74 %!shared c, v, nvia, nvib
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 %! nvia = [100 100 100 100 100 99.44349315 99.44349315 98.49559157 98.49559157 98.49559157 98.49559157 98.78565011 98.78565011 100.7353669 98.13574451 98.13574451 98.13574451 99.14355704 99.14355704]';
78 %! nvib = [5 5 5 5 5 4.972174658 4.972174658 4.924779578 4.924779578 4.924779578 4.924779578 4.939282505 4.939282505 5.036768344 4.906787226 4.906787226 4.906787226 4.957177852 4.957177852]';
79 %!assert(negvolidx(c, v), nvia, 1e-5)
80 %!assert(negvolidx([c' v']), nvia, 1e-5)
81 %!assert(negvolidx(c, v, 5), nvib, 1e-5)
82 %!assert(negvolidx([c' v'], 5), nvib, 1e-5)