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

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 145 CC 4 SCAR 13.4
CC
4
Cov
0%
fill_part
12.0
name
6.0
longname
2.0
CRAP 3.8
1 # Copyright 2004, 2005, 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::Stochastics;
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 { __('Stochastics %K/%D') }
31 sub shortname { __('%R') }
32 sub manual { __p('manual-node','Stochastics') }
33  
34 use constant
35 { type => 'indicator',
36 units => 'percentage',
37 minimum => 0,
38 maximum => 100,
39 hlines => [ 20, 80 ],
40 parameter_info => [ { name => __('%K Days'),
41 key => 'stochastics_K_days',
42 type => 'integer',
43 minimum => 1,
44 default => 14 },
45 { name => __('%D Days'),
46 key => 'stochastics_D_days',
47 type => 'integer',
48 minimum => 1,
49 default => 3 },
50 { name => __('Slow Days'),
51 key => 'stochastics_slow_days',
52 type => 'integer',
53 minimum => 0,
54 default => 0 }],
55  
56 line_colours => { K => App::Chart::DOWN_COLOUR(),
57 D => App::Chart::UP_COLOUR() },
58 };
59  
60 sub new {
61 my ($class, $parent, $K_count, $D_count, $slow_count) = @_;
62  
63 $K_count //= parameter_info()->[0]->{'default'};
64 $D_count //= parameter_info()->[1]->{'default'};
65 $slow_count //= parameter_info()->[2]->{'default'};
66  
67 return $class->SUPER::new
68 (parent => $parent,
69 parameters => [ $K_count, $D_count, $slow_count ],
70 arrays => { K => [],
71 D => [] },
72 array_aliases => { values => 'K' });
73 }
74  
75 sub name {
76 my ($self) = @_;
77 my ($K_count, $D_count, $slow_count) = @{$self->{'parameters'}};
78 if ($slow_count == 0) {
79 return __x('%K {K_count}, %D {D_count}, no slow',
80 K_count => $K_count,
81 D_count => $D_count);
82 } else {
83 return __x('%K {K_count}, %D {D_count}, slow {slow_count}',
84 K_count => $K_count,
85 D_count => $D_count,
86 slow_count => $slow_count);
87 }
88 }
89  
90 sub warmup_count {
91 my ($self_or_class, $K_count, $D_count, $slow_count) = @_;
92 return $K_count
93 + max (1, $slow_count) - 1
94 + $D_count - 1;
95 }
96  
97 sub proc {
98 my ($class_or_self, $K_count, $D_count, $slow_count) = @_;
99 my $williams_proc = App::Chart::Series::Derived::WilliamsR->proc($K_count);
100 my $slow_proc = App::Chart::Series::Derived::SMA->proc($slow_count);
101 my $sma_proc = App::Chart::Series::Derived::SMA->proc($D_count);
102  
103 return sub {
104 my ($high, $low, $close) = @_;
105  
106 my $K = $slow_proc->($williams_proc->(@_) + 100); # 0 to 100
107 my $D = $sma_proc->($K);
108 return ($K, $D);
109 };
110 }
111  
112 sub fill_part {
113 my ($self, $lo, $hi) = @_;
114 my $parent = $self->{'parent'};
115  
116 my $warmup_count = $self->warmup_count_for_position ($lo);
117 my $start = $parent->find_before ($lo, $warmup_count);
118 $parent->fill ($start, $hi);
119 my $p = $parent->values_array;
120 my $ph = $parent->array('highs') || $p;
121 my $pl = $parent->array('lows') || $p;
122  
123 my $sk = $self->array('K');
124 my $sd = $self->array('D');
125 $hi = min ($hi, $#$p);
126 if ($#$sk < $hi) { $#$sk = $hi; } # pre-extend
127 if ($#$sd < $hi) { $#$sd = $hi; } # pre-extend
128  
129 my $proc = $self->proc(@{$self->{'parameters'}});
130  
131 foreach my $i ($start .. $lo-1) {
132 my $close = $p->[$i] // next;
133 $proc->($ph->[$i], $pl->[$i], $close);
134 }
135 foreach my $i ($lo .. $hi) {
136 my $close = $p->[$i] // next;
137 ($sk->[$i], $sd->[$i]) = $proc->($ph->[$i], $pl->[$i], $close);
138 }
139 }
140  
141 1;
142 __END__