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

filter: Statementstmt 0.0% 0 / 51 Branchbran n/a Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 84 CC 1 SCAR 6.9
CC
1
Cov
0%
longname
2.0
shortname
2.0
manual
2.0
CRAP 2.0
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::StochRSI;
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::RSI;
27 use App::Chart::Series::Derived::WilliamsR;
28  
29  
30 # http://www.stockcharts.com/education/IndicatorAnalysis/indic_stochRSI.html
31 # Numerical examples, graph of DELL checked against the code here.
32 #
33  
34  
35 sub longname { __('Stochastic RSI') }
36 sub shortname { __('Stoch RSI') }
37 sub manual { __p('manual-node','Stochastic RSI') }
38  
39 use constant
40 { type => 'indicator',
41 units => 'zero_to_one',
42 minimum => 0,
43 maximum => 1,
44 hlines => [ .20, .50, .80 ],
45 parameter_info => [ { name => __('Days'),
46 key => 'stoch_rsi_days',
47 type => 'float',
48 minimum => 1,
49 default => 14,
50 decimals => 0,
51 step => 1 } ],
52 };
53  
54 sub new {
55 my ($class, $parent, $N) = @_;
56  
57 $N //= parameter_info()->[0]->{'default'};
58 ($N > 0) || croak "StochRSI bad N: $N";
59  
60 return $class->SUPER::new
61 (parent => $parent,
62 parameters => [ $N ],
63 arrays => { values => [] },
64 array_aliases => { });
65 }
66 sub warmup_count {
67 my ($class_or_self, $N) = @_;
68 return (App::Chart::Series::Derived::WilliamsR->warmup_count($N)
69 + App::Chart::Series::Derived::RSI->warmup_count($N));
70 }
71 sub proc {
72 my ($class_or_self, $N) = @_;
73 my $rsi_proc = App::Chart::Series::Derived::RSI->proc ($N);
74 my $WR_proc = App::Chart::Series::Derived::WilliamsR->proc ($N);
75  
76 return sub {
77 my ($value) = @_;
78 my $rsi = $rsi_proc->($value) // return undef;
79 my $WR = $WR_proc->(undef, undef, $rsi);
80 return 1 + $WR*0.01;
81 };
82 }
83  
84 1;
85 __END__