blib/lib/Math/Business/LaguerreFilter.pm

filter: Statementstmt 78.1% 75 / 96 Branchbran 70.4% 31 / 44 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 73.3% 11 / 15 Podpod 0.0% 0 / 10 totaltotal 70.2% 123 / 175 CC 26 SCAR 24.3
CC
26
Cov
75%
insert
20.9
dnew
6.0
set_days
6.0
CRAP 11.4
« blib/lib/Math/Business/ParabolicSAR.pm ↑ Summary blib/lib/Math/Business/DMI.pm »
1 package Math::Business::LaguerreFilter;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ALPHA => 2,
8 LENGTH => 3,
9 F => -2,
10 TAG => -1,
11 2 };
12  
13 2 1;
14  
15 0 ▶sub tag { (shift)->[TAG] }
Subroutine tag: not called CC=1 SCAR=6.9
Pod undocumented
16  
17 0 ▶sub recommended { croak "no recommendation" }
Subroutine recommended: not called CC=1 SCAR=6.9
Pod undocumented
18  
19 0 ▶sub dnew {
Subroutine dnew: not called CC=2 SCAR=17.9
Pod undocumented
20 0 my $class = shift;
21 0 ▶ my $days = int shift; $days = 4 unless $days > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $days > 1 0 0 0
22 0 my $this = $class->new(2/(1+$days));
23  
24 0 return $this;
25 }
26  
27 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
28 4 my $class = shift;
29 4 my $this = bless [
30 [], # [0] P-hist
31 [], # [1] L0-L4
32 0, # [2] alpha
33 0, # [3] adaptive length
34 [], # [4] adaptive diff history
35 undef, # [5] filter
36 undef, # [6] tag
37 ], $class;
38  
39 4 my $alpha = shift;
40 4 ▶ $alpha = 0.5 if not defined $alpha;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alpha 1 3 4
41 4 $this->set_alpha( $alpha );
42  
43 4 my $length = shift;
44 4 ▶ if( defined $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length) 1 3 4
45 1 $this->set_adaptive( $length );
46 }
47  
48 4 return $this;
49 }
50  
51 6 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
52 6 my $this = shift;
53 6 $this->[0] = [];
54 6 $this->[1] = [];
55 6 $this->[4] = [];
56 return
57 6 }
58  
59 0 ▶sub set_days {
Subroutine set_days: not called CC=2 SCAR=17.9
Pod undocumented
60 0 my $this = shift;
61 0 my $arg = 0+shift;
62  
63 0 my $alpha = 2/(1+$arg);
64 0 eval { $this->set_alpha( $alpha ) };
65 0 ▶ croak "set_days() is basically set_alpha(2/(1+$arg)), which complained: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
66  
67 0 $this->[TAG] = "LAG($arg)";
68 0 $this->reset
69 }
70  
71 4 ▶sub set_alpha {
Subroutine set_alpha: called CC=3 SCAR=11.4
Pod undocumented
72 4 my $this = shift;
73 4 my $alpha = 0+shift;
74  
75 4 ▶ croak "alpha must be a real between >=0 and <=1" unless $alpha >= 0 and $alpha <= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $alpha >= 0 and $alpha <= 1 0 4 4
Condition33%1/3
$alpha >= 0 and $alpha <= 1
!ll&&!rl&&r
004
MC/DC0%0/2
$alpha >= 0 and $alpha <= 1
$alpha >= 0 $alpha <= 1
Truth table33%1/3
A and B
A= $alpha >= 0B= $alpha <= 1
ABresult
0X0
100
111
76  
77 4 $this->[2] = $alpha;
78  
79 4 my $arg = int ( (1/$alpha)*2-1 ); # pretty sure... gah, algebra
80  
81 4 $this->[TAG] = "LAG($arg)";
82 4 $this->reset
83 }
84  
85 2 ▶sub set_adaptive {
Subroutine set_adaptive: called CC=2 SCAR=7.0
Pod undocumented
86 2 my $this = shift;
87 2 my $that = int shift;
88  
89 2 ▶ croak "adaptive length must be an non-negative integer" unless $that >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $that >= 0 0 2 2
90 2 $this->[LENGTH] = $that;
91 2 $this->reset
92 }
93  
94 7002 ▶sub insert {
Subroutine insert: called CC=19 SCAR=30.4
Pod undocumented
95 7002 my $this = shift;
96 7002 my ($h, $L, $alpha, $length, $diff, $filter) = @$this;
97  
98 7002 ▶ croak "You must set the number of days before you try to insert" if not defined $alpha;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $alpha 0 7002 7002
99 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 7002 ▶ while( defined( my $P = shift ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $P = shift())) 7002 7002 14004
102 7002 ▶ if( ref $P ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $P) 0 7002 7002
103 0 ▶ my @a = eval {@$P}; croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
104 0 my $c = 0+@a;
105 0 ▶ croak "high+low should only be two elements, not c=$c" unless $c == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $c == 2 0 0 0
106 0 $P = ($a[0]+$a[1])/$c;
107 }
108  
109 7002 ▶ if( not defined $L->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $L->[0]) { } 2 7000 7002
110  
111 2 $L->[0] = $P;
112  
113 } else {
114  
115 # adapt alpha {{{
116 7000 ▶ if( $length and defined($filter) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length and defined $filter) 3497 3503 7000
Condition100%3/3
$length and defined $filter
!ll&&!rl&&r
350033497
MC/DC100%2/2
$length and defined $filter
$length defined $filter
Truth table100%3/3
A and B
A= $lengthB= defined $filter
ABresult
0X0
100
111
117 3497 my $d = abs($P-$filter);
118 3497 push @$diff, $d;
119  
120 3497 my $k = @$diff - $length;
121 3497 ▶ splice @$diff, 0, $k if $k>0;
Branch: 1 branch
BranchTrueFalseTotal
if $k > 0 3477 20 3497
122  
123 3497 ▶ if( $k > 0 ) { # NOTE Ehler really does this, "CurrentBar > Length". See below.
Branch: 1 branch
BranchTrueFalseTotal
if ($k > 0) 3477 20 3497
124 # IE, $k will only by >0 when we've moved past the 20th point
125 3477 my $HH = $d;
126 3477 my $LL = $d;
127  
128 3477 for(@$diff) {
129 69540 ▶ $HH = $_ if $_ > $HH;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $HH 9025 60515 69540
130 69540 ▶ $LL = $_ if $_ < $LL;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $LL 9197 60343 69540
131 }
132  
133 3477 ▶ if( $HH != $LL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HH != $LL) 3477 0 3477
134 # Ehler: If CurrentBar > Length and HH - LL <> 0 then alpha = Median(((Diff - LL) / (HH - LL)), 5);
135  
136 # NOTE: wtf is a "5 bar median"? I guess it's this, or
137 # pretty close to it. I imagine Median() runs through
138 # the [] hist for Diff, LL, and HH, but I can't say for
139 # sure without access to the programming language he
140 # uses in the book.
141  
142 # AVG # my $sum = ($diff->[-5]-$LL)/($HH-$LL);
143 # AVG # $sum += ($diff->[$_]-$LL)/($HH-$LL) for (-4 .. -1);
144  
145 # AVG # ($this->[ALPHA] = $alpha = $sum / 5);
146  
147 # NOTE (later): he appears to mean the median (not
148 # average) of a scalar $HH/$LL against the last 5 @diff
149  
150 3477 my @b5 = sort {$a<=>$b}map {(($diff->[$_]-$LL)/($HH-$LL))} -5 .. -1;
151  
152 3477 $this->[ALPHA] = $alpha = $b5[2];
153 }
154 }
155 }
156 # }}}
157  
158 7000 my $O = [ @$L ];
159  
160 # L0 = alpha*Price + (1 - alpha)*L0[1] = alpha*P + (1-alpha)*O[0]
161  
162 7000 $L->[0] = $alpha*$P + (1-$alpha)*$O->[0];
163  
164 # L1 = (1 - alpha)*L1[1] - (1 - alpha)*L0 + L0[1] = (1 - alpha)*O[1] - (1 - alpha)*L[0] + O[0]
165 # L2 = (1 - alpha)*L2[1] - (1 - alpha)*L1 + L1[1] = (1 - alpha)*O[2] - (1 - alpha)*L[1] + O[1]
166 # L3 = (1 - alpha)*L3[1] - (1 - alpha)*L2 + L2[1] = (1 - alpha)*O[3] - (1 - alpha)*L[2] + O[2]
167  
168 7000 ▶ $L->[1] = defined($O->[1]) ? (1 - $alpha)*$O->[1] - (1 - $alpha)*$L->[0] + $O->[0] : $O->[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $O->[1] ? : 6998 2 7000
169 7000 ▶ $L->[2] = defined($O->[2]) ? (1 - $alpha)*$O->[2] - (1 - $alpha)*$L->[1] + $O->[1] : $O->[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $O->[2] ? : 6996 4 7000
170 7000 ▶ $L->[3] = defined($O->[3]) ? (1 - $alpha)*$O->[3] - (1 - $alpha)*$L->[2] + $O->[2] : $O->[2];
Branch: 1 branch
BranchTrueFalseTotal
defined $O->[3] ? : 6994 6 7000
171  
172 }
173 }
174  
175 7002 ▶ if( 4 == grep {defined $_} @$L ) {
Branch: 1 branch
BranchTrueFalseTotal
if (4 == grep({defined $_;} @$L)) 6996 6 7002
176 6996 $this->[F] = ($L->[0] + 2*$L->[1] + 2*$L->[2] + $L->[3])/6;
177 }
178 }
179  
180 7002 ▶sub query {
Subroutine query: called CC=1 SCAR=0.0
Pod undocumented
181 7002 my $this = shift;
182  
183 7002 return $this->[F];
184 }
185  
186 __END__
« blib/lib/Math/Business/ParabolicSAR.pm ↑ Summary blib/lib/Math/Business/DMI.pm »