blib/lib/Math/Business/ATR.pm

filter: Statementstmt 98.2% 56 / 57 Branchbran 85.0% 17 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 6 totaltotal 83.3% 85 / 102 SCAR 26.5
CC
14
Cov
92%
insert
12.1
set_days
2.0
tag
2.0
CRAP 14.1
« blib/lib/Math/Business/CCI.pm ↑ Summary blib/lib/Math/Business/WMA.pm »
1 package Math::Business::ATR;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 1;
8  
9 0 ▶sub tag { (shift)->{tag} }
Subroutine tag: not called CC=1 SCAR=6.9
Pod undocumented
10  
11 sub recommended {
12 1 ▶ my $class = shift;
Subroutine recommended: called CC=1 SCAR=0.0
Pod undocumented
13  
14 1 $class->new(14);
15 }
16  
17 sub new {
18 5 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
19 5 my $this = bless {
20 }, $class;
21  
22 5 my $days = shift;
23 5 ▶ if( defined $days ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $days) 1 4 5
24 1 $this->set_days( $days );
25 }
26  
27 5 return $this;
28 }
29  
30 sub set_days {
31 5 ▶ my $this = shift;
Subroutine set_days: called CC=2 SCAR=7.0
Pod undocumented
32 5 my $arg = int(shift);
33  
34 5 ▶ croak "days must be a positive non-zero integer" if $arg <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $arg <= 0 0 5 5
35  
36 5 $this->{tag} = "ATR($arg)";
37  
38 # NOTE: wilder uses 13/14 * last + 1/14 * current for his exponential average ...
39 # probably wouldn't have been my first choice, but that's how ATR is defined.
40  
41 5 $this->{days} = $arg;
42 5 $this->{R1} = ($arg-1)/$arg;
43 5 $this->{R} = 1/$arg;
44 }
45  
46 sub insert {
47 1124 ▶ my $this = shift;
Subroutine insert: called CC=12 SCAR=25.0
Pod undocumented
48  
49 1124 my $y_close = $this->{y_close};
50 1124 ▶ while( defined( my $point = shift ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $point = shift())) 1124 1124 2248
51 1124 ▶ croak "insert takes three tuple [high, low, close]" unless ref $point eq "ARRAY" and @$point == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $point eq "ARRAY" and @$point == 3 0 1124 1124
Condition33%1/3
ref $point eq "ARRAY" and @$point == 3
!ll&&!rl&&r
001124
MC/DC0%0/2
ref $point eq "ARRAY" and @$point == 3
ref $point eq "ARRAY" @$point == 3
Truth table33%1/3
A and B
A= ref $point eq "ARRAY"B= @$point == 3
ABresult
0X0
100
111
52 1124 my ($t_high, $t_low, $t_close) = @$point;
53  
54 1124 ▶ if( defined $y_close ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $y_close) { } 1119 5 1124
55 1119 my $A = abs( $t_high - $t_low );
56 1119 my $B = abs( $t_high - $y_close );
57 1119 my $C = abs( $t_low - $y_close );
58  
59 1119 my $true_range = $A;
60 1119 ▶ $true_range = $B if $B > $true_range;
Branch: 1 branch
BranchTrueFalseTotal
if $B > $true_range 110 1009 1119
61 1119 ▶ $true_range = $C if $C > $true_range;
Branch: 1 branch
BranchTrueFalseTotal
if $C > $true_range 123 996 1119
62  
63 1119 ▶ if( defined(my $atr = $this->{ATR}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $atr = $this->{'ATR'})) { } 1065 54 1119
64 1065 $this->{ATR} = $this->{R1} * $atr + $this->{R} * $true_range;
65  
66 } else {
67 54 my $p;
68 54 my $N = $this->{days};
69 54 ▶ if( ref($p = $this->{_p}) and (@$p >= $N-1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($p = $this->{'_p'}) and @$p >= $N - 1) { } 4 50 54
Condition66%2/3
ref($p = $this->{"_p"}) and @$p >= $N - 1
!ll&&!rl&&r
0504
MC/DC50%1/2
ref($p = $this->{"_p"}) and @$p >= $N - 1
ref($p = $this->{"_p"}) @$p >= $N - 1
Truth table66%2/3
A and B
A= ref($p = $this->{"_p"})B= @$p >= $N - 1
ABresult
0X0
100
111
70 4 my $sum = 0;
71 4 $sum += $_ for @$p;
72 4 $sum += $true_range;
73  
74 4 $this->{ATR} = $sum / $N;
75 4 delete $this->{_p};
76  
77 } else {
78 50 push @{$this->{_p}}, $true_range;
79 }
80 }
81  
82 } else {
83 5 my $true_range = $t_high - $t_low;
84  
85 # NOTE: _p shouldn't exist because this initializer is only used for the very first entry
86 5 ▶ die "something is clearly wrong, see note below above line" if exists $this->{_p};
Branch: 1 branch
BranchTrueFalseTotal
if exists $this->{"_p"} 0 5 5
87  
88 # NOTE: this initializer sucks because the calculation is done
89 # differently than it would be if you had data from the day before.
90 # IMO, we should just return undef for an extra day, but this
91 # appears to be by definition, so we do it:
92  
93 5 $this->{_p} = [$true_range];
94 }
95  
96 1124 $y_close = $t_close;
97 }
98  
99 1124 $this->{y_close} = $y_close;
100 }
101  
102 sub query {
103 1085 ▶ my $this = shift;
Subroutine query: called CC=1 SCAR=0.0
Pod undocumented
104  
105 1085 return $this->{ATR};
106 }
107  
108 __END__
« blib/lib/Math/Business/CCI.pm ↑ Summary blib/lib/Math/Business/WMA.pm »