| 1 | package Finance::GeniusTrader::Indicators::SafeZone; | ||
| 2 | |||
| 3 | # Copyright 2000-2003 Raphaƫl Hertzog, Fabien Fulhaber, Oliver Bossert, Joerg Sauer | ||
| 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::MaxInPeriod; | ||
| 14 | use Finance::GeniusTrader::Indicators::Generic::MinInPeriod; | ||
| 15 | use Finance::GeniusTrader::Tools qw(:math); | ||
| 16 | use Finance::GeniusTrader::Prices; | ||
| 17 | |||
| 18 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 19 | @NAMES = ("SafeUp[#*]","SafeDn[#*]"); | ||
| 20 | @DEFAULT_ARGS = (20, 2, 6); | ||
| 21 | |||
| 22 | =pod | ||
| 23 | |||
| 24 | =head2 Finance::GeniusTrader::Indicators::SafeZone | ||
| 25 | |||
| 26 | The SafeZone stop is described in Dr. Alexander Elder's Book "Come into my Trading Room" and provides | ||
| 27 | stops for closing long or short positions. | ||
| 28 | |||
| 29 | It accepts the number of bars to use for the calculation and a coefficient as parameters with 20 and 2 being the defaults that | ||
| 30 | are also used in the examples in the book. The last parameter is the number of days a "plateau" is maintained regardless of | ||
| 31 | of prices moving against the trade. This is to take into account the fact that stops may only be extended in the direction of the trade. | ||
| 32 | After prices have been moving against the trade for the number of bars that is specified by the third parameter it is assumed that the stop | ||
| 33 | was triggered and normal calculation of new stops is resumed. | ||
| 34 | |||
| 35 | If this doesn't seem to make sense just plot this indicator and you will know what I am trying to say. :) | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub initialize { | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | $self->{'min'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ $self->{'args'}->get_arg_constant(3), | ||
| 43 | "{I:Prices HIGH}" ]); | ||
| 44 | $self->{'max'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ $self->{'args'}->get_arg_constant(3), | ||
| 45 | "{I:Prices LOW}" ]); | ||
| 46 | |||
| 47 | $self->add_indicator_dependency($self->{'min'}, 1); | ||
| 48 | $self->add_indicator_dependency($self->{'max'}, 1); | ||
| 49 | $self->add_prices_dependency($self->{'args'}->get_arg_constant(1) + $self->{'args'}->get_arg_constant(3)); | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 Finance::GeniusTrader::Indicators::SafeZone::calculate($calc, $day) | ||
| 53 | |||
| 54 | =cut | ||
| 55 | sub calculate { | ||
| 56 | my ($self, $calc, $i) = @_; | ||
| 57 | my $indic = $calc->indicators; | ||
| 58 | my $period = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 59 | my $coeff = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 60 | my $stickiness = $self->{'args'}->get_arg_values($calc, $i, 3); | ||
| 61 | my $safeup_name = $self->get_name(0); | ||
| 62 | my $safedn_name = $self->get_name(1); | ||
| 63 | my $min = $self->{'min'}; | ||
| 64 | my $max = $self->{'max'}; | ||
| 65 | my $last_sticky_high = 0; | ||
| 66 | my $last_sticky_low = 0; | ||
| 67 | |||
| 68 | return if ($indic->is_available($safeup_name, $i) && | ||
| 69 | $indic->is_available($safedn_name, $i)); | ||
| 70 | return if (! $self->check_dependencies($calc, $i)); | ||
| 71 | |||
| 72 | my @pen_up = (); | ||
| 73 | my @pen_dn = (); | ||
| 74 | my $sum_up = 0; | ||
| 75 | my $sum_dn = 0; | ||
| 76 | |||
| 77 | my $min_value = $indic->get($min->get_name, $i-1); | ||
| 78 | my $max_value = $indic->get($max->get_name, $i-1); | ||
| 79 | |||
| 80 | for (my $n = $i - $stickiness; $n < $i; $n++) { | ||
| 81 | if ($calc->prices->at($n)->[$LOW] eq $max_value) { | ||
| 82 | $last_sticky_high = $n; | ||
| 83 | } | ||
| 84 | if ($calc->prices->at($n)->[$HIGH] eq $min_value) { | ||
| 85 | $last_sticky_low = $n; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | for (my $n = $last_sticky_high - $period + 1; $n <= $last_sticky_high; $n++) { | ||
| 90 | if ($calc->prices->at($n)->[$LOW] < $calc->prices->at($n-1)->[$LOW]) { | ||
| 91 | push @pen_up, $calc->prices->at($n-1)->[$LOW] - $calc->prices->at($n)->[$LOW]; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | for (my $n = $last_sticky_low - $period + 1; $n <= $last_sticky_low; $n++) { | ||
| 95 | if ($calc->prices->at($n)->[$HIGH] > $calc->prices->at($n-1)->[$HIGH]) { | ||
| 96 | push @pen_dn, $calc->prices->at($n)->[$HIGH] - $calc->prices->at($n-1)->[$HIGH]; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | $sum_up += $_ foreach @pen_up; | ||
| 101 | $sum_dn += $_ foreach @pen_dn; | ||
| 102 | |||
| 103 | my $avg_up = @pen_up >= 1 ? $sum_up / @pen_up : $sum_up; | ||
| 104 | my $avg_dn = @pen_dn >= 1 ? $sum_dn / @pen_dn : $sum_dn; | ||
| 105 | |||
| 106 | my @safe_up = (); | ||
| 107 | my @safe_dn = (); | ||
| 108 | |||
| 109 | for (my $n = $i - $stickiness; $n < $i; $n++) { | ||
| 110 | push @safe_up, $calc->prices->at($n)->[$LOW] - $avg_up * $coeff; | ||
| 111 | push @safe_dn, $calc->prices->at($n)->[$HIGH] + $avg_dn * $coeff; | ||
| 112 | } | ||
| 113 | |||
| 114 | my $safeup_value = max (@safe_up); | ||
| 115 | my $safedn_value = min (@safe_dn); | ||
| 116 | |||
| 117 | $indic->set($safeup_name, $i, $safeup_value); | ||
| 118 | $indic->set($safedn_name, $i, $safedn_value); | ||
| 119 | |||
| 120 | } | ||
| 121 | |||
| 122 | 1; | ||
| 123 |