| 1 | package Finance::GeniusTrader::Indicators::ADX; | ||
| 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 | # $Id$ | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 13 | |||
| 14 | use Finance::GeniusTrader::Indicators; | ||
| 15 | use Finance::GeniusTrader::Indicators::TR; | ||
| 16 | use Finance::GeniusTrader::Indicators::SMA; | ||
| 17 | use Finance::GeniusTrader::Indicators::Generic::SumDownDiffs; | ||
| 18 | use Finance::GeniusTrader::Indicators::Generic::SumUpDiffs; | ||
| 19 | use Finance::GeniusTrader::Indicators::Generic::Sum; | ||
| 20 | use Finance::GeniusTrader::Prices; | ||
| 21 | use Finance::GeniusTrader::Tools qw(:math); | ||
| 22 | |||
| 23 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 24 | @NAMES = ("ADX[#*]","+DMI[#*]","-DMI[#*]","DMI[#*]"); | ||
| 25 | @DEFAULT_ARGS = (14, "{I:Prices HIGH}", "{I:Prices LOW}", "{I:Prices CLOSE}"); | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | Finance::GeniusTrader::Indicators::ADX - ADX | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | volatility index | ||
| 34 | |||
| 35 | =head2 Overview | ||
| 36 | |||
| 37 | =head2 Calculation | ||
| 38 | |||
| 39 | =head2 Examples | ||
| 40 | |||
| 41 | Finance::GeniusTrader::Indicators::ADX->new() | ||
| 42 | Finance::GeniusTrader::Indicators::ADX->new([20]) | ||
| 43 | |||
| 44 | =head2 Validation | ||
| 45 | |||
| 46 | This indicators is validated by the values from comdirect.de. | ||
| 47 | The stock used was the DAX (data from yahoo) at the 04.06.2003: | ||
| 48 | |||
| 49 | ADX[14] [2003-06-04] = 19.9961 (comdirect=20.00) | ||
| 50 | +DMI[14] [2003-06-04] = 28.9251 (comdirect=28.93) | ||
| 51 | -DMI[14] [2003-06-04] = 21.1723 (comdirect=21.17) | ||
| 52 | DMI[14] [2003-06-04] = 15.4754 (comdirect=15.48) | ||
| 53 | |||
| 54 | =head2 Links | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub initialize { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | # Initilize TR (True Range) | ||
| 62 | $self->{'tr'} = Finance::GeniusTrader::Indicators::TR->new( [ $self->{'args'}->get_arg_names(2), | ||
| 63 | $self->{'args'}->get_arg_names(3), | ||
| 64 | $self->{'args'}->get_arg_names(4) ] ); | ||
| 65 | |||
| 66 | $self->{'dm-'} = Finance::GeniusTrader::Indicators::Generic::SumDownDiffs->new([ $self->{'args'}->get_arg_names(1), | ||
| 67 | $self->{'args'}->get_arg_names(3) ]); | ||
| 68 | $self->{'dm+'} = Finance::GeniusTrader::Indicators::Generic::SumUpDiffs->new([ $self->{'args'}->get_arg_names(1), | ||
| 69 | $self->{'args'}->get_arg_names(2) ]); | ||
| 70 | |||
| 71 | my $tr = "{I:TR " . $self->{'args'}->get_arg_names(2) . " " . | ||
| 72 | $self->{'args'}->get_arg_names(3) . " " . | ||
| 73 | $self->{'args'}->get_arg_names(4) . "}"; | ||
| 74 | $self->{'sumtr'} = Finance::GeniusTrader::Indicators::Generic::Sum->new([$self->{'args'}->get_arg_names(1), | ||
| 75 | $tr]); | ||
| 76 | $self->{'sma'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names(1), | ||
| 77 | "{I:Generic:ByName ". $self->get_name(3) . "}" ]); | ||
| 78 | |||
| 79 | } | ||
| 80 | |||
| 81 | sub calculate { | ||
| 82 | my ($self, $calc, $i) = @_; | ||
| 83 | my $indic = $calc->indicators; | ||
| 84 | my $period = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 85 | my $adx_name = $self->get_name(0); | ||
| 86 | my $positive_di_name = $self->get_name(1); | ||
| 87 | my $negative_di_name = $self->get_name(2); | ||
| 88 | my $dmi_name = $self->get_name(3); | ||
| 89 | my $positive_di_value = 0; | ||
| 90 | my $negative_di_value = 0; | ||
| 91 | my $adx_value = 0; | ||
| 92 | my $dmi_value = 0; | ||
| 93 | |||
| 94 | return if ($indic->is_available($adx_name, $i) && | ||
| 95 | $indic->is_available($positive_di_name, $i) && | ||
| 96 | $indic->is_available($dmi_name, $i) && | ||
| 97 | $indic->is_available($negative_di_name, $i)); | ||
| 98 | |||
| 99 | $self->remove_volatile_dependencies(); | ||
| 100 | $self->add_volatile_indicator_dependency($self->{'tr'}, $period); | ||
| 101 | $self->add_volatile_indicator_dependency($self->{'sumtr'}, $period); | ||
| 102 | $self->add_volatile_indicator_dependency($self->{'dm+'}, $period); | ||
| 103 | $self->add_volatile_indicator_dependency($self->{'dm-'}, $period); | ||
| 104 | |||
| 105 | return if (! $self->check_dependencies($calc, $i)); | ||
| 106 | |||
| 107 | if ( ! $indic->is_available($dmi_name, $i-$period) ) { | ||
| 108 | $self->calculate_interval($calc, $i-$period, $i); | ||
| 109 | } | ||
| 110 | |||
| 111 | $positive_di_value = 100 * ( $indic->get($self->{'dm+'}->get_name, $i) / | ||
| 112 | $indic->get($self->{'sumtr'}->get_name, $i)) | ||
| 113 | unless ($indic->get($self->{'sumtr'}->get_name, $i) == 0); | ||
| 114 | |||
| 115 | $negative_di_value = 100 * ( $indic->get($self->{'dm-'}->get_name, $i) / | ||
| 116 | $indic->get($self->{'sumtr'}->get_name, $i)) | ||
| 117 | unless ($indic->get($self->{'sumtr'}->get_name, $i) == 0); | ||
| 118 | |||
| 119 | if ($positive_di_value + $negative_di_value != 0) { | ||
| 120 | $dmi_value = 100 * abs( $positive_di_value - $negative_di_value ) / | ||
| 121 | ( $positive_di_value + $negative_di_value ) ; | ||
| 122 | } else { | ||
| 123 | $dmi_value = 100 * abs( $positive_di_value - $negative_di_value ) / | ||
| 124 | ( 0.000001 + $positive_di_value + $negative_di_value ) ; | ||
| 125 | } | ||
| 126 | |||
| 127 | $indic->set($positive_di_name, $i, $positive_di_value); | ||
| 128 | $indic->set($negative_di_name, $i, $negative_di_value); | ||
| 129 | $indic->set($dmi_name, $i, $dmi_value); | ||
| 130 | $self->{'sma'}->calculate($calc, $i); | ||
| 131 | |||
| 132 | $adx_value = $indic->get($self->{'sma'}->get_name, $i); | ||
| 133 | $indic->set($adx_name, $i, $adx_value); | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | sub calculate_interval { | ||
| 139 | my ($self, $calc, $first, $last) = @_; | ||
| 140 | my $indic = $calc->indicators; | ||
| 141 | my $period = $self->{'args'}->get_arg_constant(1); | ||
| 142 | my $adx_name = $self->get_name(0); | ||
| 143 | my $positive_di_name = $self->get_name(1); | ||
| 144 | my $negative_di_name = $self->get_name(2); | ||
| 145 | my $dmi_name = $self->get_name(3); | ||
| 146 | my $dmi_total = 0; | ||
| 147 | |||
| 148 | return if ($indic->is_available_interval($adx_name, $first, $last) && | ||
| 149 | $indic->is_available_interval($positive_di_name, $first, $last) && | ||
| 150 | $indic->is_available_interval($dmi_name, $first, $last) && | ||
| 151 | $indic->is_available_interval($negative_di_name, $first, $last)); | ||
| 152 | |||
| 153 | $self->remove_volatile_dependencies(); | ||
| 154 | $self->add_volatile_indicator_dependency($self->{'sumtr'}, $period+1); | ||
| 155 | $self->add_volatile_indicator_dependency($self->{'tr'}, $period+1); | ||
| 156 | $self->add_volatile_indicator_dependency($self->{'dm+'}, $period+1); | ||
| 157 | $self->add_volatile_indicator_dependency($self->{'dm-'}, $period+1); | ||
| 158 | |||
| 159 | while (! $self->check_dependencies_interval($calc, $first, $last)) { | ||
| 160 | return if $first == $last; | ||
| 161 | $first++; | ||
| 162 | } | ||
| 163 | |||
| 164 | for (my $i=$first-$period;$i<=$last;$i++) { | ||
| 165 | my $positive_di_value = 0; | ||
| 166 | my $negative_di_value = 0; | ||
| 167 | my $dmi_value = 0; | ||
| 168 | |||
| 169 | $positive_di_value = 100 * ( $indic->get($self->{'dm+'}->get_name, $i) / | ||
| 170 | $indic->get($self->{'sumtr'}->get_name, $i)) | ||
| 171 | unless ($indic->get($self->{'sumtr'}->get_name, $i) == 0); | ||
| 172 | |||
| 173 | $negative_di_value = 100 * ( $indic->get($self->{'dm-'}->get_name, $i) / | ||
| 174 | $indic->get($self->{'sumtr'}->get_name, $i)) | ||
| 175 | unless ($indic->get($self->{'sumtr'}->get_name, $i) == 0); | ||
| 176 | |||
| 177 | if ($positive_di_value + $negative_di_value != 0) { | ||
| 178 | $dmi_value = 100 * abs( $positive_di_value - $negative_di_value ) / | ||
| 179 | ( $positive_di_value + $negative_di_value ) ; | ||
| 180 | } else { | ||
| 181 | $dmi_value = 100 * abs( $positive_di_value - $negative_di_value ) / | ||
| 182 | ( 0.000001 + $positive_di_value + $negative_di_value ) ; | ||
| 183 | } | ||
| 184 | |||
| 185 | $indic->set($positive_di_name, $i, $positive_di_value); | ||
| 186 | $indic->set($negative_di_name, $i, $negative_di_value); | ||
| 187 | $indic->set($dmi_name, $i, $dmi_value); | ||
| 188 | $dmi_total += $dmi_value; | ||
| 189 | |||
| 190 | $self->{'sma'}->calculate($calc, $i); | ||
| 191 | my $adx_value = $indic->get($self->{'sma'}->get_name, $i); | ||
| 192 | $indic->set($adx_name, $i, $adx_value); | ||
| 193 | |||
| 194 | } | ||
| 195 | |||
| 196 | } | ||
| 197 | |||
| 198 | 1; |