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

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 90 CC 5 SCAR 22.4
CC
5
Cov
0%
proc
30.0
longname
2.0
shortname
2.0
CRAP 9.4
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::VHF;
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::Calculation;
27 use App::Chart::Series::Derived::SMA;
28  
29 # http://www.incrediblecharts.com/indicators/vertical_horizontal_filter.php
30  
31 sub longname { __('VHF - Vertical Horizontal Filter') }
32 sub shortname { __('VHF') }
33 sub manual { __p('manual-node','Vertical Horizontal Filter') }
34  
35 use constant
36 { type => 'indicator',
37 units => 'zero_to_one',
38 minimum => 0,
39 maximum => 1,
40 hlines => [ 0.5 ],
41 parameter_info => [ { name => __('Days'),
42 key => 'vhf_days',
43 type => 'integer',
44 minimum => 2,
45 default => 20 }],
46 };
47  
48 sub new {
49 my ($class, $parent, $N) = @_;
50  
51 $N //= parameter_info()->[0]->{'default'};
52 ($N > 0) || croak "VHF bad N: $N";
53  
54 return $class->SUPER::new
55 (parent => $parent,
56 parameters => [ $N ],
57 arrays => { values => [] },
58 array_aliases => { });
59 }
60 *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1
61  
62 sub proc {
63 my ($class_or_self, $N) = @_;
64 if ($N < 2) { return sub{undef}; }
65 my @values;
66 my $sum_proc = App::Chart::Series::Calculation->sum ($N-1);
67  
68 return sub {
69 my ($value) = @_;
70  
71 my $abs_diffs = (@values
72 ? $sum_proc->(abs ($value - $values[0]))
73 : 0);
74  
75 unshift @values, $value;
76 if (@values > $N) { pop @values; }
77  
78 return ($abs_diffs == 0 ? 0.5
79 : (max(@values) - min(@values)) / $abs_diffs);
80 };
81 }
82  
83 1;
84 __END__