]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/movavg.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / movavg.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} {} movavg (@var{asset}, @var{lead}, @var{lag})
18 ## @deftypefnx {Function File} {} movavg (@var{asset}, @var{lead}, @var{lag}, @var{alpha})
19 ## @deftypefnx {Function File} {[@var{short}, @var{long}] =} movavg (@var{asset}, @var{lead}, @var{lag}, @var{alpha})
20 ##
21 ## Calculate the @var{lead}ing and @var{lag}ging moving average of an
22 ## @var{asset}. If given, @var{alpha} is the weighting power of the
23 ## delay; 0 (default) is the simple moving average, 0.5 would be the
24 ## square root weighted moving average, 1 would be linear, 2 would be
25 ## squared, ..., and 'e' is the exponential moving average.
26 ##
27 ## If no output is requested the data is plotted.  The plots are drawn
28 ## in the following order: asset, lag, lead.  If output is requested, no
29 ## plot is generated.
30 ##
31 ## @seealso{bolling, candle, dateaxis, highlow, pointfig}
32 ## @end deftypefn
33
34 function [varargout] = movavg (asset, lead, lag, alpha = 0)
35
36   if nargin < 3 || nargin > 4
37     print_usage ();
38   endif
39
40   if lead > lag
41     error ("lead must be <= lag")
42   elseif ischar (alpha)
43     if ! strcmpi (alpha, "e")
44       error ("alpha must be 'e' if it is a char");
45     endif
46   elseif ! isnumeric (alpha)
47     error ("alpha must be numeric or 'e'")
48   endif
49
50   ## Compute the weights
51   if ischar (alpha)
52     lead = exp(1:lead);
53     lag  = exp(1:lag);
54   else
55     lead = (1:lead).^alpha;
56     lag  = (1:lag).^alpha;
57   endif
58   ## Adjust the weights to equal 1
59   lead = lead / sum (lead);
60   lag  = lag / sum (lag);
61
62   short = asset;
63   long  = asset;
64   for i = 1:length (asset)
65     if i < length (lead)
66       ## Compute the run-in period
67       r        = length (lead) - i + 1:length(lead);
68       short(i) = dot (asset(1:i), lead(r))./sum (lead(r));
69     else
70       short(i) = dot (asset(i - length(lead) + 1:i), lead);
71     endif
72     if i < length (lag)
73       r       = length (lag) - i + 1:length(lag);
74       long(i) = dot (asset(1:i), lag(r))./sum (lag(r));
75     else
76       long(i) = dot (asset(i - length(lag) + 1:i), lag);
77     endif
78   endfor
79
80   if nargout > 0
81     varargout{1} = short;
82   else
83     plot((1:length(asset))', [asset(:), long(:), short(:)]);
84   endif
85   if nargout > 1
86     varargout{2} = long;
87   endif
88
89 endfunction
90
91 ## Tests
92 %!shared a
93 %! a = [1 2 3 2 4 2 1];
94 %!test
95 %! [s l] = movavg(a, 2, 4);
96 %! assert(s, [1 1.5 2.5 2.5 3 3 1.5])
97 %! assert(l, [1 1.5 2 2 2.75 2.75 2.25])
98 %!test
99 %! [s l] = movavg(a', 2, 4);
100 %! assert(s, [1;1.5;2.5;2.5;3;3;1.5])
101 %! assert(l, [1;1.5;2;2;2.75;2.75;2.25])
102 %!test
103 %! [s l] = movavg(a, 3, 4, 1);
104 %! assert(s, [3 4.8 7 7 9.5 8 5.5]./3, 10*eps)
105 %! assert(l, [1 11/7 20/9 2.2 3 2.7 2], 10*eps)