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