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

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 121 CC 5 SCAR 17.9
CC
5
Cov
0%
proc
20.0
fill_part
6.0
longname
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::IMI;
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.prophet.net/learn/taglossary.jsp?index=I
30 # Description.
31 #
32 # http://www.fmlabs.com/reference/IMI.htm
33 # Formula, brief description.
34  
35 sub longname { __('IMI - Intraday Momentum Index') }
36 sub shortname { __('IMI') }
37 sub manual { __p('manual-node','Intraday Momentum Index') }
38  
39 use constant
40 { type => 'indicator',
41 units => 'percentage',
42 minimum => 0,
43 maximum => 100,
44 hlines => [ 30, 70 ],
45 parameter_info => [ { name => __('Days'),
46 key => 'imi_days',
47 type => 'integer',
48 minimum => 1,
49 default => 14 } ],
50 };
51  
52 sub new {
53 my ($class, $parent, $N) = @_;
54  
55 $N //= parameter_info()->[0]->{'default'};
56 ($N > 0) || croak "IMI bad N: $N";
57  
58 return $class->SUPER::new
59 (parent => $parent,
60 parameters => [ $N ],
61 arrays => { values => [] },
62 array_aliases => { });
63 }
64  
65  
66 # Return a procedure which calculates an intraday momentum index over an
67 # accumulated window of $N values.
68 #
69 # Each call $proc->($open, $close) enters a new point into the window, and
70 # the return is the intraday momentum index up to (and including) that
71 # point. If there's no value the return is undef, which can happen if every
72 # day has $open==$close.
73 #
74 # To prime the window initially, call $proc with $N-1 many points preceding
75 # the first desired.
76 #
77 sub proc {
78 my ($class_or_self, $N) = @_;
79 my $num_proc = App::Chart::Series::Calculation->sum ($N);
80 my $den_proc = App::Chart::Series::Calculation->sum ($N);
81  
82 return sub {
83 my ($open, $close) = @_;
84 if (! defined $open) { return undef; }
85  
86 my $num = $num_proc->($close > $open ? $close - $open : 0);
87 my $den = $den_proc->(abs ($open - $close));
88 return ($den == 0 ? undef : 100 * $num / $den);
89 };
90 }
91 *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1
92  
93 sub fill_part {
94 my ($self, $lo, $hi) = @_;
95 my $parent = $self->{'parent'};
96  
97 my $warmup_count = $self->warmup_count_for_position ($lo);
98 my $start = $parent->find_before ($lo, $warmup_count);
99 $parent->fill ($lo, $hi);
100 my $pc = $parent->values_array;
101 my $po = $parent->array('opens') || [];
102  
103 my $s = $self->values_array;
104 $hi = min ($hi, $#$pc);
105 if ($#$s < $hi) { $#$s = $hi; } # pre-extend
106  
107 my $proc = $self->proc(@{$self->{'parameters'}});
108  
109 foreach my $i ($start .. $lo-1) {
110 my $close = $pc->[$i] // next;
111 $proc->($po->[$i], $close)
112 }
113 foreach my $i ($lo .. $hi) {
114 my $close = $pc->[$i] // next;
115 $s->[$i] = ($proc->($po->[$i], $close) // next);
116 }
117 }
118  
119 1;
120 __END__