« GeniusTrader/CloseStrategy/LimitPeriodInTheMarket.pm
↑ Summary
GeniusTrader/MoneyManagement/CheckCommissions.pm »
| 1 | package Finance::GeniusTrader::CloseStrategy::Reinvest::InWinners; | ||
| 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::Prices; | ||
| 15 | |||
| 16 | @ISA = qw(Finance::GeniusTrader::CloseStrategy); | ||
| 17 | @NAMES = ("ReinvestShortGain[#1]"); | ||
| 18 | @DEFAULT_ARGS = (15); | ||
| 19 | |||
| 20 | =head1 Finance::GeniusTrader::CloseStrategy::Reinvest::InWinners | ||
| 21 | |||
| 22 | This Position Manager will reinvest money in winning trades every time they meet a new target. This strategy is based on the famous "Let your profits run and cut your losses" while thinking about trend following systems where it is very profitable to bet more when we catched a "big one" ! | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | sub initialize { | ||
| 27 | my ($self) = @_; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub long_position_opened { | ||
| 31 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 32 | |||
| 33 | $self->{'long_factor'} = 1 + $self->{'args'}->get_arg_values($calc, $i, 1) / 100; | ||
| 34 | $position->set_attribute("ReinvestInWinners::LongLimit", $position->open_price * $self->{'long_factor'}); | ||
| 35 | |||
| 36 | return; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub short_position_opened { | ||
| 40 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 41 | |||
| 42 | $self->{'short_factor'} = 1 - $self->{'args'}->get_arg_values($calc, $i, 1) / 100; | ||
| 43 | $position->set_attribute("ReinvestInWinners::ShortLimit", $position->open_price * $self->{'short_factor'}); | ||
| 44 | |||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub manage_long_position { | ||
| 49 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 50 | |||
| 51 | $self->{'long_factor'} = 1 + $self->{'args'}->get_arg_values($calc, $i, 1) / 100; | ||
| 52 | if ($position->has_attribute("ReinvestInWinners::LongLimit")) | ||
| 53 | { | ||
| 54 | my $limit = $position->attribute("ReinvestInWinners::LongLimit"); | ||
| 55 | my $price = $calc->prices->at($i)->[$LAST]; | ||
| 56 | |||
| 57 | if ($price > $limit) | ||
| 58 | { | ||
| 59 | |||
| 60 | # Increase the position size every time we meet a new target | ||
| 61 | my $order = $pf_manager->buy_market_price($calc, $sys_manager->get_name); | ||
| 62 | $pf_manager->decide_quantity($order, $i, $calc); | ||
| 63 | if ($order->quantity > 0) { | ||
| 64 | $pf_manager->submit_order_in_position($position, $order, $i, $calc); | ||
| 65 | } | ||
| 66 | |||
| 67 | my $new_limit = $limit * $self->{'long_factor'}; | ||
| 68 | $position->set_attribute("ReinvestInWinners::LongLimit", $new_limit); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub manage_short_position { | ||
| 75 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 76 | |||
| 77 | $self->{'short_factor'} = 1 - $self->{'args'}->get_arg_values($calc, $i, 1) / 100; | ||
| 78 | if ($position->has_attribute("ReinvestInWinners::ShortLimit")) | ||
| 79 | { | ||
| 80 | my $limit = $position->attribute("ReinvestInWinners::ShortLimit"); | ||
| 81 | my $price = $calc->prices->at($i)->[$LAST]; | ||
| 82 | |||
| 83 | if ($price < $limit) | ||
| 84 | { | ||
| 85 | |||
| 86 | # Increase the position size every time we meet a new target | ||
| 87 | my $order = $pf_manager->sell_market_price($calc, $sys_manager->get_name); | ||
| 88 | $pf_manager->decide_quantity($order, $i, $calc); | ||
| 89 | if ($order->quantity > 0) { | ||
| 90 | $pf_manager->submit_order_in_position($position, $order, $i, $calc); | ||
| 91 | } | ||
| 92 | |||
| 93 | my $new_limit = $limit * $self->{'short_factor'}; | ||
| 94 | $position->set_attribute("ReinvestInWinners::ShortLimit", $new_limit); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | 1; |