GeniusTrader/Signals/Systems/MacdDiff.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 85 CC 8 SCAR 27.8
CC
8
Cov
0%
new
30.0
detect
20.0
initialize
2.0
CRAP 16.1
1 package Finance::GeniusTrader::Signals::Systems::MacdDiff;
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 use strict;
8 use vars qw(@ISA @NAMES);
9  
10 use Finance::GeniusTrader::Signals;
11 use Finance::GeniusTrader::Prices;
12 use Finance::GeniusTrader::Indicators::MACD;
13  
14 @ISA = qw(Finance::GeniusTrader::Signals);
15 @NAMES = ("MacdDiffHigh", "MacdDiffLow");
16  
17 =pod
18  
19 =head1 Finance::GeniusTrader::Signals::MacdDiff
20  
21 =cut
22 sub new {
23 my $type = shift;
24 my $class = ref($type) || $type;
25 my $args = shift;
26  
27 my $self = { "args" => defined($args) ? $args : [12,26,9] };
28
29 $args->[0] = 12 if (! defined($args->[0]));
30 $args->[1] = 26 if (! defined($args->[1]));
31 $args->[2] = 9 if (! defined($args->[2]));
32
33 return manage_object(\@NAMES, $self, $class, $self->{'args'}, "");
34 }
35  
36 sub initialize {
37 my ($self) = @_;
38
39 $self->{'macd'} = Finance::GeniusTrader::Indicators::MACD->new($self->{'args'});
40  
41 $self->add_indicator_dependency($self->{'macd'}, 3);
42 }
43  
44 sub detect {
45 my ($self, $calc, $i) = @_;
46 my $q = $calc->prices;
47 my $name_high = $self->get_name(0);
48 my $name_low = $self->get_name(1);
49 my $diff_name = $self->{'macd'}->get_name(2);
50  
51 return if (! $self->check_dependencies($calc, $i));
52  
53 my $today = $calc->indicators->get($diff_name, $i);
54 my $yesterday = $calc->indicators->get($diff_name, $i - 1);
55 my $before = $calc->indicators->get($diff_name, $i - 2);
56 if ( # Yesterday is a local high on macddiff
57 $today < $yesterday and $yesterday > $before
58 )
59 {
60 $calc->signals->set($name_high, $i, 1);
61 } else {
62 $calc->signals->set($name_high, $i, 0);
63 }
64  
65 if ( # Yesterday is a local bottom
66 $yesterday < $today and $yesterday < $before
67 )
68 {
69 $calc->signals->set($name_low, $i, 1);
70 } else {
71 $calc->signals->set($name_low, $i, 0);
72 }
73 }
74  
75 1;