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

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 104 CC 2 SCAR 8.8
CC
2
Cov
0%
proc
6.0
longname
2.0
shortname
2.0
CRAP 2.4
1 # Copyright 2008, 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::RangeMidpoint;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use List::Util qw(min max);
23 use Scalar::Util;
24 use Locale::TextDomain ('App-Chart');
25  
26 use App::Chart::Database;
27 use App::Chart::TZ;
28 use base 'App::Chart::Series::Indicator';
29 use App::Chart::Series::Derived::SMA;
30 use App::Chart::Series::Derived::WilliamsR;
31  
32 sub longname { __('Range Midpoint') }
33 sub shortname { __('Range Mid') }
34 sub manual { __p('manual-node','Range Midpoint') }
35  
36 use constant
37 { type => 'average',
38 priority => -10,
39 parameter_info => [ { name => __('Days'),
40 key => 'range_midpoint_days',
41 type => 'integer',
42 minimum => 1,
43 default => 20 } ],
44 };
45  
46 sub new {
47 my ($class, $parent, $N) = @_;
48  
49 $N //= parameter_info()->[0]->{'default'};
50 ($N > 0) || croak "Range Midpoint bad N: $N";
51  
52 return $class->SUPER::new
53 (parent => $parent,
54 parameters => [ $N ],
55 arrays => { values => [] },
56 array_aliases => { });
57 }
58 *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1
59 *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part;
60  
61 sub proc {
62 my ($class_or_self, $N) = @_;
63 my (@H, @L);
64  
65 return sub {
66 my ($high, $low, $close) = @_;
67  
68 unshift @H, $high // $close;
69 unshift @L, $low // $close;
70 if (@H > $N) {
71 pop @H;
72 pop @L;
73 }
74 return 0.5 * (max(@H)+min(@L));
75 };
76 }
77  
78 1;
79 __END__