| 1 | package Finance::GeniusTrader::Indicators::InstantTrendLine; | ||
| 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::Indicators; | ||
| 11 | use Finance::GeniusTrader::Indicators::WMA; | ||
| 12 | use Finance::GeniusTrader::Indicators::WTCL; | ||
| 13 | use Finance::GeniusTrader::Indicators::HilbertPeriod; | ||
| 14 | use Finance::GeniusTrader::Prices; | ||
| 15 | |||
| 16 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 17 | @NAMES = ("InstantTrendLine"); | ||
| 18 | |||
| 19 | =head1 Finance::GeniusTrader::Indicators::InstantTrendLine | ||
| 20 | |||
| 21 | =head2 Overview | ||
| 22 | |||
| 23 | =head2 Calculation | ||
| 24 | |||
| 25 | =head2 Examples | ||
| 26 | |||
| 27 | =head2 Links | ||
| 28 | |||
| 29 | TASC May 2000 - page 22 | ||
| 30 | |||
| 31 | =cut | ||
| 32 | sub initialize { | ||
| 33 | my $self = shift; | ||
| 34 | |||
| 35 | $self->{'median'} = Finance::GeniusTrader::Indicators::WTCL->new([0]); | ||
| 36 | $self->{'period'} = Finance::GeniusTrader::Indicators::HilbertPeriod->new; | ||
| 37 | |||
| 38 | $self->add_indicator_dependency($self->{'period'}, 1); | ||
| 39 | } | ||
| 40 | |||
| 41 | =head2 Finance::GeniusTrader::Indicators::InstantTrendLine::calculate($calc, $day) | ||
| 42 | |||
| 43 | =cut | ||
| 44 | sub calculate { | ||
| 45 | my ($self, $calc, $i) = @_; | ||
| 46 | my $indic = $calc->indicators; | ||
| 47 | my $prices = $calc->prices; | ||
| 48 | |||
| 49 | return if ($indic->is_available($self->get_name, $i)); | ||
| 50 | return if (! $self->check_dependencies($calc, $i)); | ||
| 51 | |||
| 52 | my $period = $indic->get($self->{'period'}->get_name, $i); | ||
| 53 | |||
| 54 | $self->{'median'}->calculate_interval($calc, $i - $period - 1, $i); | ||
| 55 | |||
| 56 | my $trendline = 0; | ||
| 57 | for (my $n = 0; $n <= $period + 1; $n++) | ||
| 58 | { | ||
| 59 | $trendline += $indic->get($self->{'median'}->get_name, $i - $n); | ||
| 60 | } | ||
| 61 | $trendline /= $period + 2; | ||
| 62 | |||
| 63 | $indic->set($self->get_name, $i, $trendline); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub calculate_interval { | ||
| 67 | my ($self, $calc, $first, $last) = @_; | ||
| 68 | |||
| 69 | $self->{'period'}->calculate_interval($calc, $first, $last); | ||
| 70 | $self->{'median'}->calculate_interval($calc, $first, $last); | ||
| 71 | for (my $i = $first; $i <= $last; $i++) | ||
| 72 | { | ||
| 73 | $self->calculate($calc, $i); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | 1; |