| 1 | package Finance::GeniusTrader::Indicators::TMA; | ||
| 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 | |||
| 12 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 13 | @NAMES = ("TMA[#1,#2]"); | ||
| 14 | @DEFAULT_ARGS = (20, "{I:Prices CLOSE}"); | ||
| 15 | |||
| 16 | =head1 Finance::GeniusTrader::Indicators::TMA | ||
| 17 | |||
| 18 | =head2 Overview | ||
| 19 | |||
| 20 | Triangular Moving Averages (TMA) place the majority of the weight on the middle portion of the price series. | ||
| 21 | |||
| 22 | =head2 Calculation | ||
| 23 | |||
| 24 | TMA(5) = (1/9) * (1 * Close(i) + 2 * Close(i - 1) + 3 * Close(i - 2) + 2 * Close(i - 3) + 1 * Close(i - 4)) | ||
| 25 | |||
| 26 | =head2 Examples | ||
| 27 | |||
| 28 | Finance::GeniusTrader::Indicators::TMA->new() | ||
| 29 | Finance::GeniusTrader::Indicators::TMA->new([50]) | ||
| 30 | Finance::GeniusTrader::Indicators::TMA->new([30], {I:Prices OPEN}) | ||
| 31 | |||
| 32 | =head2 Links | ||
| 33 | |||
| 34 | http://www.equis.com/free/taaz/movingaverages.html | ||
| 35 | http://www.ivorix.com/en/products/tech/smooth/tma.html | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub initialize { | ||
| 40 | my ($self) = @_; | ||
| 41 | |||
| 42 | $self->add_prices_dependency($self->{'args'}->get_arg_constant(1)); | ||
| 43 | } | ||
| 44 | |||
| 45 | =head2 Finance::GeniusTrader::Indicators::TMA::calculate($calc, $day) | ||
| 46 | |||
| 47 | =cut | ||
| 48 | sub calculate { | ||
| 49 | my ($self, $calc, $i) = @_; | ||
| 50 | my $nb = $self->{'args'}->get_arg_constant(1); | ||
| 51 | my $name = $self->get_name; | ||
| 52 | my $weight = 0; | ||
| 53 | my $sum_of_weight = 0; | ||
| 54 | my $sum = 0; | ||
| 55 | my $up = 1; | ||
| 56 | my $first = 0; | ||
| 57 | |||
| 58 | return if ($calc->indicators->is_available($name, $i)); | ||
| 59 | return if (! $self->check_dependencies($calc, $i)); | ||
| 60 | |||
| 61 | my $max_weight = int(($nb / 2) + 0.99); | ||
| 62 | |||
| 63 | for(my $n = $i - $nb + 1; $n <= $i; $n++) | ||
| 64 | { | ||
| 65 | # Determine the weights | ||
| 66 | if ($up eq 1) { | ||
| 67 | if ($weight < $max_weight) { | ||
| 68 | $weight += 1; | ||
| 69 | } | ||
| 70 | if (($weight eq $max_weight) and (($nb / 2) != $max_weight)) { | ||
| 71 | $up = 0; | ||
| 72 | } | ||
| 73 | if (($weight eq $max_weight) and (($nb / 2) eq $max_weight)) { | ||
| 74 | if ($first != 0) { | ||
| 75 | $up = 0; | ||
| 76 | } else { | ||
| 77 | $first = 1; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } else { | ||
| 81 | $weight -= 1; | ||
| 82 | } | ||
| 83 | $sum += $self->{'args'}->get_arg_values($calc, $n, 2) * $weight; | ||
| 84 | $sum_of_weight += $weight; | ||
| 85 | } | ||
| 86 | $calc->indicators->set($name, $i, $sum / $sum_of_weight); | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; |