App/Chart/Series/Derived/Donchian.pm

filter: Statementstmt 100% 20 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 27 / 27 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« App/Chart/Series/Derived/DMI.pm ↑ Summary App/Chart/Series/Derived/EMA.pm »
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 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Locale::TextDomain ('App-Chart');
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use base 'App::Chart::Series::Indicator';
Subroutine BEGIN: called CC=1 SCAR=0.0
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__
« App/Chart/Series/Derived/DMI.pm ↑ Summary App/Chart/Series/Derived/EMA.pm »