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

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 124 CC 4 SCAR 17.2
CC
4
Cov
0%
fill_part
20.0
longname
2.0
shortname
2.0
CRAP 5.6
1 # Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2011 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::Bollinger;
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::Calculation;
28 use App::Chart::Series::Derived::SMA;
29  
30  
31 sub longname { __('Bollinger Bands') }
32 sub shortname { __('Bollinger') }
33 sub manual { __p('manual-node','Bollinger Bands') }
34  
35 use constant
36 { type => 'average',
37 parameter_info => [ { name => __('Days'),
38 key => 'bollinger_days',
39 type => 'integer',
40 minimum => 1,
41 default => 20 },
42 { name => __('Stddevs'),
43 key => 'bollinger_stddevs',
44 type => 'float',
45 default => 2.0,
46 decimals => 2,
47 step => 0.1,
48 minimum => 0 }],
49 line_colours => { upper => App::Chart::BAND_COLOUR(),
50 lower => App::Chart::BAND_COLOUR() },
51 };
52  
53 sub new {
54 my ($class, $parent, $N, $stddev_factor) = @_;
55 ### Bollinger new(): @_
56  
57 $N //= parameter_info()->[0]->{'default'};
58 ($N > 0) || croak "Bollinger bad N: $N";
59  
60 $stddev_factor //= parameter_info()->[1]->{'default'};
61  
62 return $class->SUPER::new
63 (parent => $parent,
64 parameters => [ $N, $stddev_factor ],
65 arrays => { middle => [],
66 upper => [],
67 lower => [] },
68 array_aliases => { values => 'middle' });
69 }
70  
71 sub proc {
72 my ($class_or_self, $N, $stddev_factor) = @_;
73 my $sma_stddev_proc = App::Chart::Series::Calculation->sma_and_stddev($N);
74  
75 return sub {
76 my ($value) = @_;
77 my ($sma, $stddev) = $sma_stddev_proc->($value);
78 $stddev *= $stddev_factor;
79 return ($sma, $sma + $stddev, $sma - $stddev);
80 };
81 }
82 *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1
83  
84 sub fill_part {
85 my ($self, $lo, $hi) = @_;
86 my $parent = $self->{'parent'};
87  
88 my $warmup_count = $self->warmup_count_for_position ($lo);
89 my $start = $parent->find_before ($lo, $warmup_count);
90  
91 $parent->fill ($start, $hi);
92 my $p = $parent->values_array;
93  
94 my $s_middle = $self->array('middle');
95 my $s_upper = $self->array('upper');
96 my $s_lower = $self->array('lower');
97 $hi = min ($hi, $#$p);
98 if ($#$s_middle < $hi) { $#$s_middle = $hi; } # pre-extend
99 if ($#$s_upper < $hi) { $#$s_upper = $hi; } # pre-extend
100 if ($#$s_lower < $hi) { $#$s_lower = $hi; } # pre-extend
101  
102 my $proc = $self->proc(@{$self->{'parameters'}});
103  
104 foreach my $i ($start .. $lo-1) {
105 my $value = $p->[$i] // next;
106 $proc->($value);
107 }
108 foreach my $i ($lo .. $hi) {
109 my $value = $p->[$i] // next;
110 ($s_middle->[$i], $s_upper->[$i], $s_lower->[$i]) = $proc->($value);
111 }
112 }
113  
114 1;
115 __END__