File Coverage

blib/lib/Math/Business/MACD.pm
Criterion Covered Total %
statement 45 59 76.2
branch 10 20 50.0
condition 3 6 50.0
subroutine 11 14 78.5
pod 0 10 0.0
total 69 109 63.3


line stmt bran cond sub pod time code
1             package Math::Business::MACD;
2              
3 1     1   4439 use strict;
  1         8  
  1         23  
4 1     1   5 use warnings;
  1         1  
  1         25  
5 1     1   13 use Carp;
  1         2  
  1         51  
6              
7 1     1   360 use Math::Business::EMA;
  1         2  
  1         480  
8              
9             1;
10              
11 0     0 0 0 sub tag { (shift)->{tag} }
12              
13             sub recommended {
14 0     0 0 0 my $class = shift;
15              
16 0         0 $class->new(26, 12, 9);
17             }
18              
19             sub new {
20 1     1 0 309 my $class = shift;
21              
22 1         19 my $this = bless {
23             slow_EMA => new Math::Business::EMA,
24             fast_EMA => new Math::Business::EMA,
25             trig_EMA => new Math::Business::EMA,
26             days => 0,
27             }, $class;
28              
29 1 50       4 if( @_ == 3 ) {
30 0         0 $this->set_days(@_);
31             }
32              
33 1         4 return $this;
34             }
35              
36             sub set_days {
37 1     1 0 5 my $this = shift;
38 1         4 my ($slow, $fast, $trig) = @_;
39              
40 1 50       2 croak "slow days must be a positive non-zero integers" if $slow <= 0;
41 1 50       4 croak "fast days must be a positive non-zero integers" if $fast <= 0;
42 1 50       3 croak "trig days must be a positive non-zero integers" if $trig <= 0;
43              
44 1         24 $this->{days} = 1;
45              
46 1         6 $this->{slow_EMA}->set_days($slow);
47 1         3 $this->{fast_EMA}->set_days($fast);
48 1         4 $this->{trig_EMA}->set_days($trig);
49              
50 1         10 $this->{tag} = "MACD($fast,$slow,$trig)";
51             }
52              
53 2     2 0 8 sub query_trig_ema { my $this = shift; return $this->{trig_EMA}->query }
  2         3  
54 42     42 0 47 sub query_slow_ema { my $this = shift; return $this->{slow_EMA}->query }
  42         60  
55 42     42 0 43 sub query_fast_ema { my $this = shift; return $this->{fast_EMA}->query }
  42         66  
56              
57             sub query_histogram {
58 0     0 0 0 my $this = shift;
59              
60 0         0 my $m = $this->query;
61 0         0 my $t = $this->query_trig_ema;
62              
63 0 0 0     0 return unless defined($m) and defined($t);
64 0         0 return $m - $t;
65             }
66              
67             sub query {
68 42     42 0 49 my $this = shift;
69              
70 42         49 my $f = $this->query_fast_ema;
71 42         53 my $s = $this->query_slow_ema;
72              
73 42 100 100     110 return unless defined($f) and defined($s);
74 16 50       23 if( wantarray ) {
75 0         0 my $m = $f-$s;
76 0 0       0 my $t = $this->query_trig_ema; return unless defined $t;
  0         0  
77 0         0 my $h = $m-$t;
78              
79 0         0 return ( $m, $f, $s, $t, $h );
80             }
81 16         27 return $f - $s;
82             }
83              
84             sub insert {
85 39     39 0 82 my $this = shift;
86              
87 39 50       65 croak "You must set the number of days before you try to insert" if not $this->{days};
88              
89 39         58 while( defined( my $value = shift ) ) {
90 39         79 $this->{slow_EMA}->insert($value);
91 39         81 $this->{fast_EMA}->insert($value);
92              
93 39         57 my $m = $this->query;
94 39 100       90 $this->{trig_EMA}->insert( $m ) if defined($m);
95             }
96             }
97              
98             __END__