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

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 116 CC 4 SCAR 17.6
CC
4
Cov
0%
proc
20.0
longname
2.0
shortname
2.0
CRAP 5.8
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::FRAMAdimension;
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::SMA;
27 use App::Chart::Series::Derived::WilliamsR;
28  
29  
30 sub longname { __('FRAMA - Dimension') }
31 sub shortname { __('FRAMA dimension') }
32 sub manual { __p('manual-node','Fractal Adaptive Moving Average') }
33  
34 use constant
35 { type => 'indicator',
36 priority => -10,
37 units => 'frama_dimension',
38 minimum => 1,
39 maximum => 2,
40 parameter_info => [ { name => __('Days'),
41 key => 'frama_days',
42 type => 'integer',
43 minimum => 2,
44 default => 16 } ],
45 };
46  
47 use constant M_1_LN2 => 1.442695040888963407359924681; # 1/log(2)
48  
49 sub new {
50 my ($class, $parent, $N) = @_;
51  
52 $N //= parameter_info()->[0]->{'default'};
53  
54 return $class->SUPER::new
55 (parent => $parent,
56 parameters => [ $N ],
57 arrays => { values => [] },
58 array_aliases => { });
59 }
60 *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1
61 *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part; # HLC
62  
63 sub proc {
64 my ($class, $N) = @_;
65  
66 # @h is the high values for each day
67 # @l is the corresponding low values for each day
68 my (@h, @l);
69  
70 $N = max ($N, 2);
71 my $near_N = int ($N / 2);
72 my $far_N = $N - $near_N;
73  
74 return sub {
75 my ($high, $low, $close) = @_;
76  
77 unshift @h, $high // $close;
78 unshift @l, $low // $close;
79 if (@h > $N) {
80 pop @h;
81 pop @l;
82 }
83 if (@h < $N) { return undef; }
84  
85 my $long_avg = (max(@h) - min(@l)) / $N;
86 my $near_avg = (max(@h[0..$near_N-1]) - min(@l[0..$near_N-1]))
87 / $near_N;
88 my $far_avg = (max(@h[$near_N..$#h]) - min(@l[$near_N..$#l]))
89 / $far_N;
90  
91 if ($long_avg == 0 || $near_avg == 0 || $far_avg == 0) { return 0; }
92  
93 return M_1_LN2 * log (($near_avg + $far_avg) / $long_avg);
94 };
95 }
96  
97 1;
98 __END__