| 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 | use constant { | ||||||||||||||||||||||||||||
| 7 | 2 | ALPHA => 2, | |||||||||||||||||||||||||||
| 8 | LENGTH => 3, | ||||||||||||||||||||||||||||
| 9 | F => -2, | ||||||||||||||||||||||||||||
| 10 | TAG => -1, | ||||||||||||||||||||||||||||
| 11 | 2 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | sub dnew { | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
dnew: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | my $days = int shift; $days = 4 unless $days > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | my $this = $class->new(2/(1+$days)); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | return $this; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub new { | ||||||||||||||||||||||||||||
| 28 | 4 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 41 | 4 | $this->set_alpha( $alpha ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 4 | my $length = shift; | |||||||||||||||||||||||||||
| 44 | 4 | ▶ | if( defined $length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | $this->set_adaptive( $length ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | return $this; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub reset { | ||||||||||||||||||||||||||||
| 52 | 6 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 6 | $this->[0] = []; | |||||||||||||||||||||||||||
| 54 | 6 | $this->[1] = []; | |||||||||||||||||||||||||||
| 55 | 6 | $this->[4] = []; | |||||||||||||||||||||||||||
| 56 | return | ||||||||||||||||||||||||||||
| 57 | 6 | } | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub set_days { | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_days: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | $this->[TAG] = "LAG($arg)"; | |||||||||||||||||||||||||||
| 68 | 0 | $this->reset | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub set_alpha { | ||||||||||||||||||||||||||||
| 72 | 4 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_alpha: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$alpha >= 0 and $alpha <= 1
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
| |||||||||||||||||||||||||||||
| 76 | 4 | my $arg = int ( (1/$alpha)*2-1 ); # pretty sure... gah, algebra | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | $this->[TAG] = "LAG($arg)"; | |||||||||||||||||||||||||||
| 79 | 4 | $this->reset | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub set_adaptive { | ||||||||||||||||||||||||||||
| 83 | 2 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_adaptive: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 2 | my $that = int shift; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | croak "adaptive length must be an non-negative integer" unless $that >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 2 | $this->[LENGTH] = $that; | |||||||||||||||||||||||||||
| 88 | 2 | $this->reset | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub insert { | ||||||||||||||||||||||||||||
| 92 | 7002 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
insert: called CC=19 SCAR=30.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 7002 | my ($h, $L, $alpha, $length, $diff, $filter) = @$this; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 7002 | ▶ | croak "You must set the number of days before you try to insert" if not defined $alpha; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 7002 | ▶ | while( defined( my $P = shift ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 7002 | ▶ | if( ref $P ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | my @a = eval {@$P}; croak $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | my $c = 0+@a; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | croak "high+low should only be two elements, not c=$c" unless $c == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | $P = ($a[0]+$a[1])/$c; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 7002 | ▶ | if( defined $L->[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | # adapt alpha {{{ | ||||||||||||||||||||||||||||
| 108 | 7000 | ▶ | if( $length and defined($filter) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$length and defined $filter
MC/DC100%2/2
$length and defined $filter
$length
defined $filter
Truth table100%3/3
A and B
A= $lengthB= defined $filter
| |||||||||||||||||||||||||||||
| 109 | 3497 | my $d = abs($P-$filter); | |||||||||||||||||||||||||||
| 110 | 3497 | push @$diff, $d; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 3497 | my $k = @$diff - $length; | |||||||||||||||||||||||||||
| 113 | 3497 | ▶ | splice @$diff, 0, $k if $k>0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3497 | ▶ | if( $k > 0 ) { # NOTE Ehler really does this, "CurrentBar > Length". See below. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | # IE, $k will only by >0 when we've moved past the 20th point | ||||||||||||||||||||||||||||
| 117 | 3477 | my $HH = $d; | |||||||||||||||||||||||||||
| 118 | 3477 | my $LL = $d; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 3477 | for(@$diff) { | |||||||||||||||||||||||||||
| 121 | 69540 | ▶ | $HH = $_ if $_ > $HH; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 69540 | ▶ | $LL = $_ if $_ < $LL; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3477 | ▶ | if( $HH != $LL ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | # Ehler: If CurrentBar > Length and HH - LL <> 0 then alpha = Median(((Diff - LL) / (HH - LL)), 5); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # NOTE: wtf is a "5 bar median"? I guess it's this, or | ||||||||||||||||||||||||||||
| 129 | # pretty close to it. I imagine Median() runs through | ||||||||||||||||||||||||||||
| 130 | # the [] hist for Diff, LL, and HH, but I can't say for | ||||||||||||||||||||||||||||
| 131 | # sure without access to the programming language he | ||||||||||||||||||||||||||||
| 132 | # uses in the book. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # AVG # my $sum = ($diff->[-5]-$LL)/($HH-$LL); | ||||||||||||||||||||||||||||
| 135 | # AVG # $sum += ($diff->[$_]-$LL)/($HH-$LL) for (-4 .. -1); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # AVG # ($this->[ALPHA] = $alpha = $sum / 5); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # NOTE (later): he appears to mean the median (not | ||||||||||||||||||||||||||||
| 140 | # average) of a scalar $HH/$LL against the last 5 @diff | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 3477 | my @b5 = sort {$a<=>$b}map {(($diff->[$_]-$LL)/($HH-$LL))} -5 .. -1; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 3477 | $this->[ALPHA] = $alpha = $b5[2]; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | # }}} | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 7000 | my $O = [ @$L ]; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # L0 = alpha*Price + (1 - alpha)*L0[1] = alpha*P + (1-alpha)*O[0] | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 7000 | $L->[0] = $alpha*$P + (1-$alpha)*$O->[0]; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # L1 = (1 - alpha)*L1[1] - (1 - alpha)*L0 + L0[1] = (1 - alpha)*O[1] - (1 - alpha)*L[0] + O[0] | ||||||||||||||||||||||||||||
| 157 | # L2 = (1 - alpha)*L2[1] - (1 - alpha)*L1 + L1[1] = (1 - alpha)*O[2] - (1 - alpha)*L[1] + O[1] | ||||||||||||||||||||||||||||
| 158 | # L3 = (1 - alpha)*L3[1] - (1 - alpha)*L2 + L2[1] = (1 - alpha)*O[3] - (1 - alpha)*L[2] + O[2] | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 7000 | ▶ | $L->[1] = defined($O->[1]) ? (1 - $alpha)*$O->[1] - (1 - $alpha)*$L->[0] + $O->[0] : $O->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 7000 | ▶ | $L->[2] = defined($O->[2]) ? (1 - $alpha)*$O->[2] - (1 - $alpha)*$L->[1] + $O->[1] : $O->[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 7000 | ▶ | $L->[3] = defined($O->[3]) ? (1 - $alpha)*$O->[3] - (1 - $alpha)*$L->[2] + $O->[2] : $O->[2]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | } else { | ||||||||||||||||||||||||||||
| 165 | 2 | $L->[0] = $P; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 7002 | ▶ | if( 4 == grep {defined $_} @$L ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 6996 | $this->[F] = ($L->[0] + 2*$L->[1] + 2*$L->[2] + $L->[3])/6; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub query { | ||||||||||||||||||||||||||||
| 175 | 7002 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 7002 | return $this->[F]; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | __END__ | ||||||||||||||||||||||||||||