GeniusTrader/MoneyManagement/FixedRatio.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 6 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 / 56 CC 4 SCAR 18.3
CC
4
Cov
0%
manage_quantity
12.0
new
6.0
BEGIN
2.0
CRAP 6.2
1 package Finance::GeniusTrader::MoneyManagement::FixedRatio;
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 = ("FixedRatio[#1,#2]");
14 @ISA = qw(Finance::GeniusTrader::MoneyManagement);
15  
16 =head1 Finance::GeniusTrader::MoneyManagement::FixedRatio
17  
18 =head2 Overview
19  
20 This money management rule is described in Ryan Jones's book "The Trading
21 Game" as an alternative to the standard Fixed Fractional type of money
22 management rules.
23  
24 =cut
25  
26 sub new {
27 my $type = shift;
28 my $class = ref($type) || $type;
29 my $args = shift;
30
31 my $self = { 'args' => defined($args) ? $args : [] };
32  
33 return manage_object(\@NAMES, $self, $class, $self->{'args'}, '');
34 }
35  
36 sub manage_quantity {
37 my ($self, $order, $i, $calc, $portfolio) = @_;
38  
39 # Initialization of $margin and $delta
40 my $margin = (defined($self->{'args'}[0])) ? $self->{'args'}[0] : $calc->prices->at($i)->[$LAST];
41 my $delta = (defined($self->{'args'}[1])) ? $self->{'args'}[1] : $margin / 100;
42  
43 # Initialization of portfolio value
44 my $cash = $portfolio->current_cash;
45 my $positions = $portfolio->current_evaluation;
46 my $upcoming_gains_or_losses = $portfolio->current_marged_gains;
47 my $portfolio_value = $cash + $positions + $upcoming_gains_or_losses;
48  
49 # Calculate the number of units at which the deltas required and the
50 # margin required to increase to one additional contract occurs.
51 my $number_of_units = $margin / $delta;
52  
53 # Apply the following calculation to determine the starting balance
54 my $total_margin_for_units = $number_of_units * $margin;
55 my $total_required_to_increase_to_units_using_delta = ($number_of_units * $number_of_units - $number_of_units) / 2 * $delta;
56 my $starting_account_balance = $total_margin_for_units - $total_required_to_increase_to_units_using_delta;
57
58 # Calculate the number of shares to trade according to the fixed ratio
59 my $sum = $starting_account_balance;
60 my $number_of_shares = 1;
61
62 while ($sum <= $portfolio_value) {
63 $sum += ($delta * ($number_of_shares - 1));
64 $number_of_shares += 1;
65 }
66
67 return $number_of_shares;
68 }
69  
70 1;