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

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 74 CC 2 SCAR 9.3
CC
2
Cov
0%
proc
6.0
longname
2.0
shortname
2.0
CRAP 2.5
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::TrueRange;
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  
27 sub longname { __('True Range') }
28 sub shortname { __('TR') }
29 sub manual { __p('manual-node','True Range') }
30  
31 use constant
32 { type => 'indicator',
33 priority => -1000,
34 minimum => 0,
35 parameter_info => [ ],
36 };
37  
38 sub new {
39 my ($class, $parent) = @_;
40  
41 return $class->SUPER::new
42 (parent => $parent,
43 parameters => [ ],
44 arrays => { values => [] },
45 array_aliases => { });
46 }
47  
48 # proc() returns a procedure which calculates the "true range" for
49 # successive days.
50 #
51 # Each call $proc->($high, $low, $close) enters a new day into the window,
52 # and the return is the true range for that day. The first call is just the
53 # difference $high-$low as there's no previous close yet. Prime by calling
54 # with one day before the desired.
55 #
56 # $high and/or $low can be undef in each call, in which case the $close is
57 # used. $close cannot be undef. If $high and $low are always undef then
58 # the true range is just close-to-close changes (as positive values).
59 #
60 sub proc {
61 my $prev_close;
62 return sub {
63 my ($high, $low, $close) = @_;
64 $high //= $close;
65 $low //= $close;
66 if (defined $prev_close) {
67 $high = max ($high, $prev_close);
68 $low = min ($low, $prev_close);
69 }
70 $prev_close = $close;
71 return $high - $low;
72 };
73 }
74 use constant warmup_count => 1;
75  
76 1;
77 __END__