blib/lib/Math/Business/CCI.pm

filter: Statementstmt 93.4% 57 / 61 Branchbran 68.7% 11 / 16 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 7 totaltotal 76.1% 83 / 109 SCAR 26.9
CC
14
Cov
85%
insert
9.1
set_days
3.3
new
3.1
CRAP 14.7
« blib/lib/Math/Business/BollingerBands.pm ↑ Summary blib/lib/Math/Business/ATR.pm »
1 package Math::Business::CCI;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Math::Business::SMA;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 1;
10  
11 0 ▶sub tag { (shift)->{tag} }
Subroutine tag: not called CC=1 SCAR=6.9
Pod undocumented
12  
13 0 ▶sub recommended { croak "no recommendation" }
Subroutine recommended: not called CC=1 SCAR=6.9
Pod undocumented
14  
15 sub new {
16 1 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
17 1 ▶ my $days = shift || 20;
Condition50%1/2
shift() || 20
l!l
10
MC/DC0%0/1
shift() || 20
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
18 1 ▶ my $mul = shift || 0.015;
Condition50%1/2
shift() || 0.015
l!l
01
MC/DC0%0/1
shift() || 0.015
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
19  
20 1 my $this = bless {
21 sma => Math::Business::SMA->new($days),
22 }, $class;
23  
24 1 $this->set_days($days);
25 1 $this->set_scale($mul);
26  
27 1 return $this;
28 }
29  
30 sub set_days {
31 1 ▶ my $this = shift;
Subroutine set_days: called CC=3 SCAR=12.0
Pod undocumented
32 1 my $arg = shift;
33  
34 1 ▶ croak "days must be a positive non-zero integer" if $arg <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $arg <= 0 0 1 1
35  
36 1 $this->{sma}->set_days($arg);
37 1 $this->{len} = $arg;
38  
39 1 ▶ return unless exists $this->{mul};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"mul"} 1 0 1
40 0 my $s = sprintf("%0.0f", 1/$this->{mul});
41 0 $this->{tag} = "CCI($arg,$s)";
42 }
43  
44 sub set_scale {
45 1 ▶ my $this = shift;
Subroutine set_scale: called CC=2 SCAR=7.0
Pod undocumented
46 1 my $scale = shift;
47  
48 # NOTE: "Lambertset the constant at 0.015 to ensure that approximately 70
49 # to 80 percent of CCI values would fall between −100 and +100"
50  
51 1 $this->{mul} = 1/$scale;
52  
53 1 ▶ return unless exists $this->{len};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"len"} 0 1 1
54 1 my $s = sprintf("%0.0f", $scale);
55 1 $this->{tag} = "CCI($this->{len},$s)";
56 }
57  
58 sub insert {
59 43 ▶ my $this = shift;
Subroutine insert: called CC=9 SCAR=22.1
Pod undocumented
60 43 my $sma = $this->{sma};
61 43 my $mul = $this->{mul};
62 43 my $len = $this->{len};
63  
64 43 ▶ my $hist = ($this->{pt_hist} ||= []);
Condition100%2/2
$this->{"pt_hist"} ||= []
l!l
421
MC/DC100%1/1
$this->{"pt_hist"} ||= []
$this->{"pt_hist"}
Truth table100%2/2
A
A= $this->{"pt_hist"}
Aresult
11
00
65  
66 43 my $cci;
67 43 ▶ while( defined( my $point = shift ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $point = shift())) 43 43 86
68 43 ▶ 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 43 43
Condition33%1/3
ref $point eq "ARRAY" and @$point == 3
!ll&&!rl&&r
0043
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
69 43 my ($t_high, $t_low, $t_close) = @$point;
70 43 my $pt = ($t_high + $t_low + $t_close) / 3;
71  
72 43 push @$hist, $pt;
73 43 ▶ shift @$hist while @$hist > $len;
Branch: 1 branch
BranchTrueFalseTotal
@$hist > $len and do { shift @$hist } 23 43 66
74  
75 43 $sma->insert( $pt );
76 43 ▶ if( defined (my $v = $sma->query) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $sma->query)) 24 19 43
77 24 my @mad = map { abs($v - $_) } @$hist;
78 24 my $mad = shift @mad;
79 24 $mad += $_ for @mad;
80 24 $mad /= @mad+1;
81  
82 24 ▶ if( @$hist == $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$hist == $len) 24 0 24
83 24 $cci = $mul * ( $pt - $v ) / $mad;
84 }
85 }
86 }
87  
88 43 $this->{CCI} = $cci;
89  
90 43 return;
91 }
92  
93 sub query {
94 43 ▶ my $this = shift;
Subroutine query: called CC=1 SCAR=0.0
Pod undocumented
95  
96 43 return $this->{CCI};
97 }
98  
99 __END__
« blib/lib/Math/Business/BollingerBands.pm ↑ Summary blib/lib/Math/Business/ATR.pm »