GeniusTrader/MoneyManagement/CheckCommissions.pm untested

filter: Statementstmt 0.0% 0 / 38 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 / 61 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::CheckCommissions;
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 = ("CheckCommissions[#1]");
14 @ISA = qw(Finance::GeniusTrader::MoneyManagement);
15  
16 =head1 Finance::GeniusTrader::MoneyManagement::CheckCommissions
17  
18 =head2 Overview
19  
20 This money management rule will keep an eye to the size of each trade.
21 Trade only when commissions represents less than a fixed percentage of the
22 investment.
23  
24 =head2 Parameters
25  
26 By default, we will accept all trades where commissions represents less
27 than 1 % of the investment and reject others.
28  
29 =cut
30  
31 sub new {
32 my $type = shift;
33 my $class = ref($type) || $type;
34 my $args = shift;
35
36 my $self = { 'args' => defined($args) ? $args : [ 0.005 ] };
37  
38 $args->[0] = 0.005 if (! defined($args->[0]));
39
40 return manage_object(\@NAMES, $self, $class, $self->{'args'}, '');
41 }
42  
43 sub manage_quantity {
44 my ($self, $order, $i, $calc, $portfolio) = @_;
45 my $percentage = $self->{'args'}[0];
46 my $commission = 0;
47 my $investment = 0;
48  
49 if (defined($order->quantity)) {
50  
51 # This is a bit hackish
52 # A fake order is created with an execution price so that
53 # we can calculate an associated cost (that's only an evaluation)
54
55 my $bidon = Finance::GeniusTrader::Portfolio::Order->new;
56 %{$bidon} = %{$order};
57 $bidon->set_price($calc->prices->at($i)->[$LAST]);
58 $commission = $portfolio->get_order_cost($bidon);
59
60 # Accept all trades where commissions represents less
61 # than 1 % of the investment and reject others.
62
63 if ($order->price) {
64 $investment = $order->quantity * $order->price;
65 } else {
66 $investment = $order->quantity * $calc->prices->at($i)->[$LAST];
67 }
68 if ($commission <= ($investment * $percentage)) {
69 return $order->quantity;
70 } else {
71 return 0;
72 }
73 }
74 }
75  
76 1;