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

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 133 CC 5 SCAR 17.3
CC
5
Cov
0%
proc_original
20.0
proc
6.0
longname
2.0
CRAP 5.6
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::RVI;
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 use App::Chart::Series::Derived::Stddev;
28 use App::Chart::Series::Derived::WilliamsR;
29  
30  
31 # http://www.fmlabs.com/reference/RVI.htm
32 # Formula, some description.
33 #
34 # http://trader.online.pl/MSZ/e-w-Relative_Volatility_Index_RVI.html
35 # Reproducing equis.com original 1993 version, adding sample of Kredyt
36 # Bank (which is not in Yahoo, or only as .PK pink sheets).
37 # http://trader.online.pl/MSZ/e-w-Inertia.html
38 # 1995 revision.
39 #
40 # http://www.equis.com/customer/resources/formulas/formula.aspx?Id=52
41 # Original 1993.
42 #
43 # http://store.traders.com/-v11-c06-therela-pdf.html
44 # TASC 1993.
45 #
46 # http://store.traders.com/-v13-c09-refinin-pdf.html
47 # TASC 1995, high/low combination, and introducing Inertia.
48 #
49 # http://store.traders.com/v1340tradtip.html
50 # TASC traders tips for sale.
51 #
52  
53 sub longname { __('RVI - Relative Volatility Index') }
54 sub shortname { __('RVI') }
55 sub manual { __p('manual-node','Relative Volatility Index') }
56  
57 use constant
58 { hlines => [ 40, 50, 60 ],
59 type => 'indicator',
60 units => 'percentage',
61 minimum => 0,
62 maximum => 100,
63 parameter_info => [ { name => __('Stddev Days'),
64 key => 'rvi_stddev_days',
65 type => 'integer',
66 minimum => 1,
67 default => 10 },
68 { name => __('Smooth Days'),
69 key => 'rvi_smooth_days',
70 type => 'float',
71 minimum => 1,
72 default => 14,
73 decimals => 0,
74 step => 1 }],
75 };
76  
77 sub new {
78 my ($class, $parent, $N_stddev, $N_smooth) = @_;
79  
80 $N_stddev //= parameter_info()->[0]->{'default'};
81 ($N_stddev > 0) || croak "RVI bad N_stddev: $N_stddev";
82  
83 $N_smooth //= parameter_info()->[1]->{'default'};
84 ($N_smooth > 0) || croak "RVI bad N_smooth: $N_smooth";
85  
86 return $class->SUPER::new
87 (parent => $parent,
88 parameters => [ $N_stddev, $N_smooth ],
89 arrays => { values => [] },
90 array_aliases => { });
91 }
92 *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part;
93  
94 sub warmup_count {
95 my ($class_or_self, $N_stddev, $N_smooth) = @_;
96 $N_smooth = App::Chart::Series::Derived::EMA::N_from_Wilder_N($N_smooth);
97 return (App::Chart::Series::Derived::Stddev->warmup_count($N_stddev)
98 + App::Chart::Series::Derived::EMA->warmup_count($N_smooth));
99 }
100  
101 # Return a procedure which calculates a relative volatility index, using
102 # Dorsey's original 1993 definition, over an accumulated window.
103 #
104 sub proc_original {
105 my ($class_or_self, $N_stddev, $N_smooth) = @_;
106 $N_smooth = App::Chart::Series::Derived::EMA::N_from_Wilder_N($N_smooth);
107  
108 my $stddev_proc = App::Chart::Series::Derived::Stddev->proc ($N_stddev);
109 my $num_proc = App::Chart::Series::Derived::EMA->proc ($N_smooth);
110 my $den_proc = App::Chart::Series::Derived::EMA->proc ($N_smooth);
111 my $prev;
112  
113 return sub {
114 my ($value) = @_;
115 my $stddev = $stddev_proc->($value);
116 my $rvi;
117 if (defined $prev) {
118 my $num = $num_proc->($value > $prev ? $stddev : 0);
119 my $den = $den_proc->($stddev);
120 $rvi = ($den == 0 ? 50 : 100 * $num/$den);
121 }
122 $prev = $value;
123 return $rvi;
124 };
125 }
126  
127 # Return a procedure which calculates a relative volatility index, using
128 # Dorsey's 1995 revised definition, over an accumulated window.
129 #
130 sub proc {
131 my ($class_or_self, $N_stddev, $N_smooth) = @_;
132 my $high_proc = $class_or_self->proc_original($N_stddev, $N_smooth);
133 my $low_proc = $class_or_self->proc_original($N_stddev, $N_smooth);
134  
135 return sub {
136 my ($high, $low, $close) = @_;
137 my $h_rvi = $high_proc->($high // $close);
138 my $l_rvi = $low_proc-> ($low // $close);
139 return (defined $h_rvi ? ($h_rvi+$l_rvi)/2 : undef);
140 };
141 }
142  
143 1;
144 __END__