blib/lib/Math/Business/RSI.pm

filter: Statementstmt 98.8% 83 / 84 Branchbran 73.3% 22 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 10 totaltotal 85.6% 119 / 139 SCAR 27.8
CC
16
Cov
92%
insert
8.0
set_standard
3.0
set_cutler
3.0
CRAP 16.1
« blib/lib/Math/Business/MACD.pm ↑ Summary blib/lib/Math/Business/BollingerBands.pm »
1 package Math::Business::RSI;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Math::Business::SMA;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Math::Business::EMA;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 1;
11  
12 0 ▶sub tag { (shift)->{tag} }
Subroutine tag: not called CC=1 SCAR=6.9
Pod undocumented
13  
14 sub recommended {
15 1 ▶ my $class = shift;
Subroutine recommended: called CC=1 SCAR=0.0
Pod undocumented
16  
17 1 $class->new(14);
18 }
19  
20 sub new {
21 5 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
22 5 my $this = bless {
23 U => Math::Business::EMA->new,
24 D => Math::Business::EMA->new,
25 RSI => undef,
26 cy => undef,
27 }, $class;
28  
29 5 my $alpha = shift;
30 5 ▶ if( defined $alpha ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alpha) 4 1 5
31 4 $this->set_alpha( $alpha );
32 }
33  
34 5 return $this;
35 }
36  
37 sub set_alpha {
38 4 ▶ my $this = shift;
Subroutine set_alpha: called CC=2 SCAR=7.0
Pod undocumented
39 4 my $alpha = shift;
40  
41 # NOTE: this alpha is different than you might think ... it's really inverse alpha
42 # Wilder uses alpha=14 instead of alpha=(1/14) like you might expect
43  
44 4 my $days = 2*$alpha - 1; # so days is 2*$alpha-1 instead of the expected 2*(1/$alpha)-1
45  
46 4 eval { $this->set_days( $days ) };
47 4 ▶ croak "set_alpha() is basically set_days(2*$alpha-1), which complained: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
48 4 $this->set_tag;
49 }
50  
51 sub set_standard {
52 1 ▶ my $this = shift;
Subroutine set_standard: called CC=3 SCAR=11.1
Pod undocumented
53 1 my $rm = ref $this->{U};
54  
55 1 ▶ if( $rm =~ m/SMA/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rm =~ /SMA/) 1 0 1
56 1 $this->{U} = Math::Business::EMA->new;
57 1 $this->{D} = Math::Business::EMA->new;
58  
59 1 ▶ if( my $d = $this->{days} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $this->{"days"}) 1 0 1
60 1 $this->set_days($d);
61 }
62 }
63 1 $this->set_tag;
64 }
65  
66 sub set_cutler {
67 1 ▶ my $this = shift;
Subroutine set_cutler: called CC=3 SCAR=11.1
Pod undocumented
68 1 my $rm = ref $this->{U};
69  
70 1 ▶ if( $rm =~ m/EMA/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rm =~ /EMA/) 1 0 1
71 1 $this->{U} = Math::Business::SMA->new;
72 1 $this->{D} = Math::Business::SMA->new;
73  
74 1 ▶ if( my $d = $this->{days} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $this->{"days"}) 1 0 1
75 1 $this->set_days($d);
76 }
77 }
78 1 $this->set_tag;
79 }
80  
81 sub set_days {
82 7 ▶ my $this = shift;
Subroutine set_days: called CC=2 SCAR=7.0
Pod undocumented
83 7 my $arg = int(shift);
84  
85 7 ▶ croak "days must be a positive non-zero integer" if $arg <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $arg <= 0 0 7 7
86  
87 7 $this->{U}->set_days($this->{days} = $arg);
88 7 $this->{D}->set_days($arg);
89 7 delete $this->{cy};
90 7 delete $this->{RSI};
91 7 $this->set_tag;
92 }
93  
94 sub set_tag {
95 13 ▶ my $this = shift;
Subroutine set_tag: called CC=2 SCAR=6.9
Pod undocumented
96  
97 13 ▶ if( $this->{U}->isa("Math::Business::EMA") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'U'}->isa('Math::Business::EMA')) { } 11 2 13
98 11 $this->{tag} = "RSI($this->{days})";
99  
100 } else {
101 2 $this->{tag} = "RSI($this->{days},cutler)";
102 }
103 }
104  
105 sub insert {
106 2527 ▶ my $this = shift;
Subroutine insert: called CC=8 SCAR=20.8
Pod undocumented
107 2527 my $close_yesterday = $this->{cy};
108  
109 2527 my $EMA_U = $this->{U};
110 2527 my $EMA_D = $this->{D};
111  
112 2527 ▶ croak "You must set the number of days before you try to insert" if not $this->{days};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"days"} 0 2527 2527
113 2527 ▶ while( defined( my $close_today = shift ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $close_today = shift())) 3277 2527 5804
114 3277 ▶ if( defined $close_yesterday ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $close_yesterday) 3271 6 3277
115 3271 my $delta = $close_today - $close_yesterday;
116  
117 3271 my ($U,$D) = (0,0);
118 3271 ▶ if( $delta > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($delta > 0) { } 1619 1652 3271
elsif ($delta < 0) { } 1635 17 1652
119 1619 $U = $delta;
120 1619 $D = 0;
121  
122 } elsif( $delta < 0 ) {
123 1635 $U = 0;
124 1635 $D = abs $delta;
125 }
126  
127 3271 $EMA_U->insert($U);
128 3271 $EMA_D->insert($D);
129 }
130  
131 3277 ▶ if( defined(my $eu = $EMA_U->query) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $eu = $EMA_U->query)) 3187 90 3277
132 3187 my $ed = $EMA_D->query;
133 3187 ▶ my $rs = (($ed == 0) ? 100 : $eu/$ed ); # NOTE: This is by definition apparently.
Branch: 1 branch
BranchTrueFalseTotal
$ed == 0 ? : 0 3187 3187
134  
135 3187 $this->{RSI} = 100 - 100/(1+$rs);
136 }
137  
138 3277 $close_yesterday = $close_today;
139 }
140  
141 2527 $this->{cy} = $close_yesterday;
142 }
143  
144 sub query {
145 2528 ▶ my $this = shift;
Subroutine query: called CC=1 SCAR=0.0
Pod undocumented
146  
147 2528 return $this->{RSI};
148 }
149  
150 __END__
« blib/lib/Math/Business/MACD.pm ↑ Summary blib/lib/Math/Business/BollingerBands.pm »