| 1 | package Finance::GeniusTrader::CloseStrategy::Reinvest::ShortGain; | ||
| 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::ShortGain | ||
| 21 | |||
| 22 | In a long position the gains are "automatically" reinvested since the | ||
| 23 | initial sum and the gains are on the market. With a short position this | ||
| 24 | is no more true. | ||
| 25 | |||
| 26 | This CloseStrategy tries to defeat this by reinvesting the gains each time | ||
| 27 | a certain amount of gain has been made since last time the position was | ||
| 28 | augmented. | ||
| 29 | |||
| 30 | Use this CloseStrategy at the end of the "system chain" so that a position | ||
| 31 | is not augmented if it's planned to be closed. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub initialize { | ||
| 36 | my ($self) = @_; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub long_position_opened { | ||
| 40 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 41 | |||
| 42 | return; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub short_position_opened { | ||
| 46 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 47 | |||
| 48 | $self->{'factor'} = 1 - $self->{'args'}->get_arg_values($calc, $i, 1) / 100; | ||
| 49 | $position->set_attribute("ReinvestShortGain::Limit", | ||
| 50 | $position->open_price * $self->{'factor'}); | ||
| 51 | $position->set_attribute("ReinvestShortGain::PreviousGain", 0); | ||
| 52 | |||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub manage_long_position { | ||
| 57 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 58 | |||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub manage_short_position { | ||
| 63 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 64 | |||
| 65 | $self->{'factor'} = 1 - $self->{'args'}->get_arg_values($calc, $i, 1) / 100; | ||
| 66 | if ($position->has_attribute("ReinvestShortGain::Limit")) | ||
| 67 | { | ||
| 68 | my $limit = $position->attribute("ReinvestShortGain::Limit"); | ||
| 69 | my $prevgain = $position->attribute("ReinvestShortGain::PreviousGain"); | ||
| 70 | my $stats = $position->stats($pf_manager->portfolio); | ||
| 71 | my $price = $calc->prices->at($i)->[$LAST]; | ||
| 72 | if ($price < $limit) | ||
| 73 | { | ||
| 74 | # Augment the position according to the gains accumulated | ||
| 75 | # since the last time | ||
| 76 | my $gains = $stats->{'sold'} - $stats->{'bought'} - | ||
| 77 | $stats->{'cost'} - $position->quantity * $price; | ||
| 78 | my $realgains = $gains - $prevgain; | ||
| 79 | if ($realgains > $price) | ||
| 80 | { | ||
| 81 | my $quantity = int($realgains / $price); | ||
| 82 | my $order = $pf_manager->sell_market_price($calc, | ||
| 83 | $sys_manager->get_name); | ||
| 84 | $order->set_quantity($quantity); | ||
| 85 | $pf_manager->submit_order_in_position($position, $order, | ||
| 86 | $i, $calc); | ||
| 87 | } | ||
| 88 | my $newlimit = $limit * $self->{'factor'}; | ||
| 89 | $position->set_attribute("ReinvestShortGain::Limit", $newlimit); | ||
| 90 | $position->set_attribute("ReinvestShortGain::PreviousGain", $gains); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | return; | ||
| 94 | } | ||
| 95 |