| 1 | package Finance::GeniusTrader::Indicators::UI; | ||
| 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 = ("UI[#*]"); | ||
| 17 | @DEFAULT_ARGS = ("{I:Prices HIGH}", "{I:Prices CLOSE}"); | ||
| 18 | |||
| 19 | =head1 Finance::GeniusTrader::Indicators::UI | ||
| 20 | |||
| 21 | =head2 Overview | ||
| 22 | |||
| 23 | The Ulcer Index (UI) is a risk measurement tool superior to the standard deviation because it differentiates between rising returns and losses. | ||
| 24 | Investors do not view a set of rising returns as a negative sign of volatility, after all; one of the investor goals is to avoid losses. | ||
| 25 | |||
| 26 | =head2 Calculation | ||
| 27 | |||
| 28 | It is the square root of the average of the squared retracements from the latest high. | ||
| 29 | |||
| 30 | =head2 Example | ||
| 31 | |||
| 32 | Finance::GeniusTrader::Indicators::UI->new() | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub initialize { | ||
| 37 | my $self = shift; | ||
| 38 | $self->add_arg_dependency(1,2); | ||
| 39 | $self->add_arg_dependency(2,2); | ||
| 40 | } | ||
| 41 | |||
| 42 | =head2 Finance::GeniusTrader::Indicators::UI::calculate($calc, $day) | ||
| 43 | |||
| 44 | =cut | ||
| 45 | sub calculate { | ||
| 46 | my ($self, $calc, $i) = @_; | ||
| 47 | my $indic = $calc->indicators; | ||
| 48 | my $q = $calc->prices; | ||
| 49 | my $ui_name = $self->get_name(0); | ||
| 50 | my $max = 0; | ||
| 51 | my $retracement = 0; | ||
| 52 | my $squared_retracement = 0; | ||
| 53 | my $sum_of_all_squared_retracements = 0; | ||
| 54 | |||
| 55 | return if ($indic->is_available($ui_name, $i)); | ||
| 56 | return if (! $self->check_dependencies($calc, $i)); | ||
| 57 | |||
| 58 | for (my $n = 0; $n <= $i; $n++) { | ||
| 59 | # We need to know what's the previous Highest High | ||
| 60 | if ( $self->{'args'}->get_arg_values($calc, $n, 1) > $max ) { | ||
| 61 | $max = $self->{'args'}->get_arg_values($calc, $n, 1); | ||
| 62 | } | ||
| 63 | |||
| 64 | if ($max > 0) { | ||
| 65 | $retracement = ($max - $self->{'args'}->get_arg_values($calc, $n, 2)) * 100 / $max; | ||
| 66 | $squared_retracement = $retracement ** 2; | ||
| 67 | $sum_of_all_squared_retracements += $squared_retracement; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | my $average_squared_retracements = $sum_of_all_squared_retracements / ($i + 1); | ||
| 72 | my $ui_value = sqrt($average_squared_retracements); | ||
| 73 | |||
| 74 | # Return the result | ||
| 75 | $indic->set($ui_name, $i, $ui_value); | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; |