GeniusTrader/Indicators/MACD.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 126 CC 10 SCAR 34.1
CC
10
Cov
0%
calculate_interval
56.0
calculate
20.0
initialize
2.0
CRAP 30.1
1 package Finance::GeniusTrader::Indicators::MACD;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
4 # This file is distributed under the terms of the General Public License
5 # version 2 or (at your option) any later version.
6  
7 # Standards-Version: 1.0
8  
9 use strict;
10 use vars qw(@ISA @NAMES @DEFAULT_ARGS);
11  
12 use Finance::GeniusTrader::Indicators;
13 use Finance::GeniusTrader::Indicators::EMA;
14  
15 @ISA = qw(Finance::GeniusTrader::Indicators);
16 @NAMES = ("MACD[#1,#2,#4]","MACDSignal[#3,#4]","MACDDifference[#1,#2,#3,#4]");
17 @DEFAULT_ARGS = (12, 26, 9, "{I:Prices CLOSE}");
18  
19 =pod
20  
21 =head2 Finance::GeniusTrader::Indicators::MACD
22  
23 The standard Moving Average Convergence Divergence (MACD 12-26-9) can be called like that : Finance::GeniusTrader::Indicators::MACD->new()
24  
25 If you need a non standard MACD :
26 Finance::GeniusTrader::Indicators::MACD->new([20, 50, 15])
27  
28 =cut
29  
30 sub initialize {
31 my $self = shift;
32
33 # We need 3 EMA indicators to calculate the MACD
34 $self->{'first_ema'} = Finance::GeniusTrader::Indicators::EMA->new([ $self->{'args'}->get_arg_names(1), $self->{'args'}->get_arg_names(4)]);
35 $self->{'second_ema'} = Finance::GeniusTrader::Indicators::EMA->new([ $self->{'args'}->get_arg_names(2), $self->{'args'}->get_arg_names(4)]);
36 my $diff = "{I:Generic:Eval {I:EMA @{[$self->{'first_ema'}->{'args'}->get_arg_names()]}} - {I:EMA @{[$self->{'second_ema'}->{'args'}->get_arg_names()]}}}";
37 $self->{'third_ema'} = Finance::GeniusTrader::Indicators::EMA->new([ $self->{'args'}->get_arg_names(3), "$diff" ]);
38 }
39  
40 =pod
41  
42 =head2 Finance::GeniusTrader::Indicators::MACD::calculate($calc, $day)
43  
44 =cut
45  
46 sub calculate {
47 my ($self, $calc, $i) = @_;
48 my $indic = $calc->indicators;
49 my $macd_name = $self->get_name(0);
50 my $signal_name = $self->get_name(1);
51 my $diff_name = $self->get_name(2);
52 my $nb = $self->{'args'}->get_arg_values($calc, $i, 2);
53  
54 return if ( !defined($nb) );
55  
56 return if ($indic->is_available($macd_name, $i) &&
57 $indic->is_available($signal_name, $i) &&
58 $indic->is_available($diff_name, $i));
59  
60 $self->remove_volatile_dependencies();
61 $self->add_volatile_indicator_dependency( $self->{'first_ema'}, $nb);
62 $self->add_volatile_indicator_dependency( $self->{'second_ema'}, $nb);
63 $self->add_volatile_indicator_dependency( $self->{'third_ema'}, 1);
64  
65 return if (! $self->check_dependencies($calc, $i));
66  
67 # Get the EMA values and calculate and stores the MACD values
68 my $first_ema_value = $indic->get($self->{'first_ema'}->get_name, $i);
69 my $second_ema_value = $indic->get($self->{'second_ema'}->get_name, $i);
70  
71 #No need to calculate the 3rd EMA here, check_dependecies does it for us
72 #$self->{'third_ema'}->calculate($calc, $i);
73 my $third_ema_value = $indic->get($self->{'third_ema'}->get_name, $i);
74  
75 my $macd = $first_ema_value - $second_ema_value;
76 my $signal = $third_ema_value;
77 my $diff = $macd - $signal;
78  
79 $indic->set($macd_name, $i, $macd);
80 $indic->set($signal_name, $i, $signal);
81 $indic->set($diff_name, $i, $diff);
82 }
83  
84 =pod
85  
86 =head2 Finance::GeniusTrader::Indicators::MACD::calculate_interval($calc, $first, $last)
87  
88 =cut
89  
90 sub calculate_interval {
91 my ($self, $calc, $first, $last) = @_;
92 my $indic = $calc->indicators;
93 my $macd_name = $self->get_name(0);
94 my $signal_name = $self->get_name(1);
95 my $diff_name = $self->get_name(2);
96 my $nb = $self->{'args'}->get_arg_constant(2);
97 my $first_ema_name = $self->{'first_ema'}->get_name(0);
98 my $second_ema_name = $self->{'second_ema'}->get_name(0);
99 my $third_ema_name = $self->{'third_ema'}->get_name(0);
100  
101 ($first, $last) = $self->update_interval($calc, $first, $last);
102  
103 $self->remove_volatile_dependencies();
104 $self->add_volatile_indicator_dependency( $self->{'first_ema'}, $nb);
105 $self->add_volatile_indicator_dependency( $self->{'second_ema'}, $nb);
106 $self->add_volatile_indicator_dependency( $self->{'third_ema'}, 1);
107  
108 while (! $self->check_dependencies_interval($calc, $first, $last)) {
109 return if $first == $last;
110 $first++;
111 }
112 return if ( !defined($nb) );
113  
114 for (my $i=$first;$i<=$last;$i++) {
115  
116 return if (defined($indic->{'values'}{$macd_name}[$i]) ? 1 : 0 &&
117 defined($indic->{'values'}{$signal_name}[$i]) ? 1 : 0 &&
118 defined($indic->{'values'}{$diff_name}[$i]) ? 1 : 0);
119  
120 # Get the EMA values and calculate and stores the MACD values
121 my $first_ema_value = $indic->get($first_ema_name, $i);
122 my $second_ema_value = $indic->get($second_ema_name, $i);
123  
124 #No need to calculate the 3rd EMA here, check_dependecies does it for us
125 #$self->{'third_ema'}->calculate($calc, $i);
126 my $third_ema_value = $indic->get($third_ema_name, $i);
127  
128 my $macd = $first_ema_value - $second_ema_value;
129 my $signal = $third_ema_value;
130 my $diff = $macd - $signal;
131  
132 $indic->set($macd_name, $i, $macd);
133 $indic->set($signal_name, $i, $signal);
134 $indic->set($diff_name, $i, $diff);
135 }
136 }
137  
138 1;