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

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 75 CC 3 SCAR 13.5
CC
3
Cov
0%
proc
12.0
longname
2.0
shortname
2.0
CRAP 3.9
1 # Copyright 2008, 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::ROC;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use Locale::TextDomain 1.17; # for __p()
23 use Locale::TextDomain ('App-Chart');
24  
25 use base 'App::Chart::Series::Indicator';
26 use App::Chart::Series::Derived::Momentum;
27  
28 sub longname { __('ROC - Rate of Change') }
29 sub shortname { __('ROC') }
30 sub manual { __p('manual-node','Momentum and Rate of Change') }
31  
32 use constant
33 { type => 'indicator',
34 units => 'roc_percent',
35 hlines => [ 0 ],
36 parameter_info => App::Chart::Series::Derived::Momentum::parameter_info(),
37 };
38  
39 sub new {
40 my ($class, $parent, $N) = @_;
41  
42 return $class->SUPER::new
43 (parent => $parent,
44 N => $N,
45 parameters => [ $N ],
46 arrays => { values => [] });
47 }
48 sub proc {
49 my ($self_or_class, $N) = @_;
50  
51 # $a[0] is the newest point, $a[1] the prev, through to $a[$N-1]
52 my @a;
53 my $pos = $N-1; # initial pre-extends
54  
55 return sub {
56 my ($value) = @_;
57 my $prev = $a[$pos];
58 $a[$pos] = $value;
59 if (++$pos >= $N) { $pos = 0; }
60  
61 if (defined $prev && $prev != 0) {
62 return 100 * ($value - $prev) / $prev;
63 } else {
64 return undef;
65 }
66 };
67 }
68 *warmup_count = \&App::Chart::Series::Derived::Momentum::warmup_count; # $N-1
69  
70 1;
71 __END__