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{obv} =} onbalvol (@var{closeprice}, @var{vol})
18 ## @deftypefnx {Function File} {@var{obv} =} onbalvol ([@var{closeprice} @var{vol}])
20 ## Compute the on balance volume of a security based on its closing
21 ## price (@var{closeprice}) and @var{vol}ume. They may be given as
22 ## separate arguments or as an nx2 matrix.
24 ## The output will be a column vector, and the first number in the
25 ## output is always 0.
27 ## @seealso{negvolidx, posvolidx}
30 function obv = onbalvol (c, vol)
40 obv = zeros (size (c, 1), 1);
43 obv(i) = obv(i-1) + c(i,2);
44 elseif c(i,1) < c(i-1,1)
45 obv(i) = obv(i-1) - c(i,2);
55 %! 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];
56 %! v = [10 12 23 25 34 12 32 15 15 34 54 12 86 45 32 76 89 13 28];
57 %! obv = [0 12 35 60 94 82 50 35 50 84 138 150 236 281 249 173 84 97 69]';
58 %!assert(onbalvol(c, v), obv)
59 %!assert(onbalvol([c' v']), obv)