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

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 90 CC 2 SCAR 9.2
CC
2
Cov
0%
proc
6.0
longname
2.0
manual
2.0
CRAP 2.5
1 # Copyright 2006, 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::RAVI;
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::Derived::SMA;
28  
29 sub longname { __('RAVI') }
30 *shortname = \&longname;
31 sub manual { __p('manual-node','RAVI') }
32  
33 use constant
34 { type => 'indicator',
35 units => 'units',
36 minimum => 0,
37 hlines => [ 3 ],
38 parameter_info => [ { name => __('Short Days'),
39 key => 'ravi_short_days',
40 type => 'integer',
41 minimum => 1,
42 default => 7 },
43 { name => __('Long Days'),
44 key => 'ravi_long_days',
45 type => 'integer',
46 minimum => 1,
47 default => 65 }],
48 };
49  
50 sub new {
51 my ($class, $parent, $short_N, $long_N) = @_;
52  
53 $short_N //= parameter_info()->[0]->{'default'};
54 ($short_N > 0) || croak "RAVI bad short N: $short_N";
55  
56 $long_N //= parameter_info()->[1]->{'default'};
57 ($long_N > 0) || croak "RAVI bad long N: $long_N";
58  
59 return $class->SUPER::new
60 (parent => $parent,
61 parameters => [ $short_N, $long_N ],
62 arrays => { values => [] },
63 array_aliases => { });
64 }
65 sub warmup_count {
66 my ($class_or_self, $short_N, $long_N) = @_;
67 return App::Chart::Series::Derived::SMA->warmup_count
68 (max ($short_N, $long_N));
69 }
70 sub proc {
71 my ($self_or_class, $short_N, $long_N) = @_;
72 my $short_proc = App::Chart::Series::Derived::SMA->proc ($short_N);
73 my $long_proc = App::Chart::Series::Derived::SMA->proc ($long_N);
74 return sub {
75 my ($value) = @_;
76 my $short = $short_proc->($value);
77 my $long = $long_proc->($value);
78 return ($long == 0 ? undef : 100 * abs($short-$long) / $long);
79 };
80 }
81  
82 1;
83 __END__