| 1 | package Finance::GeniusTrader::Indicators::WilliamsR; | ||
| 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::Indicators::Generic::MinInPeriod; | ||
| 14 | use Finance::GeniusTrader::Indicators::Generic::MaxInPeriod; | ||
| 15 | use Finance::GeniusTrader::Prices; | ||
| 16 | |||
| 17 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 18 | @NAMES = ("WilliamsPercentR[#1]"); | ||
| 19 | @DEFAULT_ARGS = (14, "{I:Prices HIGH}", "{I:Prices LOW}", "{I:Prices CLOSE}"); | ||
| 20 | |||
| 21 | =head1 Finance::GeniusTrader::Indicators::WilliamsR | ||
| 22 | |||
| 23 | =head2 Overview | ||
| 24 | |||
| 25 | Williams %R is a momentum indicator developed by Larry Williams that | ||
| 26 | measures overbought/oversold levels. | ||
| 27 | |||
| 28 | =head2 Calculation | ||
| 29 | |||
| 30 | The formula used to calculate Williams' %R is similar to the Stochastic | ||
| 31 | Oscillator : | ||
| 32 | |||
| 33 | Williams %R = - 100 * ((Highest High (n) - Close) / (Highest High (n) - Lowest Low (n))) | ||
| 34 | |||
| 35 | =head2 Parameters | ||
| 36 | |||
| 37 | The standard Williams %R works with a 14-days parameter : n = 14 | ||
| 38 | |||
| 39 | =head2 Validation | ||
| 40 | |||
| 41 | This Indicator was validated by the data available from comdirect.de: | ||
| 42 | The DAX at 04.06.2003 (data from yahoo.com) had a Williams %R of -5.99. | ||
| 43 | This is consistent with this indicator: -5.99328026152501 | ||
| 44 | |||
| 45 | =head2 Links | ||
| 46 | |||
| 47 | http://www.equis.com/free/taaz/williamspercr.html | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub initialize { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | $self->{'min'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ $self->{'args'}->get_arg_names(1), | ||
| 55 | $self->{'args'}->get_arg_names(3) ] ); | ||
| 56 | $self->{'max'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ $self->{'args'}->get_arg_names(1), | ||
| 57 | $self->{'args'}->get_arg_names(2)] ); | ||
| 58 | |||
| 59 | $self->add_indicator_dependency($self->{'min'}, 1); | ||
| 60 | $self->add_indicator_dependency($self->{'max'}, 1); | ||
| 61 | } | ||
| 62 | |||
| 63 | =head2 Finance::GeniusTrader::Indicators::Williams%R::calculate($calc, $day) | ||
| 64 | |||
| 65 | =cut | ||
| 66 | sub calculate { | ||
| 67 | my ($self, $calc, $i) = @_; | ||
| 68 | my $indic = $calc->indicators; | ||
| 69 | my $min_name = $self->{'min'}->get_name; | ||
| 70 | my $max_name = $self->{'max'}->get_name; | ||
| 71 | my $name = $self->get_name(0); | ||
| 72 | |||
| 73 | $self->remove_volatile_dependencies(); | ||
| 74 | $self->add_volatile_arg_dependency(4, 1); | ||
| 75 | return if (! $self->check_dependencies($calc, $i)); | ||
| 76 | |||
| 77 | return if ($indic->is_available($name, $i)); | ||
| 78 | return if (! $self->check_dependencies($calc, $i)); | ||
| 79 | |||
| 80 | # Get MIN and MAX values | ||
| 81 | my $min = $indic->get($min_name, $i); | ||
| 82 | my $max = $indic->get($max_name, $i); | ||
| 83 | |||
| 84 | # Calculate Williams % R | ||
| 85 | my $williams_percent_r = - 100 * (($max - $self->{'args'}->get_arg_values($calc, $i, 4)) / ($max - $min)); | ||
| 86 | |||
| 87 | # And return the result | ||
| 88 | $indic->set($name, $i, $williams_percent_r); | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; |