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

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 100 CC 3 SCAR 10.4
CC
3
Cov
0%
mean_and_meanabsdev_proc
6.0
proc
6.0
longname
2.0
CRAP 2.8
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::CCI;
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::TypicalPrice;
28 use App::Chart::Series::Derived::WilliamsR;
29  
30 # http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:commodity_channel_index_cci
31 # Description, formula, sample of Brooktrout BRKT from 1999/2000 (no
32 # longer available from Yahoo).
33 #
34 # http://www.incrediblecharts.com/indicators/commodity_channel_index.php
35 # Description, formula, sample of IBM from 1999.
36 #
37  
38 sub longname { __('CCI - Commodity Channel Index') }
39 sub shortname { __('CCI') }
40 sub manual { __p('manual-node','Commodity Channel Index') }
41  
42 use constant
43 { type => 'indicator',
44 units => 'CCI',
45 hlines => [ -100, 100 ],
46 parameter_info => [ { name => __('Days'),
47 key => 'cci_days',
48 type => 'integer',
49 minimum => 1,
50 default => 20 } ],
51 };
52  
53 sub new {
54 my ($class, $parent, $N) = @_;
55  
56 $N //= parameter_info()->[0]->{'default'};
57 ($N > 0) || croak "CCI bad N: $N";
58  
59 return $class->SUPER::new
60 (parent => $parent,
61 parameters => [ $N ],
62 arrays => { values => [] },
63 array_aliases => { });
64 }
65 sub mean_and_meanabsdev_proc {
66 my ($class, $N) = @_;
67 my @array;
68 my $total;
69  
70 return sub {
71 my ($value) = @_;
72 unshift @array, $value;
73 $total += $value;
74 if (@array > $N) { $total -= pop @array; }
75  
76 my $mean = $total / scalar @array;
77 my $meanabsdev = (List::Util::sum (map {abs($_-$mean)} @array)
78 / scalar @array);
79 return ($mean, $meanabsdev);
80 };
81 }
82 sub proc {
83 my ($class_or_self, $N) = @_;
84 my $meandev_proc = $class_or_self->mean_and_meanabsdev_proc ($N);
85  
86 return sub {
87 my $tp = App::Chart::Series::Derived::TypicalPrice::typical_price (@_);
88 my ($mean, $meanabsdev) = $meandev_proc->($tp);
89 return ($meanabsdev == 0 ? undef
90 : ($tp - $mean) / ($meanabsdev * 0.015));
91 };
92 }
93 *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part;
94 *warmup_count = \&App::Chart::Series::Derived::WilliamsR::warmup_count; # $N-1
95  
96 1;
97 __END__