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

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 101 CC 5 SCAR 17.9
CC
5
Cov
0%
ulcer_on_aref
20.0
proc
6.0
longname
2.0
CRAP 6.0
1 # Copyright 2006, 2007, 2009, 2010, 2015 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::Ulcer;
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::SMA;
28  
29  
30 # http://www.tangotools.com/ui/ui.htm
31 # Description by creator Peter Martin.
32 #
33 # http://www.tangotools.com/misc/index.html
34 # Peter Martin's home page.
35 #
36 # http://www.investopedia.com/articles/technical/03/030403.asp
37 # Sample chart of XOM.
38 #
39 # http://trader.online.pl/MSZ/e-w-Ulcer_Index.html
40 # Believe HHV described is not right, want highest up to each point in
41 # the calculation, not of whole period.
42 #
43  
44  
45 sub longname { __('Ulcer Index') }
46 sub shortname { __('Ulcer') }
47 sub manual { __p('manual-node','Ulcer Index') }
48  
49 use constant
50 { type => 'indicator',
51 units => 'percentage',
52 minimum => 0,
53 parameter_info => [ { name => __('Days'),
54 key => 'ulcer_days',
55 type => 'integer',
56 minimum => 1,
57 # don't know a good default, this is arbitrary
58 default => 60 }],
59 };
60  
61 sub new {
62 my ($class, $parent, $N) = @_;
63  
64 $N //= parameter_info()->[0]->{'default'};
65 ($N > 0) || croak "Ulcer bad N: $N";
66  
67 return $class->SUPER::new
68 (parent => $parent,
69 parameters => [ $N ],
70 arrays => { values => [] },
71 array_aliases => { });
72 }
73 *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1
74  
75 sub ulcer_on_aref {
76 my ($aref) = @_;
77 if (@$aref <= 1) { return 0; }
78 my $high = $aref->[-1];
79 my $sumsq = 0;
80 for (my $i = $#$aref - 1; $i >= 0; $i--) {
81 my $value = $aref->[$i];
82 if ($value >= $high) {
83 $high = $value;
84 } elsif ($high != 0) {
85 # percent decline
86 $sumsq += (100 * ($high - $value) / $high) ** 2;
87 }
88 }
89 return sqrt ($sumsq / scalar @$aref);
90 }
91  
92 sub proc {
93 my ($class_or_self, $N) = @_;
94 my @array;
95  
96 return sub {
97 my ($value) = @_;
98 unshift @array, $value;
99 if (@array > $N) { pop @array; }
100 return ulcer_on_aref(\@array);
101 };
102 }
103  
104 1;
105 __END__