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

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 97 CC 2 SCAR 8.8
CC
2
Cov
0%
proc_laguerre_and_alpha
6.0
longname
2.0
shortname
2.0
CRAP 2.4
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::AdaptiveLaguerre;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use List::Util qw(min max);
23 use Locale::TextDomain ('App-Chart');
24  
25 use base 'App::Chart::Series::Indicator';
26 use App::Chart::Series::Derived::LaguerreFilter;
27 use App::Chart::Series::Derived::Median;
28 use App::Chart::Series::Derived::WilliamsR;
29  
30  
31 # http://www.mesasoftware.com/technicalpapers.htm
32 # http://www.mesasoftware.com/Papers/TIME%20WARP.pdf
33 # Paper by John Elhers.
34 #
35 # http://www.mesasoftware.com/seminars.htm
36 # http://www.mesasoftware.com/Seminars/TradeStation%20World%2005.pdf
37 # http://www.mesasoftware.com/Seminars/Seminars/TSWorld05.ppt
38 # (View the powerpoint with google.)
39 # Powerpoint summary by John Ehlers of several of his and other averages.
40 # View in google,
41 # * A Laguerre filter warps time in the filter coefficients
42 # - Enables extreme smoothing with just a few filter terms
43 # * A NonLinear Laguerre filter measures the difference between the
44 # current price and the last computed filter output.
45 # - Objective is to drive this "error" to zero
46 # - The "error", normalized to the error range over a selected period
47 # is the alpha of the Laguerre filter
48 #
49  
50  
51 sub longname { __('Adaptive Laguerre Filter') }
52 sub shortname { __('Adaptive Laguerre') }
53 sub manual { __p('manual-node','Adaptive Laguerre Filter') }
54  
55 use constant
56 { type => 'average',
57 parameter_info => [ { name => __('Days'),
58 key => 'adaptive_laguerre_filter_days',
59 type => 'integer',
60 minimum => 1,
61 default => 20 } ],
62 };
63  
64 sub new {
65 my ($class, $parent, $N) = @_;
66  
67 $N //= parameter_info()->[0]->{'default'};
68 ($N >= 1) || croak "Adaptive Laguerre Filter bad N: $N";
69  
70 return $class->SUPER::new
71 (parent => $parent,
72 parameters => [ $N ],
73 arrays => { values => [] },
74 array_aliases => { });
75 }
76  
77 sub proc {
78 my ($class, $N) = @_;
79 my $proc_laguerre_and_alpha = $class->proc_laguerre_and_alpha($N);
80 return sub {
81 return ($proc_laguerre_and_alpha->(@_))[0];
82 };
83 }
84 sub proc_laguerre_and_alpha {
85 my ($class, $N) = @_;
86  
87 my $laguerre_proc
88 = App::Chart::Series::Derived::LaguerreFilter->proc_for_alpha();
89 my $williams_proc = App::Chart::Series::Derived::WilliamsR->proc($N);
90 my $median_proc = App::Chart::Series::Derived::Median->proc(5);
91 my $alpha = 0.2;
92 my $prev;
93  
94 return sub {
95 my ($value) = @_;
96 if (defined $prev) {
97 my $w = $williams_proc->(undef, undef, abs ($value - $prev));
98 $alpha = $median_proc->(0.01 * ($w + 100)); # 0 to 1
99 }
100 return (($prev = $laguerre_proc->($value, $alpha)),
101 $alpha);
102 };
103 }
104  
105  
106 # warmup_count() gives a fixed amount, based on the worst-case EMA alphas
107 # all the slowest possible. It ends up being 1656 which is hugely more than
108 # needed in practice.
109 #
110 # warmup_count_for_position() calculates a value on actual data, working
111 # backwards. In practice it's as little as about 100.
112 #
113 sub warmup_count {
114 my ($self_or_class, $N) = @_;
115  
116 # FIXME: this is a big over-estimate
117 return $N + App::Chart::Series::Derived::LaguerreFilter->warmup_count(0.01);
118 }
119  
120 ### AdaptiveLaguerre warmup_count(): __PACKAGE__->warmup_count(parameter_info()->[0]->{'default'})
121  
122 1;
123 __END__