GeniusTrader/Signals/Trend/TTT.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 116 CC 11 SCAR 40.3
CC
11
Cov
0%
detect
110.0
new
6.0
initialize
2.0
CRAP 56.4
1 package Finance::GeniusTrader::Signals::Trend::TTT;
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::HilbertPeriod;
13 use Finance::GeniusTrader::Indicators::InstantTrendLine;
14  
15 @ISA = qw(Finance::GeniusTrader::Signals);
16 @NAMES = ("TTTUp", "TTTDown");
17  
18 =head1 Finance::GeniusTrader::Signals::Trend::TTT
19  
20 Trade The Trend !! Use Hilbert period to detect the start of a trend.
21  
22 =cut
23 sub new {
24 my $type = shift;
25 my $class = ref($type) || $type;
26 my $args = shift;
27  
28 my $self = { "args" => defined($args) ? $args : [] };
29
30 return manage_object(\@NAMES, $self, $class, $self->{'args'}, "");
31 }
32  
33 sub initialize {
34 my ($self) = @_;
35
36 $self->{'median'} = Finance::GeniusTrader::Indicators::WTCL->new([0]);
37 $self->{'period'} = Finance::GeniusTrader::Indicators::HilbertPeriod->new;
38 $self->{'trend'} = Finance::GeniusTrader::Indicators::InstantTrendLine->new;
39  
40 $self->add_indicator_dependency($self->{'median'}, 2);
41 $self->add_indicator_dependency($self->{'trend'}, 2);
42 }
43  
44 sub detect {
45 my ($self, $calc, $i) = @_;
46 my $indic = $calc->indicators;
47 my $name_up = $self->get_name(0);
48 my $name_down = $self->get_name(1);
49  
50 return if ($calc->signals->is_available($name_up, $i));
51 return if (! $self->check_dependencies($calc, $i));
52
53 my $period = $indic->get($self->{'period'}->get_name, $i);
54  
55 return if ($i < $period);
56
57 $self->{'median'}->calculate_interval($calc, $i - $period, $i);
58 $self->{'trend'}->calculate_interval($calc, $i - $period, $i);
59  
60 return if (! $indic->is_available_interval($self->{'trend'}->get_name,
61 $i - $period, $i));
62
63 my ($above, $below) = (0, 0);
64 for (my $n = 0; $n <= $period; $n++) {
65 if ($indic->get($self->{'median'}->get_name, $i - $n) >
66 $indic->get($self->{'trend'}->get_name, $i - $n))
67 {
68 $above++;
69 last if ($below);
70 } else {
71 $below++;
72 last if ($above);
73 }
74 }
75  
76 if (($above > $below) and ($above >= $period / 2))
77 {
78 $calc->signals->set($name_up, $i, 1);
79 } else {
80 $calc->signals->set($name_up, $i, 0);
81 }
82  
83 if (($below > $above) and ($below >= $period / 2))
84 {
85 $calc->signals->set($name_down, $i, 1);
86 } else {
87 $calc->signals->set($name_down, $i, 0);
88 }
89 }
90  
91 sub detect_interval {
92 my ($self, $calc, $first, $last) = @_;
93  
94 $self->{'period'}->calculate_interval($calc, $first, $last);
95 $self->{'trend'}->calculate_interval($calc, $first, $last);
96 $self->{'median'}->calculate_interval($calc, $first, $last);
97
98 Finance::GeniusTrader::Signals::detect_interval(@_);
99 }
100  
101 1;