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

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 141 CC 4 SCAR 13.6
CC
4
Cov
0%
proc
12.0
fill_part
6.0
longname
2.0
CRAP 3.9
1 # Copyright 2005, 2006, 2007, 2009, 2010, 2011 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::MFI;
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::RSI;
27 use App::Chart::Series::Derived::TypicalPrice;
28  
29  
30 # http://www.linnsoft.com/tour/techind/mfi.htm
31 # Formula, and sample Apple (AAPL) from 1999/2000 as 9-day MFI smoothed
32 # by 7-day SMA.
33 #
34  
35 # The MFI formula is sometimes written
36 #
37 # / 100 \
38 # | --------------- |
39 # MFI = 100 - | positive MF |
40 # | 1 + ----------- |
41 # \ negative MF /
42 #
43 #
44 # but it's much easier to rearrange that to what's shown in the manual,
45 #
46 # positive MF
47 # MFI = 100 * -----------
48 # pos+neg MF
49  
50  
51  
52 sub longname { __('MFI - Money Flow Index') }
53 sub shortname { __('MFI') }
54 sub manual { __p('manual-node','Money Flow Index') }
55  
56 use constant
57 { type => 'indicator',
58 units => 'percentage',
59 minimum => 0,
60 maximum => 100,
61 hlines => [ 20, 80 ],
62 parameter_info => [ { name => __('Days'),
63 key => 'mfi_days',
64 type => 'integer',
65 minimum => 1,
66 default => 14 } ],
67 };
68  
69 sub new {
70 my ($class, $parent, $N) = @_;
71  
72 $N //= parameter_info()->[0]->{'default'};
73 ($N > 0) || croak "MFI bad N: $N";
74  
75 return $class->SUPER::new
76 (parent => $parent,
77 parameters => [ $N ],
78 arrays => { values => [] },
79 array_aliases => { });
80 }
81 sub warmup_count {
82 my ($self_or_class, $N) = @_;
83 return $N;
84 }
85 sub proc {
86 my ($class_or_self, $N) = @_;
87 my $num_proc = App::Chart::Series::Calculation->sum ($N);
88 my $den_proc = App::Chart::Series::Calculation->sum ($N);
89 my $prev_tp;
90  
91 return sub {
92 my ($high, $low, $close, $volume) = @_;
93  
94 my $tp = App::Chart::Series::Derived::TypicalPrice::typical_price
95 ($high, $low, $close);
96 $volume //= 0;
97  
98 my $mfi;
99 if (defined $prev_tp) {
100 # money flow is +tp*volume, -tp*volume, or 0
101 my $mf = ($tp <=> $prev_tp) * $tp * $volume;
102 $prev_tp = $tp;
103  
104 my $num = $num_proc->(max ($mf, 0));
105 my $den = $den_proc->(abs ($mf));
106 $mfi = ($den == 0 ? 0 : $num/$den);
107 }
108 $prev_tp = $tp;
109 return $mfi;
110 };
111 }
112  
113 sub fill_part {
114 my ($self, $lo, $hi) = @_;
115 ### MFI fill_part(): "$lo $hi"
116 my $parent = $self->{'parent'};
117  
118 my $warmup_count = $self->warmup_count_for_position ($lo);
119 my $start = $parent->find_before ($lo, $warmup_count);
120 $parent->fill ($lo, $hi);
121 my $p = $parent->values_array;
122 my $ph = $parent->array('highs') || $p;
123 my $pl = $parent->array('lows') || $p;
124 my $pv = $parent->array('volumes') || [];
125  
126 my $s = $self->values_array;
127 $hi = min ($hi, $#$p);
128 if ($#$s < $hi) { $#$s = $hi; } # pre-extend
129  
130 my $proc = $self->proc(@{$self->{'parameters'}});
131  
132 foreach my $i ($start .. $lo-1) {
133 my $close = $p->[$i] // next;
134 $proc->($ph->[$i], $pl->[$i], $close, $pv->[$i]);
135 }
136 foreach my $i ($lo .. $hi) {
137 my $close = $p->[$i] // next;
138 $s->[$i] = $proc->($ph->[$i], $pl->[$i], $close, $pv->[$i]);
139 }
140 }
141  
142  
143 1;
144 __END__