GeniusTrader/MoneyManagement/OrderSizeLimit.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 58 CC 6 SCAR 24.2
CC
6
Cov
0%
manage_quantity
20.0
new
12.0
BEGIN
2.0
CRAP 11.3
1 package Finance::GeniusTrader::MoneyManagement::OrderSizeLimit;
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 = ("OrderSizeLimit[#1]");
14 @ISA = qw(Finance::GeniusTrader::MoneyManagement);
15  
16 =head1 Finance::GeniusTrader::MoneyManagement::OrderSizeLimit
17  
18 =head2 Overview
19  
20 This money management rule will keep an eye to the size of each order 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 $maximum_shares = 0;
41  
42 # Initialization of portfolio value
43 my $cash = $portfolio->current_cash;
44 my $positions = $portfolio->current_evaluation;
45 my $upcoming_gains_or_losses = $portfolio->current_marged_gains;
46 my $portfolio_value = $cash + $positions + $upcoming_gains_or_losses;
47
48 if (defined($order->{'quantity'})) {
49  
50 # Determine the maximum number of shares to trade
51 if ($order->{'price'}) {
52 $maximum_shares = int($portfolio_value * $size_limit / $order->{'price'});
53 } else {
54 $maximum_shares = int($portfolio_value * $size_limit / $calc->prices->at($i)->[$LAST]);
55 }
56  
57 # Limit other money management's eager to bet too much !
58 if ($order->{'quantity'} > $maximum_shares) {
59 return $maximum_shares;
60 } else {
61 return $order->{'quantity'};
62 }
63 }
64 }
65  
66 1;