| 1 | package Finance::GeniusTrader::Indicators::Chandelier; | ||
| 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::MinInPeriod; | ||
| 14 | use Finance::GeniusTrader::Indicators::Generic::MaxInPeriod; | ||
| 15 | use Finance::GeniusTrader::Indicators::ATR; | ||
| 16 | |||
| 17 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 18 | @NAMES = ("ChanUp[#*]","ChanDn[#*]"); | ||
| 19 | @DEFAULT_ARGS = (22, 3); | ||
| 20 | |||
| 21 | =pod | ||
| 22 | |||
| 23 | =head2 Finance::GeniusTrader::Indicators::Chandelier | ||
| 24 | |||
| 25 | The Chandelier Exit is described in Dr. Alexander Elder's Book "Come into my Trading Room" and provides | ||
| 26 | stops for closing long or short positions. It was originally conceived by Chuck LeBeau. | ||
| 27 | |||
| 28 | It accepts the number of bars to use for the calculation and a coefficient as parameters with 22 and 3 being the defaults that | ||
| 29 | are also used in the examples in the book. | ||
| 30 | |||
| 31 | ChanUp should be used for long positions, ChanDn for short positions. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub initialize { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | $self->{'min'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ $self->{'args'}->get_arg_constant(1), | ||
| 39 | "{I:Prices LOW}" ]); | ||
| 40 | $self->{'max'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ $self->{'args'}->get_arg_constant(1), | ||
| 41 | "{I:Prices HIGH}" ]); | ||
| 42 | $self->{'atr'} = Finance::GeniusTrader::Indicators::ATR->new([ $self->{'args'}->get_arg_names(1) ]); | ||
| 43 | |||
| 44 | $self->add_indicator_dependency($self->{'min'}, 1); | ||
| 45 | $self->add_indicator_dependency($self->{'max'}, 1); | ||
| 46 | $self->add_indicator_dependency($self->{'atr'}, 1); | ||
| 47 | } | ||
| 48 | |||
| 49 | =head2 Finance::GeniusTrader::Indicators::Chandelier::calculate($calc, $day) | ||
| 50 | |||
| 51 | =cut | ||
| 52 | sub calculate { | ||
| 53 | my ($self, $calc, $i) = @_; | ||
| 54 | my $indic = $calc->indicators; | ||
| 55 | my $period = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 56 | my $coeff = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 57 | my $min = $self->{'min'}; | ||
| 58 | my $max = $self->{'max'}; | ||
| 59 | my $atr = $self->{'atr'}; | ||
| 60 | my $chanup_name = $self->get_name(0); | ||
| 61 | my $chandn_name = $self->get_name(1); | ||
| 62 | |||
| 63 | return if ($indic->is_available($chanup_name, $i) && | ||
| 64 | $indic->is_available($chandn_name, $i)); | ||
| 65 | return if (! $self->check_dependencies($calc, $i)); | ||
| 66 | |||
| 67 | my $min_value = $indic->get($min->get_name, $i); | ||
| 68 | my $max_value = $indic->get($max->get_name, $i); | ||
| 69 | my $atr_value = $indic->get($atr->get_name, $i); | ||
| 70 | |||
| 71 | my $chanup_value = $max_value - $coeff * $atr_value; | ||
| 72 | my $chandn_value = $min_value + $coeff * $atr_value; | ||
| 73 | |||
| 74 | $indic->set($chanup_name, $i, $chanup_value); | ||
| 75 | $indic->set($chandn_name, $i, $chandn_value); | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; | ||
| 79 |