blib/lib/Math/Business/ParabolicSAR.pm

filter: Statementstmt 92.2% 83 / 90 Branchbran 78.2% 36 / 46 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 8 totaltotal 74.3% 133 / 179 SCAR 35.2
CC
29
Cov
82%
insert
25.8
set_alpha
4.6
new
3.1
CRAP 33.8
« blib/lib/Math/Business/LaguerreFilter.pm ↑ Summary blib/lib/Math/Business/ConnorRSI.pm »
1 package Math::Business::ParabolicSAR;
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 LONG => 7,
8 SHORT => 9,
9 HP => 1,
10 LP => 0,
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 sub recommended {
18 2 ▶ my $class = shift;
Subroutine recommended: called CC=1 SCAR=0.0
Pod undocumented
19 2 $class->new(0.02, 0.20);
20 }
21  
22 sub new {
23 2 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
24 2 my $this = bless {e=>[], y=>[]}, $class;
25  
26 2 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
27 2 eval { $this->set_alpha(@_) };
28 2 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
29 }
30  
31 2 return $this;
32 }
33  
34 sub set_alpha {
35 2 ▶ my $this = shift;
Subroutine set_alpha: called CC=4 SCAR=15.2
Pod undocumented
36 2 my ($as,$am) = @_;
37  
38 2 ▶ croak "set_alpha(as,am) takes two arguments, the alpha start (0<as<1) and the alpha max (0<as<am<1)"
Branch: 1 branch
BranchTrueFalseTotal
unless 0 < $as and $as < $am and $am < 1 0 2 2
Condition33%2/6
0 < $as and $as < $am and $am < 1
!ll&&!rl&&r
002
0 < $as and $as < $am
!ll&&!rl&&r
002
MC/DC0%0/3
0 < $as and $as < $am and $am < 1
0 < $as $as < $am $am < 1
Truth table25%1/4
A and B and C
A= 0 < $asB= $as < $amC= $am < 1
ABCresult
0XX0
10X0
1100
1111
39 unless 0 < $as and $as < $am and $am < 1;
40  
41 2 $this->{as} = $as;
42 2 $this->{am} = $am;
43  
44 2 $this->{tag} = "PSAR($as,$am)";
45  
46 2 return;
47 }
48  
49 sub insert {
50 22 ▶ my $this = shift;
Subroutine insert: called CC=24 SCAR=32.5
Pod undocumented
51  
52 22 my ($as,$am);
53 22 ▶ croak "must set_alpha(as,am) before inserting data" unless defined( $am = $this->{am} ) and defined( $as = $this->{as} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($am = $this->{"am"}) and defined($as = $this->{"as"}) 0 22 22
Condition33%1/3
defined($am = $this->{"am"}) and defined($as = $this->{"as"})
!ll&&!rl&&r
0022
MC/DC0%0/2
defined($am = $this->{"am"}) and defined($as = $this->{"as"})
defined($am = $this->{"am"}) defined($as = $this->{"as"})
Truth table33%1/3
A and B
A= defined($am = $this->{"am"})B= defined($as = $this->{"as"})
ABresult
0X0
100
111
54  
55 22 my ($y_low, $y_high) = @{$this->{y}};
56 22 my ($open,$high,$low,$close);
57  
58 22 my $S;
59 22 my $P = $this->{S};
60 22 my $A = $this->{A};
61 22 my $e = $this->{e};
62  
63 22 my $ls = $this->{ls};
64  
65 22 ▶ while( defined( my $ar = shift ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ar = shift())) 137 22 159
66 137 ▶ croak "arguments to insert must be four tuple (open,high,low,close) with high greater than or equal to low"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ar eq "ARRAY" and @$ar == 4 and $ar->[2] <= $ar->[1] 0 137 137
Condition33%2/6
ref $ar eq "ARRAY" and @$ar == 4 and $ar->[2] <= $ar->[1]
!ll&&!rl&&r
00137
ref $ar eq "ARRAY" and @$ar == 4
!ll&&!rl&&r
00137
MC/DC0%0/3
ref $ar eq "ARRAY" and @$ar == 4 and $ar->[2] <= $ar->[1]
ref $ar eq "ARRAY" @$ar == 4 $ar->[2] <= $ar->[1]
Truth table25%1/4
A and B and C
A= ref $ar eq "ARRAY"B= @$ar == 4C= $ar->[2] <= $ar->[1]
ABCresult
0XX0
10X0
1100
1111
67 unless ref($ar) eq "ARRAY" and @$ar==4 and $ar->[2]<=$ar->[1];
68  
69 # NOTE: we really only use open and close to initialize ...
70 137 ($open,$high,$low,$close) = @$ar;
71  
72 137 ▶ if( defined $ls ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ls) { } 135 2 137
73 # calculate sar_t
74 # The Encyclopedia of Technical Market Indicators - Page 495
75  
76 135 my @oe = @$e;
77 135 ▶ $e->[HP] = $high if $high > $e->[HP]; # the highest point during the trend
Branch: 1 branch
BranchTrueFalseTotal
if $high > $e->[1] 27 108 135
78 135 ▶ $e->[LP] = $low if $low < $e->[LP]; # the lowest point during the trend
Branch: 1 branch
BranchTrueFalseTotal
if $low < $e->[0] 29 106 135
79  
80 135 ▶ if( $ls == LONG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ls == 7) { } 58 77 135
81 58 $S = $P + $A*($e->[HP] - $P); # adjusted upwards from the reset like so
82  
83 # NOTE: many sources say you should flop short/long if you get
84 # inside the price range for the last *two* periods. Amazon,
85 # Yahoo! and stockcharts dont' seem to do it that way.
86  
87 58 ▶ if( $S > $low ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($S > $low) { } 8 50 58
elsif ($S > $y_low) { } 2 48 50
elsif ($oe[1] != $e->[1]) { } 17 31 48
88 8 $ls = SHORT; # new short position
89  
90 8 $S = $e->[HP];
91 8 $A = $as;
92  
93 8 ▶ $e->[HP] = ($high>$y_high ? $high : $y_high);
Branch: 1 branch
BranchTrueFalseTotal
$high > $y_high ? : 2 6 8
94 8 ▶ $e->[LP] = ($low <$y_low ? $low : $y_low );
Branch: 1 branch
BranchTrueFalseTotal
$low < $y_low ? : 7 1 8
95  
96 } elsif( $S > $y_low ) {
97 2 $S = $y_low;
98  
99 } elsif( $oe[HP] != $e->[HP] ) {
100 17 $A += $as;
101 17 ▶ $A = $am if $A > $am;
Branch: 1 branch
BranchTrueFalseTotal
if $A > $am 0 17 17
102 }
103  
104 } else {
105 77 $S = $P + $A*($e->[LP] - $P); # adjusted downwards from the reset like so
106  
107 # NOTE: many sources say you should flop short/long if you get
108 # inside the price range for the last *two* periods. Amazon,
109 # Yahoo! and stockcharts dont' seem to do it that way.
110  
111 77 ▶ if( $S < $high ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($S < $high) { } 7 70 77
elsif ($S < $y_high) { } 4 66 70
elsif ($oe[0] != $e->[0]) { } 22 44 66
112 7 $ls = LONG; # new long position
113  
114 7 $S = $e->[LP];
115 7 $A = $as;
116  
117 7 ▶ $e->[HP] = ($high>$y_high ? $high : $y_high);
Branch: 1 branch
BranchTrueFalseTotal
$high > $y_high ? : 7 0 7
118 7 ▶ $e->[LP] = ($low <$y_low ? $low : $y_low );
Branch: 1 branch
BranchTrueFalseTotal
$low < $y_low ? : 0 7 7
119  
120 } elsif( $S < $y_high ) {
121 4 $S = $y_high;
122  
123 } elsif( $oe[LP] != $e->[LP] ) {
124 22 $A += $as;
125 22 ▶ $A = $am if $A > $am;
Branch: 1 branch
BranchTrueFalseTotal
if $A > $am 0 22 22
126 }
127 }
128  
129 } else {
130 # circa 2010: initialize somehow
131 # (never did find a good description of how to initialize this mess.
132 # I think you're supposed to tell it how to start)
133 # this is the only time we use open/close and it's not even in the definition
134 #
135 # 2011-01-03: I did look this up, it's the "SIP" or significant
136 # point. It should be the lowest (or the highest) point we have
137 # from our recent-ish data or "long trade" as he calls it. This'll
138 # do as an approximation of that imo — otherwise we'll have to
139 # start asking for a few days previous trades just to initialize.
140  
141 2 $A = $as;
142  
143 2 ▶ if( $open < $close ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open < $close) { } 2 0 2
144 2 $ls = LONG;
145 2 $S = $low;
146  
147 } else {
148 0 $ls = SHORT;
149 0 $S = $high;
150 }
151  
152 2 $e->[HP] = $high;
153 2 $e->[LP] = $low;
154 }
155  
156 137 $P = $S;
157  
158 137 ($y_low, $y_high) = ($low, $high);
159 }
160  
161 ## DEBUG ## warn "{S}=$S; {A}=$A";
162  
163 22 $this->{S} = $S;
164 22 $this->{A} = $A;
165 22 $this->{ls} = $ls;
166  
167 22 @{$this->{y}} = ($y_low, $y_high);
168 }
169  
170 sub query {
171 9 ▶ my $this = shift;
Subroutine query: called CC=1 SCAR=0.0
Pod undocumented
172  
173 9 $this->{S};
174 }
175  
176 sub long {
177 0 ▶ my $this = shift;
Subroutine long: not called CC=1 SCAR=6.9
Pod undocumented
178 0 $this->{ls} == LONG;
179 }
180  
181 sub short {
182 0 ▶ my $this = shift;
Subroutine short: not called CC=1 SCAR=6.9
Pod undocumented
183 0 $this->{ls} == SHORT;
184 }
185  
186 __END__
« blib/lib/Math/Business/LaguerreFilter.pm ↑ Summary blib/lib/Math/Business/ConnorRSI.pm »