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