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   4852 use strict;
  1         6  
  1         20  
4 1     1   3 use warnings;
  1         1  
  1         18  
5 1     1   10 use Carp;
  1         1  
  1         43  
6              
7 1     1   276 use Math::Business::EMA;
  1         2  
  1         364  
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 281 my $class = shift;
21              
22 1         8 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       3 if( @_ == 3 ) {
30 0         0 $this->set_days(@_);
31             }
32              
33 1         3 return $this;
34             }
35              
36             sub set_days {
37 1     1 0 4 my $this = shift;
38 1         3 my ($slow, $fast, $trig) = @_;
39              
40 1 50       3 croak "slow days must be a positive non-zero integers" if $slow <= 0;
41 1 50       2 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         18 $this->{days} = 1;
45              
46 1         5 $this->{slow_EMA}->set_days($slow);
47 1         3 $this->{fast_EMA}->set_days($fast);
48 1         3 $this->{trig_EMA}->set_days($trig);
49              
50 1         14 $this->{tag} = "MACD($fast,$slow,$trig)";
51             }
52              
53 2     2 0 7 sub query_trig_ema { my $this = shift; return $this->{trig_EMA}->query }
  2         3  
54 42     42 0 34 sub query_slow_ema { my $this = shift; return $this->{slow_EMA}->query }
  42         50  
55 42     42 0 38 sub query_fast_ema { my $this = shift; return $this->{fast_EMA}->query }
  42         46  
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 36 my $this = shift;
69              
70 42         44 my $f = $this->query_fast_ema;
71 42         48 my $s = $this->query_slow_ema;
72              
73 42 100 100     78 return unless defined($f) and defined($s);
74 16 50       20 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         22 return $f - $s;
82             }
83              
84             sub insert {
85 39     39 0 62 my $this = shift;
86              
87 39 50       77 croak "You must set the number of days before you try to insert" if not $this->{days};
88              
89 39         65 while( defined( my $value = shift ) ) {
90 39         61 $this->{slow_EMA}->insert($value);
91 39         70 $this->{fast_EMA}->insert($value);
92              
93 39         42 my $m = $this->query;
94 39 100       80 $this->{trig_EMA}->insert( $m ) if defined($m);
95             }
96             }
97              
98             __END__