| 1 | package Finance::GeniusTrader::CloseStrategy::LimitPeriodInTheMarket; | ||
| 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 = ("LimitPeriodInTheMarket[#1]"); | ||
| 18 | @DEFAULT_ARGS = (30, "{S:Generic:True}", "{S:Generic:True}"); | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Finance::GeniusTrader::CloseStrategy::LimitPeriodInTheMarket - Only allow the trade to last for X days | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This strategy closes the position once the maximum time for the trade has been reached | ||
| 27 | if the second signal is true. | ||
| 28 | |||
| 29 | The second/third parameter is a signal which can infirm the closing order | ||
| 30 | of long/short position. In particular you may want to not close a position | ||
| 31 | which looks like to be a great winner... | ||
| 32 | |||
| 33 | You confirm the order with a true value and infirm it with a false value. | ||
| 34 | |||
| 35 | =head1 EXAMPLES | ||
| 36 | |||
| 37 | Close a long position if after 3 days, the security hasn't increased. | ||
| 38 | Close a short position if after 3 days, the security hasn't dropped. | ||
| 39 | |||
| 40 | CS:LimitPeriodInTheMarket 3 | ||
| 41 | {S:Generic:Below {I:Prices CLOSE} {I:Generic:PeriodAgo 3 {I:Prices CLOSE}}} | ||
| 42 | {S:Generic:Above {I:Prices CLOSE} {I:Generic:PeriodAgo 3 {I:Prices CLOSE}}} | ||
| 43 | =cut | ||
| 44 | |||
| 45 | sub initialize { | ||
| 46 | my $self = shift; | ||
| 47 | $self->add_arg_dependency(2, 1); | ||
| 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 $initial_period = $calc->prices->date($position->{'open_date'}); | ||
| 65 | my $period_in_the_market = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 66 | |||
| 67 | return if (! $self->check_dependencies($calc, $i)); | ||
| 68 | |||
| 69 | return if (! $self->{'args'}->get_arg_values($calc, $i, 2)); | ||
| 70 | |||
| 71 | if (($i + 1) eq ($initial_period + $period_in_the_market)) { | ||
| 72 | my $order = $pf_manager->sell_market_price($calc, $sys_manager->get_name); | ||
| 73 | $pf_manager->submit_order_in_position($position, $order, $i, $calc); | ||
| 74 | } | ||
| 75 | |||
| 76 | return; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub manage_short_position { | ||
| 80 | my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_; | ||
| 81 | my $initial_period = $calc->prices->date($position->{'open_date'}); | ||
| 82 | my $period_in_the_market = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 83 | |||
| 84 | return if (! $self->check_dependencies($calc, $i)); | ||
| 85 | |||
| 86 | return if (! $self->{'args'}->get_arg_values($calc, $i, 3)); | ||
| 87 | |||
| 88 | if (($i + 1) eq ($initial_period + $period_in_the_market)) { | ||
| 89 | my $order = $pf_manager->buy_market_price($calc, $sys_manager->get_name); | ||
| 90 | $pf_manager->submit_order_in_position($position, $order, $i, $calc); | ||
| 91 | } | ||
| 92 | |||
| 93 | return; | ||
| 94 | } | ||
| 95 |