]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/onbalvol.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / onbalvol.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{obv} =} onbalvol (@var{closeprice}, @var{vol})
18 ## @deftypefnx {Function File} {@var{obv} =} onbalvol ([@var{closeprice} @var{vol}])
19 ##
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.
23 ##
24 ## The output will be a column vector, and the first number in the
25 ## output is always 0.
26 ##
27 ## @seealso{negvolidx, posvolidx}
28 ## @end deftypefn
29
30 function obv = onbalvol (c, vol)
31
32   if nargin == 1
33     % do nothing
34   elseif nargin == 2
35     c = [c(:) vol(:)];
36   else
37     print_usage ();
38   endif
39
40   obv = zeros (size (c, 1), 1);
41   for i = 2:size (c, 1)
42     if c(i,1) > c(i-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);
46     else
47       obv(i) = obv(i-1);
48     endif
49   endfor
50
51 endfunction
52
53 ## Tests
54 %!shared c, v, obv
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)