GeniusTrader/Indicators/AT3.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 95 CC 3 SCAR 16.1
CC
3
Cov
0%
calculate
12.0
initialize
2.0
BEGIN
2.0
CRAP 5.0
1 package Finance::GeniusTrader::Indicators::AT3;
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 @DEFAULT_ARGS);
9  
10 use Finance::GeniusTrader::Indicators;
11 use Finance::GeniusTrader::Indicators::EMA;
12 use Finance::GeniusTrader::Indicators::RSquare;
13 use Finance::GeniusTrader::Prices;
14  
15 @ISA = qw(Finance::GeniusTrader::Indicators);
16 @NAMES = ("AT3[#1,#2]");
17 @DEFAULT_ARGS = (5, 4.1);
18  
19 =pod
20  
21 =head1 Finance::GeniusTrader::Indicators::AT3
22  
23 =head2 Overview
24  
25 AT3 is an excellent data-fitting technique by Tim Tillson (cf. "Smoothing
26 Techniques For More Accurate Signals" in Technical Analysis of Stocks and
27 Commodities - January 1998) that works with RSquare.
28  
29 =head2 Calculation
30  
31 N is the Exponential Moving Average Period
32 a is the amplification percentage of the filter's response to price
33 movement (0 < a < 1)
34  
35 a = RSquare()
36  
37 e1 = N-days EMA of Close Prices
38 e2 = N-days EMA of e1
39 e3 = N-days EMA of e2
40 e4 = N-days EMA of e3
41 e5 = N-days EMA of e4
42 e6 = N-days EMA of e5
43  
44 c1 = (-a)^3
45 c2 = 3 * a^2
46 c3 = - 6 * a^2 - 3 * a - 3 * a^3
47 c4 = 1 + 3 * a + a^3 + 3 * a^2
48  
49 T3 = c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3
50  
51 =cut
52  
53 sub initialize {
54 my $self = shift;
55
56 # Initialization of RSquare
57 $self->{'rsquare'} = Finance::GeniusTrader::Indicators::RSquare->new([ $self->{'args'}->get_arg_names(2) ]);
58
59 # Initialize e1, e2, e3, e4, e5 and e6
60 my $period = $self->{'args'}->get_arg_names(1);
61 $self->{'e1'} = Finance::GeniusTrader::Indicators::EMA->new([ $period ]);
62
63 $self->{'e2'} = Finance::GeniusTrader::Indicators::EMA->new([ $period, "{I:Generic:ByName " . $self->{'e1'}->get_name . "}" ]);
64
65 $self->{'e3'} = Finance::GeniusTrader::Indicators::EMA->new([ $period, "{I:Generic:ByName " . $self->{'e2'}->get_name . "}" ]);
66
67 $self->{'e4'} = Finance::GeniusTrader::Indicators::EMA->new([ $period, "{I:Generic:ByName " . $self->{'e3'}->get_name . "}" ]);
68
69 $self->{'e5'} = Finance::GeniusTrader::Indicators::EMA->new([ $period, "{I:Generic:ByName " . $self->{'e4'}->get_name . "}" ]);
70
71 $self->{'e6'} = Finance::GeniusTrader::Indicators::EMA->new([ $period, "{I:Generic:ByName " . $self->{'e5'}->get_name . "}" ]);
72  
73 $self->add_indicator_dependency($self->{'e1'}, $period * 5 - 4);
74 $self->add_indicator_dependency($self->{'e2'}, $period * 4 - 3);
75 $self->add_indicator_dependency($self->{'e3'}, $period * 3 - 2);
76 $self->add_indicator_dependency($self->{'e4'}, $period * 2 - 1);
77 $self->add_indicator_dependency($self->{'e5'}, $period);
78 $self->add_indicator_dependency($self->{'e6'}, 1);
79 $self->add_indicator_dependency($self->{'rsquare'}, 1);
80 }
81  
82 =pod
83  
84 =head2 Finance::GeniusTrader::Indicators::T3::calculate($calc, $day, $args, $key, $data)
85  
86 =cut
87  
88 sub calculate {
89 my ($self, $calc, $i) = @_;
90 my $indic = $calc->indicators;
91 my $prices = $calc->prices;
92 my $e1_name = $self->{'e1'}->get_name;
93 my $e2_name = $self->{'e2'}->get_name;
94 my $e3_name = $self->{'e3'}->get_name;
95 my $e4_name = $self->{'e4'}->get_name;
96 my $e5_name = $self->{'e5'}->get_name;
97 my $e6_name = $self->{'e6'}->get_name;
98 my $t3_name = $self->get_name(0);
99 my $period = $self->{'args'}->get_arg_constant(1);
100
101 return if ($indic->is_available($t3_name, $i));
102 return if (! $self->check_dependencies($calc, $i));
103  
104 # Get RSquare Value
105 my $a = $indic->get($self->{'rsquare'}->get_name, $i);
106  
107 # Calculate e1, e2, e3, e4, e5 and e6
108 $self->{'e1'}->calculate_interval($calc, $i - $period * 5 + 5, $i);
109 $self->{'e2'}->calculate_interval($calc, $i - $period * 4 + 4, $i);
110 $self->{'e3'}->calculate_interval($calc, $i - $period * 3 + 3, $i);
111 $self->{'e4'}->calculate_interval($calc, $i - $period * 2 + 2, $i);
112 $self->{'e5'}->calculate_interval($calc, $i - $period + 1, $i);
113 $self->{'e6'}->calculate($calc, $i);
114  
115 # Get e3, e4, e5 and e6
116 my $e3 = $indic->get($self->{'e3'}->get_name, $i);
117 my $e4 = $indic->get($self->{'e4'}->get_name, $i);
118 my $e5 = $indic->get($self->{'e5'}->get_name, $i);
119 my $e6 = $indic->get($self->{'e6'}->get_name, $i);
120  
121 # Calculate c1, c2, c3 and c4
122 my $c1 = (-$a) ** 3;
123 my $c2 = 3 * ($a ** 2) + 3 * ($a ** 3);
124 my $c3 = -6 * ($a ** 2) - 3 * $a - 3 * ($a ** 3);
125 my $c4 = 1 + 3 * $a + ($a ** 3) + 3 * ($a ** 2);
126
127 # Calculate and return T3
128 my $t3 = $c1 * $e6 + $c2 * $e5 + $c3 * $e4 + $c4 * $e3;
129 $indic->set($t3_name, $i, $t3);
130 }
131  
132 1;