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} {} 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}, ...)
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
30 ## @seealso{movavg, candle, dateaxis, highlow, pointfig}
33 function [varargout] = bolling (asset, samples, alpha, width)
35 ## Check input and set the defaults
36 if nargin < 2 || nargin > 4
45 if samples > length (asset)
46 error ("Samples must be <= the length of the asset")
49 ## the moving average and the standard deviation
50 avg = movavg(asset, samples, samples, alpha);
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));
63 plot((1:length(avg))', [avg(:), avg(:)+s(:), avg(:)-s(:)]);
66 varargout{2} = avg + s;
69 varargout{3} = avg - s;