GeniusTrader/MoneyManagement/PositionSizeLimit.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 85 CC 10 SCAR 37.2
CC
10
Cov
0%
manage_quantity
72.0
new
12.0
BEGIN
2.0
CRAP 41.3
1 package Finance::GeniusTrader::MoneyManagement::PositionSizeLimit;
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(@NAMES @ISA);
9  
10 use Finance::GeniusTrader::MoneyManagement;
11 use Finance::GeniusTrader::Prices;
12  
13 @NAMES = ("PositionSizeLimit[#1]");
14 @ISA = qw(Finance::GeniusTrader::MoneyManagement);
15  
16 =head1 Finance::GeniusTrader::MoneyManagement::PositionSizeLimit
17  
18 =head2 Overview
19  
20 This money management rule will keep an eye to the size of each position to
21 remain them below a fixed percentage of the portfolio value.
22  
23 =cut
24  
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 : [100] };
31  
32 $args->[0] = 100 if (! defined($args->[0]));
33
34 return manage_object(\@NAMES, $self, $class, $self->{'args'}, '');
35 }
36  
37 sub manage_quantity {
38 my ($self, $order, $i, $calc, $portfolio) = @_;
39 my $size_limit = ($self->{'args'}[0] / 100);
40 my $code = $order->code;
41 my $source = $order->source;
42 my $maximum_shares = 0;
43 my $shares = 0;
44  
45 # Initialization of portfolio value
46 my $cash = $portfolio->current_cash;
47 my $positions = $portfolio->current_evaluation;
48 my $upcoming_gains_or_losses = $portfolio->current_marged_gains;
49 my $portfolio_value = $cash + $positions + $upcoming_gains_or_losses;
50
51 if (defined($order->quantity)) {
52  
53 # Initialization of $position with the last available position
54 my $open_quantity = 0;
55 foreach my $position ($portfolio->get_position($code, $source))
56 {
57 # Check the position exists
58 next if (! defined($position));
59
60 # Don't count the position that is being closed
61 next if ($position->being_closed);
62
63 # How much shares are on play
64 my $stats = $position->stats($portfolio);
65 if ($position->is_long)
66 {
67 $open_quantity += $position->quantity;
68 } else {
69 $open_quantity -= $position->quantity;
70 }
71  
72 # Well there may be pending orders to grow/reduce the position
73 # but since they may not be executed (in particular orders
74 # for partial close which are in the "pending" state for
75 # numerous days) I don't count them.
76 }
77
78 # Calculate the maximum number of shares we are allowed to trade
79 # either on short or on long position
80 if ($order->price) {
81 $maximum_shares = int($portfolio_value * $size_limit
82 / $order->price);
83 } else {
84 $maximum_shares = int($portfolio_value * $size_limit
85 / $calc->prices->at($i)->[$LAST]);
86 }
87  
88 # Remove the quantity that is already open
89 $maximum_shares -= abs($open_quantity);
90  
91 # Return the number of shares to trade
92 if (($order->quantity > 0) && ($order->quantity < $maximum_shares))
93 {
94 return $order->quantity;
95 } elsif ($maximum_shares > 0) {
96 return $maximum_shares;
97 } else {
98 return 0;
99 }
100 }
101 }
102  
103 1;