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

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 93 CC 3 SCAR 13.3
CC
3
Cov
0%
proc
12.0
longname
2.0
shortname
2.0
CRAP 3.8
1 # Copyright 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::Donchian;
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::Keltner;
27  
28  
29 # http://www.linnsoft.com/tour/techind/donch.htm
30 # Sample Intel (INTC) July-September 2001 (year not marked, but is 2001).
31 #
32  
33 sub longname { __('Donchian Channel') }
34 sub shortname { __('Donchian') }
35 sub type { 'average' }
36 sub manual { __p('manual-node','Donchian Channel') }
37  
38 use constant
39 { parameter_info => [ { name => __('Days'),
40 key => 'donchian_days',
41 type => 'integer',
42 minimum => 1,
43 default => 20 } ],
44 line_colours => { upper => App::Chart::BAND_COLOUR(),
45 lower => App::Chart::BAND_COLOUR() },
46 };
47  
48 sub new {
49 my ($class, $parent, $N) = @_;
50  
51 $N //= parameter_info()->[0]->{'default'};
52 ($N > 0) || croak "Donchian bad N: $N";
53  
54 return $class->SUPER::new
55 (parent => $parent,
56 parameters => [ $N ],
57 arrays => { middle => [],
58 upper => [],
59 lower => [] },
60 array_aliases => { values => 'middle' });
61 }
62  
63 sub proc {
64 my ($class_or_self, $N) = @_;
65 my @highs;
66 my @lows;
67  
68 return sub {
69 my ($high, $low, $close) = @_;
70  
71 # without the new day
72 my $upper = max (@highs);
73 my $lower = min (@lows);
74 my $middle = (defined $upper && defined $lower
75 ? ($upper + $lower) / 2
76 : undef);
77  
78 unshift @highs, $high//$close;
79 unshift @lows, $low //$close;
80 if (@highs > $N) {
81 pop @highs;
82 pop @lows;
83 }
84 return ($middle, $upper, $lower);
85 };
86 }
87 *warmup_count = \&App::Chart::Series::Derived::Keltner::warmup_count; # $N-1
88 *fill_part = \&App::Chart::Series::Derived::Keltner::fill_part;
89  
90 1;
91 __END__
92  
93 # =head1 NAME
94 #
95 # App::Chart::Series::Derived::Donchian -- Donchian channel
96 #
97 # =head1 SYNOPSIS
98 #
99 # my $series = $parent->Donchian($N);
100 #
101 # =head1 DESCRIPTION
102 #
103 # ...
104 #
105 # =head1 SEE ALSO
106 #
107 # L<App::Chart::Series>
108 #
109 # =cut