| 1 | package Finance::GeniusTrader::CloseStrategy::CloseGain; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 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 | use strict; | ||
| 8 | use vars qw(@ISA @NAMES); | ||
| 9 | |||
| 10 | use Finance::GeniusTrader::CloseStrategy; | ||
| 11 | |||
| 12 | @ISA = qw(Finance::GeniusTrader::CloseStrategy); | ||
| 13 | @NAMES = ("CloseGain[#1]"); | ||
| 14 | |||
| 15 | =head1 Finance::GeniusTrader::CloseStrategy::CloseGain | ||
| 16 | |||
| 17 | This strategy closes the position once the prices have crossed a | ||
| 18 | limit called target. This target is defined as a percentage from the initial | ||
| 19 | price. By default, it's defined as + 25 %. If you use it together with | ||
| 20 | PartialGain, this strategy will only close the remaining shares to be | ||
| 21 | sold/bought. Take care however to place the CloseGain strategy after | ||
| 22 | the PartialGain strategy. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | sub new { | ||
| 26 | my $type = shift; | ||
| 27 | my $class = ref($type) || $type; | ||
| 28 | my $args = shift; | ||
| 29 | |||
| 30 | my $self = { "args" => defined($args) ? $args : [ 25 ] }; | ||
| 31 | |||
| 32 | $self->{'args'}->[0] = 25 if (! defined($self->{'args'}->[0])); | ||
| 33 | |||
| 34 | return manage_object(\@NAMES, $self, $class, $self->{'args'}, ""); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub initialize { | ||
| 38 | my ($self) = @_; | ||
| 39 | $self->{'long_factor'} = 1 + $self->{'args'}[0] / 100; | ||
| 40 | $self->{'short_factor'} = 1 - $self->{'args'}[0] / 100; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub long_position_opened { | ||
| 44 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 45 | |||
| 46 | # Check for other "target" orders | ||
| 47 | my $quantity = $position->quantity; | ||
| 48 | foreach ($position->list_pending_orders) | ||
| 49 | { | ||
| 50 | if ((! $_->discardable) && $_->is_sell_order && $_->is_type_limited) | ||
| 51 | { | ||
| 52 | $quantity -= $_->quantity; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | my $order = $pf_manager->sell_limited_price($calc, $position->source, | ||
| 57 | $position->open_price * $self->{'long_factor'}); | ||
| 58 | $order->set_not_discardable; | ||
| 59 | $order->set_quantity($quantity); | ||
| 60 | $pf_manager->submit_order_in_position($position, $order, $i, $calc); | ||
| 61 | $position->set_no_intent_to_close; | ||
| 62 | |||
| 63 | return; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub short_position_opened { | ||
| 67 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 68 | |||
| 69 | # Check for other "target" orders | ||
| 70 | my $quantity = $position->quantity; | ||
| 71 | foreach ($position->list_pending_orders) | ||
| 72 | { | ||
| 73 | if ((! $_->discardable) && $_->is_buy_order && $_->is_type_limited) | ||
| 74 | { | ||
| 75 | $quantity -= $_->quantity; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | my $order = $pf_manager->buy_limited_price($calc, $position->source, | ||
| 80 | $position->open_price * $self->{'short_factor'}); | ||
| 81 | $order->set_not_discardable; | ||
| 82 | $order->set_quantity($quantity); | ||
| 83 | $pf_manager->submit_order_in_position($position, $order, $i, $calc); | ||
| 84 | $position->set_no_intent_to_close; | ||
| 85 | |||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub manage_long_position { | ||
| 90 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 91 | |||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub manage_short_position { | ||
| 96 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 97 | |||
| 98 | return; | ||
| 99 | } | ||
| 100 |