]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/bolling.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / bolling.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} {} bolling (@var{asset}, @var{samples})
18 ## @deftypefnx {Function File} {} bolling (@var{asset}, @var{samples}, @var{alpha})
19 ## @deftypefnx {Function File} {} bolling (@var{asset}, @var{samples}, @var{alpha}, @var{width})
20 ## @deftypefnx {Function File} {[@var{movavg}, @var{upperband}, @var{lowerband}] =} bolling (@var{asset}, @var{samples}, ...)
21 ##
22 ## If no output is requested, plot the bollinger bands of the
23 ## @var{asset}. If output is requested, return the values for the
24 ## bollinger bands. If given, @var{alpha} is the weighting power of the
25 ## moving average; 0 (default) is the simple moving average, see
26 ## @code{movavg} for the full definition.  @var{width} is the number of
27 ## standard deviations to plot above and below the moving average
28 ## (default: 2).
29 ##
30 ## @seealso{movavg, candle, dateaxis, highlow, pointfig}
31 ## @end deftypefn
32
33 function [varargout] = bolling (asset, samples, alpha, width)
34
35   ## Check input and set the defaults
36   if nargin < 2 || nargin > 4
37     print_usage ();
38   elseif nargin < 3
39     alpha = 0;
40   endif
41   if nargin < 4
42     width = 2;
43   endif
44
45   if samples > length (asset)
46     error ("Samples must be <= the length of the asset")
47   endif
48
49   ## the moving average and the standard deviation
50   avg = movavg(asset, samples, samples, alpha);
51   s   = zeros(size(avg));
52
53   ## Assume that the standard deviation is constant for the first samples
54   ## FIXME: is this what matlab assumes
55   s(1:samples) = std (asset(1:samples));
56   for i = samples+1:length (asset)
57     s(i) = std (asset(i - samples + 1:i));
58   endfor
59
60   if nargout > 0
61     varargout{1} = avg;
62   else
63     plot((1:length(avg))', [avg(:), avg(:)+s(:), avg(:)-s(:)]);
64   endif
65   if nargout > 1
66     varargout{2} = avg + s;
67   endif
68   if nargout > 2
69     varargout{3} = avg - s;
70   endif
71
72 endfunction