App/Chart/Series/Derived/MedianAverage.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 120 CC 4 SCAR 16.4
CC
4
Cov
0%
proc_average_and_alpha
20.0
longname
2.0
shortname
2.0
CRAP 5.1
1 # Copyright 2007, 2009, 2010, 2011 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Chart. If not, see <http://www.gnu.org/licenses/>.
16  
17 package App::Chart::Series::Derived::MedianAverage;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use List::Util qw(min max);
23 use POSIX ();
24 use Locale::TextDomain ('App-Chart');
25  
26 use base 'App::Chart::Series::Indicator';
27 use App::Chart::Series::Derived::EMA;
28 use App::Chart::Series::Derived::TMA;
29  
30  
31 # http://www.mesasoftware.com/technicalpapers.htm
32 # http://www.mesasoftware.com/Papers/What's%20the%20Difference.exe
33 # http://web.archive.org/web/20070720222047/http://www.mesasoftware.com/Papers/What%27s+the+Difference.exe
34 # Original gone, use archive.org.
35 # John Ehler's paper. Sample chart of something unspecified.
36 #
37  
38 sub longname { __('Median-Average Adaptive') }
39 sub shortname { __('Median-Average') }
40 sub manual { __p('manual-node','Median-Average Adaptive Filter') }
41  
42 use constant
43 { type => 'average',
44 parameter_info => [ ],
45 };
46  
47 sub new {
48 my ($class, $parent) = @_;
49  
50 return $class->SUPER::new
51 (parent => $parent,
52 parameters => [ ],
53 arrays => { values => [] },
54 array_aliases => { });
55 }
56 use constant warmup_count =>
57 (App::Chart::Series::Derived::TMA->warmup_count(4)
58 + 39 # lookback for median
59 + App::Chart::Series::Derived::EMA->warmup_count(39)); # slowest smoothing
60 ### MedianAverage warmup_count(): warmup_count()
61  
62 sub proc {
63 my ($class) = @_;
64 my $proc_average_and_alpha = $class->proc_average_and_alpha;
65 return sub {
66 return ($proc_average_and_alpha->(@_))[0];
67 };
68 }
69  
70 my @alpha_array
71 = map {App::Chart::Series::Derived::EMA::N_to_alpha($_)} (0 .. 39);
72 use constant THRESHOLD => 0.002;
73  
74 sub proc_average_and_alpha {
75 my ($class) = @_;
76 my $smooth_proc = App::Chart::Series::Derived::TMA->proc (4);
77  
78 # last 39 smoothed input values
79 my @values;
80  
81 # $prev is the previous median-average value calculated. $prev is
82 # initialized to the first SMOOTHed input. Ehler's code and some
83 # of the Trader's tips show an initial zero, which makes the filter rise
84 # up from zero at the start of the data. Since each alpha determined
85 # depends on the previous value there's no way to chop off an infinite
86 # sequence like an ordinary EMA. Using the first smoothed should be
87 # reasonable, it won't take too long for the medians to start moving and
88 # the average tracking towards that.
89 #
90 my $prev;
91  
92 return sub {
93 my ($value) = @_;
94 my $alpha;
95  
96 my $smooth = $smooth_proc->($value);
97 $prev //= $smooth; # initial
98  
99 unshift @values, $smooth;
100 if (@values > 39) { pop @values; }
101  
102 my $len = round_down_odd (scalar @values);
103 for (;;) {
104 my @sorted = sort @values[0 .. $len-1];
105 my $median = $sorted[$len/2];
106  
107 $alpha = $alpha_array[$len];
108 my $average = $smooth * $alpha + $prev * (1 - $alpha);
109  
110 my $ratio = ($median == 0 ? 0 : abs($median-$average) / $median);
111 if ($ratio <= THRESHOLD || $len <= 3) {
112 $prev = $average;
113 return ($average, $alpha);
114 }
115  
116 $len -= 2;
117 }
118 };
119 }
120  
121 sub round_down_odd {
122 my ($x) = @_;
123 return 2 * POSIX::floor (($x-1) / 2) + 1;
124 }
125  
126 1;
127 __END__