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

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 102 CC 4 SCAR 17.9
CC
4
Cov
0%
proc
20.0
longname
2.0
shortname
2.0
CRAP 6.0
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::TII;
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::SMA;
27  
28  
29 # http://www.linnsoft.com/tour/techind/tii.htm
30 # Sample Intel (INTC) 2001/2001, seems different to what get from the
31 # code here. Maybe not 60/30 parameters?
32 #
33 # http://working-money.com/Documentation/FEEDbk_docs/Archive/062002/Abstracts_new/Pee/pee.html
34 # Start of article, rest for sale.
35 #
36 # http://www.traders.com/Documentation/FEEDbk_docs/Archive/062002/TradersTips/TradersTips.html
37 # TASC Trader's Tips June 2002.
38 # Linnsoft sample MSFT from Sep 2001.
39 #
40  
41  
42 sub longname { __('TII - Trend Intensity Index') }
43 sub shortname { __('TII') }
44 sub manual { __p('manual-node','Trend Intensity Index') }
45  
46 use constant
47 { hlines => [ 20, 50, 80 ],
48 type => 'indicator',
49 units => 'percentage',
50 minimum => 0,
51 maximum => 100,
52 parameter_info => [ { name => __('MA Days'),
53 key => 'tii_ma_days',
54 type => 'integer',
55 minimum => 1,
56 default => 60 },
57 { name => __('Dev Days'),
58 key => 'tii_dev_days',
59 type => 'integer',
60 minimum => 1,
61 default => 30 }],
62 };
63  
64 sub new {
65 my ($class, $parent, $N_ma, $N_dev) = @_;
66  
67 $N_ma //= parameter_info()->[0]->{'default'};
68 ($N_ma > 0) || croak "TII bad N_ma: $N_ma";
69  
70 $N_dev //= parameter_info()->[1]->{'default'};
71 ($N_dev > 0) || croak "TII bad N_dev: $N_dev";
72  
73 return $class->SUPER::new
74 (parent => $parent,
75 parameters => [ $N_ma, $N_dev ],
76 arrays => { values => [] },
77 array_aliases => { });
78 }
79 sub warmup_count {
80 my ($class_or_self, $N_ma, $N_dev) = @_;
81 return max($N_ma,$N_dev) - 1;
82 }
83 sub proc {
84 my ($class_or_self, $N_ma, $N_dev) = @_;
85  
86 my $ma_proc = App::Chart::Series::Derived::SMA->proc ($N_ma);
87 my @values;
88  
89 return sub {
90 my ($value) = @_;
91  
92 my $ma = $ma_proc->($value);
93 unshift @values, $value;
94 if (@values >= $N_dev) { pop @values; }
95  
96 my $num = 0;
97 my $den = 0;
98 foreach my $value (@values) {
99 my $diff = $value - $ma;
100 if ($diff > 0) { $num += $diff; }
101 $den += abs $diff;
102 }
103 return ($den == 0 ? 50 : 100 * $num/$den);
104 };
105 }
106  
107 1;
108 __END__