| 1 | package Finance::GeniusTrader::CloseStrategy::Stop::KeepRunUp; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 4 | # Modified 2004 by Oliver Bossert | ||
| 5 | # This file is distributed under the terms of the General Public License | ||
| 6 | # version 2 or (at your option) any later version. | ||
| 7 | |||
| 8 | # Standards-Version: 1.0 | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 12 | |||
| 13 | use Finance::GeniusTrader::CloseStrategy; | ||
| 14 | use Finance::GeniusTrader::Indicators::Generic::MaxInPeriod; | ||
| 15 | use Finance::GeniusTrader::Indicators::Generic::MinInPeriod; | ||
| 16 | use Finance::GeniusTrader::Prices; | ||
| 17 | |||
| 18 | @ISA = qw(Finance::GeniusTrader::CloseStrategy); | ||
| 19 | @NAMES = ("KeepRunUp[#*]"); | ||
| 20 | @DEFAULT_ARGS = (10, "{I:Prices LOW}", "{I:Prices HIGH}", "{I:Prices CLOSE}"); | ||
| 21 | |||
| 22 | =head1 Finance::GeniusTrader::CloseStrategy::Stop::KeepRunUp | ||
| 23 | |||
| 24 | =head2 Overview | ||
| 25 | |||
| 26 | This strategy closes the position once the prices have crossed the | ||
| 27 | trailing stop defined as a percentage below the highest high value for a | ||
| 28 | long trade or above the highest low value for a short trade, called the | ||
| 29 | "run up", since the trade is open. The purpose of this strategy is to keep | ||
| 30 | opening profits and avoid to turn profitable trades into loosing ones. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | sub get_indicative_long_stop { | ||
| 35 | my ($self, $calc, $i, $order, $pf_manager, $sys_manager) = @_; | ||
| 36 | my $percentage = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 37 | my $close = $self->{'args'}->get_arg_values($calc, $i, 4); | ||
| 38 | |||
| 39 | return ($close * (1 - $percentage / 100)); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub get_indicative_short_stop { | ||
| 43 | my ($self, $calc, $i, $order, $pf_manager, $sys_manager) = @_; | ||
| 44 | my $percentage = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 45 | my $close = $self->{'args'}->get_arg_values($calc, $i, 4); | ||
| 46 | |||
| 47 | return ($close * (1 + $percentage / 100)); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub long_position_opened { | ||
| 51 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 52 | |||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub short_position_opened { | ||
| 57 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 58 | |||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub manage_long_position { | ||
| 63 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 64 | my $percentage = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 65 | my $date = $position->open_date; | ||
| 66 | my $code = $position->code; | ||
| 67 | my $prices = $calc->prices; | ||
| 68 | |||
| 69 | if ($i >= $prices->date($date)) { | ||
| 70 | |||
| 71 | my $max = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([$date, | ||
| 72 | $self->{'args'}->get_arg_names(3) ]); | ||
| 73 | $max->calculate($calc, $i); | ||
| 74 | |||
| 75 | my $highest_high = $calc->indicators->get($max->get_name, $i); | ||
| 76 | $position->set_stop($highest_high * (1 - $percentage / 100)); | ||
| 77 | } | ||
| 78 | |||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub manage_short_position { | ||
| 83 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 84 | my $percentage = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 85 | |||
| 86 | my $date = $position->open_date; | ||
| 87 | my $code = $position->code; | ||
| 88 | my $prices = $calc->prices; | ||
| 89 | |||
| 90 | if ($i >= $prices->date($date)) { | ||
| 91 | |||
| 92 | my $min = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([$date, | ||
| 93 | $self->{'args'}->get_arg_names(2) ]); | ||
| 94 | $min->calculate($calc, $i); | ||
| 95 | |||
| 96 | my $lowest_low = $calc->indicators->get($min->get_name, $i); | ||
| 97 | $position->set_stop($lowest_low * (1 + $percentage / 100)); | ||
| 98 | } | ||
| 99 | |||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; |