GeniusTrader/Indicators/TRIX.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 61 CC 5 SCAR 26.9
CC
5
Cov
0%
calculate
30.0
initialize
2.0
BEGIN
2.0
CRAP 14.7
1 package Finance::GeniusTrader::Indicators::TRIX;
2  
3 # Copyright 2002 Oliver Bossert
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::ArgsTree;
13 use Finance::GeniusTrader::Indicators;
14 use Finance::GeniusTrader::Indicators::EMA;
15  
16 @ISA = qw(Finance::GeniusTrader::Indicators);
17 @NAMES = ("TRIX[#*]");
18 @DEFAULT_ARGS = (5);
19  
20 =head1 NAME
21  
22 Finance::GeniusTrader::Indicators::TRIX - TRIX-Indicator from Jack Hutson
23  
24 =head1 DESCRIPTION
25  
26 The TRIX-Indicator was developed by John Hutson. It is a 1-day-ROC of
27 a threefold EMA (A EMA of a EMA of a EMA). It is calculated as:
28  
29 TRIX = 100 * ( EMA3(t) - EMA3(t-1) ) / EMA3(t-1)
30  
31 The standard interpretation is that a signal is generated if the TRIX
32 cuts the zero-line. It is a relative stable yet not very effective
33 indicator because it generates the signals very late. You can combine
34 the TRIX with an EMA[9] to generate MACD-like signals.
35  
36 =head2 Parameters
37  
38 =over
39  
40 =item Period (default 5)
41  
42 The first argument is the period used to calculed the average.
43  
44 =back
45  
46 =head2 Creation
47  
48 Finance::GeniusTrader::Indicators::TRIX->new()
49 Finance::GeniusTrader::Indicators::TRIX->new([20])
50  
51 =head2 Link
52  
53 http://www.incrediblecharts.com/technical/trix_indicator.htm
54  
55 =cut
56  
57  
58 sub initialize {
59 my ($self) = @_;
60  
61 $self->{'ema1'} = Finance::GeniusTrader::Indicators::EMA->new( [ $self->{'args'}->get_arg_names() ] );
62  
63 $self->{'ema2'} = Finance::GeniusTrader::Indicators::EMA->new([ $self->{'args'}->get_arg_names(1),
64 "{I:EMA @{[$self->{'ema1'}->{'args'}->get_arg_names()]}}" ]);
65  
66 $self->{'ema3'} = Finance::GeniusTrader::Indicators::EMA->new([ $self->{'args'}->get_arg_names(1),
67 "{I:EMA @{[$self->{'ema2'}->{'args'}->get_arg_names()]}}" ]);
68 }
69  
70 sub calculate {
71 my ($self, $calc, $i) = @_;
72 my $indic = $calc->indicators;
73 my $trix_name = $self->get_name(0);
74 my $nb = $self->{'args'}->get_arg_values($calc, $i, 1);
75  
76 return if (! defined($nb));
77  
78 # Calculate the depencies
79 $self->remove_volatile_dependencies();
80 $self->add_volatile_indicator_dependency( $self->{'ema1'}, $nb * 5 - 4 );
81 $self->add_volatile_indicator_dependency( $self->{'ema2'}, $nb * 4 - 3 );
82 $self->add_volatile_indicator_dependency( $self->{'ema3'}, $nb * 3 - 2 );
83  
84 return if ($calc->indicators->is_available($trix_name, $i));
85 return if (! $self->check_dependencies($calc, $i));
86  
87 my $ema3_today = $indic->get($self->{'ema3'}->get_name, $i);
88 my $ema3_yesterday = $indic->get($self->{'ema3'}->get_name, $i-1);
89 my $trix = ($ema3_yesterday!=0) ? 100 * ( ($ema3_today-$ema3_yesterday)/$ema3_yesterday ) : 0;
90  
91 $indic->set($trix_name, $i, $trix);
92 }
93  
94 1;