| 1 | package Finance::GeniusTrader::Indicators::TR; | ||
| 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 | # Standards-Version: 1.0 | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 11 | |||
| 12 | use Finance::GeniusTrader::Indicators; | ||
| 13 | use Finance::GeniusTrader::Prices; | ||
| 14 | use Finance::GeniusTrader::Tools qw(:math); | ||
| 15 | |||
| 16 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 17 | @NAMES = ("TR[#*]"); | ||
| 18 | @DEFAULT_ARGS = ("{I:Prices HIGH}", "{I:Prices LOW}", "{I:Prices CLOSE}"); | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Finance::GeniusTrader::Indicators::TR - True Range | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | The True Range (TR) is designed to measure the volatility between two days. | ||
| 27 | |||
| 28 | =head2 Calculation | ||
| 29 | |||
| 30 | The True Range is defined as the greatest of the following : | ||
| 31 | |||
| 32 | - The current high less the current low. | ||
| 33 | - The absolute value of : current high less the previous close. | ||
| 34 | - The absolute value of : current low less the previous close. | ||
| 35 | |||
| 36 | =head2 Validation | ||
| 37 | |||
| 38 | The TR is not directly validated but the ATR matches the data from | ||
| 39 | comdirect.de. | ||
| 40 | |||
| 41 | =head2 Links | ||
| 42 | |||
| 43 | http://www.stockcharts.com/education/What/IndicatorAnalysis/indic_ATR.html | ||
| 44 | http://www.equis.com/free/taaz/avertrurang.html | ||
| 45 | |||
| 46 | =cut | ||
| 47 | sub initialize { | ||
| 48 | my ($self) = @_; | ||
| 49 | |||
| 50 | $self->add_arg_dependency(1,2); | ||
| 51 | $self->add_arg_dependency(2,2); | ||
| 52 | $self->add_arg_dependency(3,2); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub calculate { | ||
| 56 | my ($self, $calc, $i) = @_; | ||
| 57 | my $name = $self->get_name; | ||
| 58 | my $tr = 0; | ||
| 59 | |||
| 60 | return if ($calc->indicators->is_available($name, $i)); | ||
| 61 | return if (! $self->check_dependencies($calc, $i)); | ||
| 62 | |||
| 63 | my $prices = $calc->prices; | ||
| 64 | |||
| 65 | my $high = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 66 | my $low = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 67 | my $yesterday_close = $self->{'args'}->get_arg_values($calc, $i-1, 3); | ||
| 68 | |||
| 69 | # A = Today's High - Today's Low | ||
| 70 | my $a = $high - $low; | ||
| 71 | |||
| 72 | # B = Yesterday's Close - Today's High | ||
| 73 | my $b = abs($high - $yesterday_close); | ||
| 74 | |||
| 75 | # C = Yesterday's Close - Today's Low | ||
| 76 | my $c = abs($low - $yesterday_close); | ||
| 77 | |||
| 78 | # TR = max (A, B, C) | ||
| 79 | $tr = max($a, $b, $c); | ||
| 80 | |||
| 81 | # Return the results | ||
| 82 | $calc->indicators->set($name, $i, $tr); | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; |