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

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 150 CC 4 SCAR 17.6
CC
4
Cov
0%
fill_part
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::Keltner;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use List::Util qw(min max);
23 use Locale::TextDomain 1.17; # for __p()
24 use Locale::TextDomain ('App-Chart');
25  
26 use base 'App::Chart::Series::Indicator';
27 use App::Chart::Series::Derived::SMA;
28  
29 sub longname { __('Keltner Channel') }
30 sub shortname { __('Keltner') }
31 sub manual { __p('manual-node','Keltner Channel') }
32  
33 use constant
34 { type => 'average',
35 hlines => [ 0 ],
36 parameter_info => [ { name => __('Days'),
37 key => 'keltner_days',
38 type => 'integer',
39 minimum => 1,
40 default => 10 },
41 { name => __('Width'),
42 key => 'keltner_width',
43 type => 'float',
44 default => 1.0,
45 decimals => 1,
46 step => 0.5,
47 minimum => 0 }],
48 line_colours => { upper => App::Chart::BAND_COLOUR(),
49 lower => App::Chart::BAND_COLOUR() },
50 };
51  
52 sub new {
53 my ($class, $parent, $N, $width) = @_;
54  
55 $N //= parameter_info()->[0]->{'default'};
56 ($N >= 1) || croak "Keltner bad N: $N";
57  
58 $width //= parameter_info()->[1]->{'default'};
59 ($width >= 0) || croak "Keltner bad width: $width";
60  
61 return $class->SUPER::new
62 (parent => $parent,
63 parameters => [ $N, $width ],
64 arrays => { middle => [],
65 upper => [],
66 lower => [] },
67 array_aliases => { values => 'middle' });
68 }
69  
70 sub proc {
71 my ($class_or_self, $N, $width) = @_;
72 my $sma_tp_proc = App::Chart::Series::Derived::SMA->proc($N);
73 my $sma_range_proc = App::Chart::Series::Derived::SMA->proc($N);
74  
75 return sub {
76 my ($high, $low, $close) = @_;
77 $high //= $close;
78 $low //= $close;
79 my $sma_tp = $sma_tp_proc->(($high + $low + $close) / 3);
80 my $sma_range = $width * $sma_range_proc->($high - $low);
81 return ($sma_tp, $sma_tp + $sma_range, $sma_tp - $sma_range);
82 };
83 }
84 *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1
85  
86 sub fill_part {
87 my ($self, $lo, $hi) = @_;
88 my $parent = $self->{'parent'};
89  
90 my $warmup_count = $self->warmup_count_for_position ($lo);
91 my $start = $parent->find_before ($lo, $warmup_count);
92 $parent->fill ($start, $hi);
93 my $p = $parent->values_array;
94 my $p_highs = $parent->array('highs') || $p;
95 my $p_lows = $parent->array('lows') || $p;
96  
97 my $s_middle = $self->array('middle');
98 my $s_upper = $self->array('upper');
99 my $s_lower = $self->array('lower');
100 $hi = min ($hi, $#$p);
101 if ($#$s_middle < $hi) { $#$s_middle = $hi; } # pre-extend
102 if ($#$s_upper < $hi) { $#$s_upper = $hi; } # pre-extend
103 if ($#$s_lower < $hi) { $#$s_lower = $hi; } # pre-extend
104  
105 my $proc = $self->proc(@{$self->{'parameters'}});
106  
107 foreach my $i ($start .. $lo-1) {
108 my $close = $p->[$i] // next;
109 $proc->($p_highs->[$i], $p_lows->[$i], $close);
110 }
111  
112 foreach my $i ($lo .. $hi) {
113 my $close = $p->[$i] // next;
114 ($s_middle->[$i], $s_upper->[$i], $s_lower->[$i])
115 = $proc->($p_highs->[$i], $p_lows->[$i], $close)
116 }
117 }
118  
119 1;
120 __END__