| 1 | package Finance::GeniusTrader::Indicators::RSI; | ||
| 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 | |||
| 15 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 16 | @NAMES = ("RSI[#*]"); | ||
| 17 | @DEFAULT_ARGS = (14, "{I:Prices CLOSE}"); | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Finance::GeniusTrader::Indicators::RSI - Relative Strength Index | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | The standard RSI is the RSI 14 days : Finance::GeniusTrader::Indicators::RSI->new() | ||
| 26 | If you need a non standard RSI use for example : Finance::GeniusTrader::Indicators::RSI->new([25]) | ||
| 27 | |||
| 28 | =head2 Validation | ||
| 29 | |||
| 30 | This indicators is validated by the values from comdirect.de. | ||
| 31 | The stock used was the DAX (data from yahoo) at the 04.06.2003: | ||
| 32 | |||
| 33 | RSI[14,{I:Prices CLOSE}][2003-06-04] = 57.5433 (comdirect=57.54) | ||
| 34 | |||
| 35 | =cut | ||
| 36 | sub initialize { | ||
| 37 | my ($self) = @_; | ||
| 38 | |||
| 39 | if ($self->{'args'}->is_constant(1)) { | ||
| 40 | $self->add_prices_dependency($self->{'args'}->get_arg_constant(1) + 1); | ||
| 41 | } else { | ||
| 42 | #no harcoded dependency | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | sub calculate { | ||
| 47 | my ($self, $calc, $i) = @_; | ||
| 48 | my $name = $self->get_name; | ||
| 49 | my $sum_of_won_points = 0; | ||
| 50 | my $sum_of_lost_points = 0; | ||
| 51 | my $rs = 0; | ||
| 52 | my $rsi = 0; | ||
| 53 | |||
| 54 | if (! $self->{'args'}->is_constant(1)) { | ||
| 55 | $self->remove_volatile_dependencies(); | ||
| 56 | $self->add_volatile_prices_dependency($self->{'args'}->get_arg_values($calc, $i, 1)); | ||
| 57 | } | ||
| 58 | |||
| 59 | return if ($calc->indicators->is_available($name, $i)); | ||
| 60 | return if (! $self->check_dependencies($calc, $i)); | ||
| 61 | |||
| 62 | my $nb = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 63 | my $prices = $calc->prices; | ||
| 64 | my $diff = 0; | ||
| 65 | for(my $n = $i - $nb + 1; $n <= $i; $n++) | ||
| 66 | { | ||
| 67 | $diff = $self->{'args'}->get_arg_values($calc, $n, 2) - | ||
| 68 | $self->{'args'}->get_arg_values($calc, $n-1, 2); | ||
| 69 | # Add the won points | ||
| 70 | if ($diff > 0) { | ||
| 71 | $sum_of_won_points += $diff; | ||
| 72 | } | ||
| 73 | # Add the lost points | ||
| 74 | if ($diff < 0) { | ||
| 75 | $sum_of_lost_points -= $diff; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | if ($sum_of_lost_points != 0) { | ||
| 80 | $rs = $sum_of_won_points / $sum_of_lost_points; | ||
| 81 | } | ||
| 82 | |||
| 83 | $rsi = 100 - (100 / (1 + $rs)); | ||
| 84 | $calc->indicators->set($name, $i, $rsi); | ||
| 85 | } |