| 1 | package Finance::GeniusTrader::Indicators::RMI; | ||
| 2 | |||
| 3 | # Copyright 2003 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::Indicators::MOM; | ||
| 14 | use Finance::GeniusTrader::Indicators::EMA; | ||
| 15 | |||
| 16 | |||
| 17 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 18 | @NAMES = ("RMI[#*]", "UpAvg[#*]", "DownAvg[#*]"); | ||
| 19 | @DEFAULT_ARGS = (21,10,"{I:Prices CLOSE}"); | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | Finance::GeniusTrader::Indicators::RMI - Relative Momentum Index | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | =head2 Parameters | ||
| 28 | |||
| 29 | =over | ||
| 30 | |||
| 31 | =item Period (default 5) | ||
| 32 | |||
| 33 | The first argument is the period used to calculed the average. | ||
| 34 | |||
| 35 | =item Moment distance (default 10) | ||
| 36 | |||
| 37 | =back | ||
| 38 | |||
| 39 | =head2 Creation | ||
| 40 | |||
| 41 | Finance::GeniusTrader::Indicators::RMI->new() | ||
| 42 | Finance::GeniusTrader::Indicators::RMI->new([10,20]) | ||
| 43 | |||
| 44 | =head2 Links | ||
| 45 | |||
| 46 | http://www.geocities.com/burzum_3/rmi.html | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub initialize { | ||
| 51 | my ($self) = @_; | ||
| 52 | $self->{'mom'} = Finance::GeniusTrader::Indicators::MOM->new( [ $self->{'args'}->get_arg_names(2), | ||
| 53 | $self->{'args'}->get_arg_names(3) ] ); | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub calculate { | ||
| 58 | my ($self, $calc, $i) = @_; | ||
| 59 | my $indic = $calc->indicators; | ||
| 60 | my $prices = $calc->prices; | ||
| 61 | my $nb1 = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 62 | my $nb2 = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 63 | my $name = $self->get_name(0); | ||
| 64 | my $upname = $self->get_name(1); | ||
| 65 | my $downname = $self->get_name(2); | ||
| 66 | |||
| 67 | return if (! defined($nb1) || ! defined($nb2) ); | ||
| 68 | |||
| 69 | # Calculate the depencies | ||
| 70 | $self->remove_volatile_dependencies(); | ||
| 71 | $self->add_volatile_indicator_dependency( $self->{'mom'}, $nb2 ); | ||
| 72 | |||
| 73 | return if ($calc->indicators->is_available($name, $i)); | ||
| 74 | return if (! $self->check_dependencies($calc, $i)); | ||
| 75 | |||
| 76 | my $downsum = 0; | ||
| 77 | my $upsum = 0; | ||
| 78 | my $upavg = 0; | ||
| 79 | my $downavg = 0; | ||
| 80 | |||
| 81 | my $rmi = 0; | ||
| 82 | my $diff; | ||
| 83 | if ( ! ($calc->indicators->is_available($upname, $i-1) && | ||
| 84 | $calc->indicators->is_available($downname, $i-1)) ) | ||
| 85 | { | ||
| 86 | for(my $n = $i - $nb1 + 1; $n <= $i; $n++) | ||
| 87 | { | ||
| 88 | $diff = $calc->indicators->get($self->{'mom'}->get_name, $n); | ||
| 89 | if ($diff > 0) | ||
| 90 | { | ||
| 91 | $upsum += $diff; | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | $downsum += -$diff; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | $upavg = $upsum / $nb1; | ||
| 98 | $downavg = $downsum / $nb1; | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | $diff = $calc->indicators->get($self->{'mom'}->get_name, $i); | ||
| 102 | if ($diff > 0) | ||
| 103 | { | ||
| 104 | $upsum = $diff; | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | $downsum = -$diff; | ||
| 108 | } | ||
| 109 | $upavg = ($calc->indicators->get($upname, $i-1)*($nb1-1)+$upsum) / $nb1; | ||
| 110 | $downavg = ($calc->indicators->get($downname, $i-1)*($nb1-1)+$downsum) / $nb1; | ||
| 111 | } | ||
| 112 | |||
| 113 | $rmi = ($upavg+$downavg==0) ? 0 : 100 * $upavg / ($upavg+$downavg); | ||
| 114 | |||
| 115 | $calc->indicators->set($name, $i, $rmi); | ||
| 116 | $calc->indicators->set($upname, $i, $upavg); | ||
| 117 | $calc->indicators->set($downname, $i, $downavg); | ||
| 118 | } | ||
| 119 |