## Copyright (C) 2008 Bill Denney ## ## This software 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 software 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 software; see the file COPYING. If not, see ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- ## @deftypefn {Function File} {pvi} = posvolidx (closeprice, vol) ## @deftypefnx {Function File} {pvi} = posvolidx ([closeprice vol]) ## @deftypefnx {Function File} {pvi} = posvolidx (closeprice, vol, initpvi) ## @deftypefnx {Function File} {pvi} = posvolidx ([closeprice vol], initpvi) ## ## Compute the positive volume index of a security based on its closing ## price (@var{closeprice}) and @var{vol}ume. They may be given as ## separate arguments or as an nx2 matrix. If given, the @var{initpvi} ## is the starting value of the pvi (default: 100). ## ## The @var{pvi} will always be a column vector. ## ## @seealso{onbalvol, negvolidx} ## @end deftypefn ## Author: Bill Denney <bill@denney.ws> ## Created: 24 Feb 2008 function pvi = posvolidx (c, vol, initpvi) default_pvi = 100; pvi = zeros (length (c), 1); if isvector (c) if nargin < 2 ## a closing price was given without a volume print_usage (); elseif isscalar (vol) ## probably initpvi was given as the second argument print_usage (); elseif ~isvector (vol) print_usage (); elseif length (c) ~= length (vol) error ("closeprice and vol must be the same length"); endif c = [c(:) vol(:)]; if nargin < 3 pvi(1) = default_pvi; else pvi(1) = initpvi; endif elseif size (c, 2) ~= 2 error ("If given as a matrix, c must have exactly two columns.") elseif size (c, 2) == 2 if nargin == 2 pvi(1) = vol; else pvi(1) = default_pvi; endif else print_usage (); endif ## Start doing the work for i = 2:size (c, 1) 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); endfor endfunction ## Tests %!shared c, v, pvia, pvib %! c = ; %! v = ; %! pvia = '; %! 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]'; %!assert(posvolidx(c, v), pvia, 1e-5) %!assert(posvolidx([c' v']), pvia, 1e-5) %!assert(posvolidx(c, v, 5), pvib, 1e-5) %!assert(posvolidx([c' v'], 5), pvib, 1e-5)