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

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 104 CC 4 SCAR 16.9
CC
4
Cov
0%
proc
20.0
longname
2.0
shortname
2.0
CRAP 5.4
1 # Copyright 2006, 2007, 2009, 2010 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::KAMAalpha;
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::Calculation;
27 use App::Chart::Series::Derived::SMA;
28 use App::Chart::Series::Derived::EMA;
29  
30 use constant
31 { FASTEST_ALPHA => App::Chart::Series::Derived::EMA::N_to_alpha(2),
32 SLOWEST_ALPHA => App::Chart::Series::Derived::EMA::N_to_alpha(30) };
33  
34 sub longname { __('KAMA - Alpha') }
35 sub shortname { __('KAMA alpha') }
36 sub manual { __p('manual-node','Kaufman Adaptive Moving Average') }
37  
38 use constant
39 { type => 'indicator',
40 priority => -10,
41 units => 'ema_alpha',
42 minimum => SLOWEST_ALPHA ** 2,
43 maximum => FASTEST_ALPHA ** 2,
44 parameter_info => [ { name => __('Days'),
45 key => 'kama_days',
46 type => 'integer',
47 minimum => 1,
48 default => 10 } ],
49 };
50  
51 sub new {
52 my ($class, $parent, $N) = @_;
53  
54 $N //= parameter_info()->[0]->{'default'};
55 ($N > 0) || croak "KAMA bad N: $N";
56  
57 return $class->SUPER::new
58 (parent => $parent,
59 parameters => [ $N ],
60 arrays => { values => [] },
61 array_aliases => { });
62 }
63 sub warmup_count {
64 my ($self_or_class, $N) = @_;
65 return $N;
66 }
67 sub proc {
68 my ($class, $N) = @_;
69 my $sum_proc = App::Chart::Series::Calculation->sum ($N);
70 my @values;
71 return sub {
72 my ($value) = @_;
73 my $alpha;
74 if (@values) {
75 my $prev = $values[0];
76 my $this_absmove = abs ($value - $prev);
77 my $total_absmove = $sum_proc->($this_absmove);
78  
79 my $prevnth = $values[-1]; # $N days ago
80 my $net_move = abs ($value - $prevnth);
81  
82 my $ef_ratio = ($total_absmove == 0
83 ? 1 # would be 0/0
84 : $net_move / $total_absmove);
85 $alpha
86 = (SLOWEST_ALPHA + $ef_ratio * (FASTEST_ALPHA - SLOWEST_ALPHA)) ** 2;
87 # print " value=$value prev=$prev prevnth=$prevnth net_move=$net_move total_absmove=$total_absmove EF=$ef_ratio alpha=$alpha\n";
88 }
89 unshift @values, $value;
90 if (@values > $N) { # keep $N many
91 pop @values;
92 }
93 return $alpha;
94 };
95 }
96  
97 1;
98 __END__