| 1 | package Finance::GeniusTrader::Indicators::PercentagePosition; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Oliver Bossert | ||
| 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 | use Finance::GeniusTrader::Indicators; | ||
| 16 | |||
| 17 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 18 | @NAMES = ("PercentagePosition[#*]"); | ||
| 19 | @DEFAULT_ARGS = (65, "{I:Prices CLOSE}"); | ||
| 20 | |||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Finance::GeniusTrader::Indicators::PercentagePosition - Relative Position in a certain period | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | This indicators calculates the realtive position in a period. Zero | ||
| 29 | means that a new low is reached and 100 corresponds to a new high. | ||
| 30 | |||
| 31 | =head1 PARAMETERS | ||
| 32 | |||
| 33 | =over | ||
| 34 | |||
| 35 | =item Period 1 | ||
| 36 | |||
| 37 | The number of days in which the indicator looks for a high/low. | ||
| 38 | |||
| 39 | =item Indicator | ||
| 40 | |||
| 41 | The source | ||
| 42 | |||
| 43 | =back | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub initialize { | ||
| 48 | my ($self) = @_; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | sub calculate { | ||
| 53 | my ($self, $calc, $i) = @_; | ||
| 54 | my $name = $self->get_name; | ||
| 55 | my $indic = $calc->indicators; | ||
| 56 | my $nbdays = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 57 | |||
| 58 | return if ($calc->indicators->is_available($name, $i)); | ||
| 59 | |||
| 60 | $self->remove_volatile_dependencies(); | ||
| 61 | $self->add_volatile_prices_dependency($nbdays); | ||
| 62 | return if (! $self->check_dependencies($calc, $i)); | ||
| 63 | |||
| 64 | my $start = $i - $nbdays; | ||
| 65 | $start = 0 if ($nbdays == -1); | ||
| 66 | $start = 0 if ($start < 0); | ||
| 67 | |||
| 68 | my $min = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 69 | my $max = $min; | ||
| 70 | my $actual = $min; | ||
| 71 | for (my $j=$start; $j<=$i; $j++) { | ||
| 72 | my $today = $self->{'args'}->get_arg_values($calc, $j, 2); | ||
| 73 | $min = $today if ($today<$min); | ||
| 74 | $max = $today if ($today>$max); | ||
| 75 | } | ||
| 76 | |||
| 77 | my $res = ($max-$min==0) ? 0 : 100*($actual-$min)/($max-$min); | ||
| 78 | $calc->indicators->set($name, $i, $res); | ||
| 79 | } |