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

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 128 CC 3 SCAR 13.3
CC
3
Cov
0%
fill_part
12.0
longname
2.0
shortname
2.0
CRAP 3.8
1 # Copyright 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::ElderPower;
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::EMA;
27  
28 # http://www.incrediblecharts.com/technical/elder_ray_index.php
29 # Formula, sample All Ords (yahoo ^AORD) from Aug 2001.
30 #
31 # http://www.prophet.net/analyze/popglossary.jsp?studyid=ELDR
32 # Formula, sample Hilton Hotels (HLT) from 2002/2003, weekly scale.
33 #
34  
35 sub longname { __('Elder Bull/Bear Power') }
36 sub shortname { __('Elder Power') }
37 sub manual { __p('manual-node','Elder Bull/Bear Power') }
38  
39 use constant
40 { type => 'indicator',
41 units => 'momentum',
42 hlines => [ 0 ],
43 parameter_info => [ { name => __('Days'),
44 key => 'elder_power_days',
45 type => 'integer',
46 minimum => 1,
47 default => 13 } ],
48 line_colours => { bull => App::Chart::UP_COLOUR(),
49 bear => App::Chart::DOWN_COLOUR() },
50 };
51  
52 sub new {
53 my ($class, $parent, $N) = @_;
54  
55 $N //= parameter_info()->[0]->{'default'};
56 ($N > 0) || croak "ElderPower bad N: $N";
57  
58 return $class->SUPER::new
59 (parent => $parent,
60 parameters => [ $N ],
61 arrays => { bull => [],
62 bear => [] },
63 array_aliases => { values => 'bull' });
64 }
65 *warmup_count = \&App::Chart::Series::Derived::EMA::warmup_count;
66  
67 sub proc {
68 my ($class_or_self, $N) = @_;
69 my $ema_proc = App::Chart::Series::Derived::EMA->proc($N);
70 return sub {
71 my ($high, $low, $close) = @_;
72 my $ema = $ema_proc->($close);
73 $high //= $close;
74 $low //= $close;
75 return ($high - $ema, $low - $ema);
76 };
77 }
78 sub fill_part {
79 my ($self, $lo, $hi) = @_;
80 my $parent = $self->{'parent'};
81  
82 my $warmup_count = $self->warmup_count_for_position ($lo);
83 my $start = $parent->find_before ($lo, $warmup_count);
84 $parent->fill ($lo, $hi);
85 my $p = $parent->values_array;
86 my $ph = $parent->array('highs') || $p;
87 my $pl = $parent->array('lows') || $p;
88  
89 my $su = $self->array('bull');
90 my $sl = $self->array('bear');
91 $hi = min ($hi, $#$p);
92 if ($#$su < $hi) { $#$su = $hi; } # pre-extend
93 if ($#$sl < $hi) { $#$sl = $hi; } # pre-extend
94  
95 my $proc = $self->proc(@{$self->{'parameters'}});
96  
97 foreach my $i ($start .. $lo-1) {
98 my $value = $p->[$i] // next;
99 $proc->($ph->[$i], $pl->[$i], $value);
100 }
101 foreach my $i ($lo .. $hi) {
102 my $value = $p->[$i] // next;
103 ($su->[$i], $sl->[$i]) = $proc->($ph->[$i], $pl->[$i], $value);
104 }
105 }
106  
107  
108 1;
109 __END__