GeniusTrader/MoneyManagement/Martingale.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 129 CC 12 SCAR 44.6
CC
12
Cov
0%
manage_quantity
132.0
new
6.0
BEGIN
2.0
CRAP 86.6
1 package Finance::GeniusTrader::MoneyManagement::Martingale;
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 = ("Martingale");
14 @ISA = qw(Finance::GeniusTrader::MoneyManagement);
15  
16 =head1 Finance::GeniusTrader::MoneyManagement::Martingale
17  
18 =head2 Introduction
19  
20 The Martingale system is probably the oldest of betting systems, many
21 other systems are based on the basic theory of the Martingale, and so to
22 evaluate most systems you need a full understanding of the Martingale.
23  
24 =head2 Concept
25  
26 The Martingale is a progression system (i.e. you increase your bet after a
27 losing spin) played on the even chance bets on a roulette table although
28 it can be used on even chance bets on other games, and the basic idea is
29 that if you bet on one of the even chance bets (e.g. Red) eventually it
30 will hit. With this in mind, if you increase your bets after each losing
31 spin so that you win back all your losses plus one unit you will always
32 walk away a winner. In order to win all previous losses back plus one unit
33 you simply need to double your bet each time:
34
35 e.g. If you lost four consecutive spins and then won on the fifth spin
36 the outcome of each spin would be (-1) + (-2) + (-4) + (-8) + (+16) = +1
37  
38 You would have placed a total of 31 units at the start of the fifth spin,
39 and when red hit on the fifth spin you would pick up 32 units.
40  
41 =head2 Links
42  
43 http://www.casino-help.com/systems/martingale.shtml
44 http://roulette.casino.com/article.pl/aid=mathematical_systems_part_1
45  
46 =cut
47  
48 sub new {
49 my $type = shift;
50 my $class = ref($type) || $type;
51 my $args = shift;
52
53 my $self = { 'args' => defined($args) ? $args : [ ] };
54  
55 return manage_object(\@NAMES, $self, $class, $self->{'args'}, '');
56 }
57  
58 sub manage_quantity {
59 my ($self, $order, $i, $calc, $portfolio) = @_;
60 my $prices = $calc->prices;
61 my $code = $order->code;
62 my $source = $order->source;
63 my $no_bad_long_position = 0;
64 my $no_bad_short_position = 0;
65 my $bet_size_from_opened_positions = 0;
66 my $bet_size = 0;
67 my $continue = 1;
68 my $init = 0;
69
70 if (defined($order->{'quantity'})) {
71  
72 # Initialize @closed_positions with the list of history positions
73 # already closed and @openning_positions with the list of
74 # positions currently opened; calculte their size.
75
76 my @closed_positions = $portfolio->list_history_positions($code, $source);
77 my @opened_positions = $portfolio->get_position($code, $source);
78 my $size_closed_positions = scalar @closed_positions;
79 my $size_opened_positions = scalar @opened_positions;
80 my $size_real_opened_positions = 0;
81
82 for (my $j = 0; $j < $size_opened_positions; $j++) {
83  
84 my $position = $opened_positions[$size_opened_positions - $j - 1];
85 if ($position) {
86  
87 $size_real_opened_positions += 1;
88 if (($i < ($prices->count() - 1)) and ($continue eq 1)) {
89 if ($position->{'long'} eq 1) {
90 if (($prices->at($i+1)->[$OPEN] - $position->{'open_price'}) < 0) {
91
92 # Increase $bet_size because of this loosing long position
93 $bet_size_from_opened_positions += 1;
94 $no_bad_long_position = 0;
95 } else {
96 $no_bad_long_position = 1;
97 }
98 } else {
99 if (($position->{'open_price'} - $prices->at($i+1)->[$OPEN]) < 0) {
100
101 # Increase $bet_size because of this loosing short position
102 $bet_size_from_opened_positions += 1;
103 $no_bad_short_position = 0;
104 } else {
105 $no_bad_short_position = 1;
106 }
107 }
108 if (($no_bad_long_position eq 1) and ($no_bad_short_position eq 1)) {
109 $continue = 0;
110 } else {
111 $init = 1 if ($j eq 0);
112 }
113 }
114 }
115 }
116  
117 for (my $j = 0; $j < $size_closed_positions; $j++) {
118
119 # Increase the bet size according to the number of previous
120 # loosing trades we made.
121 my $position = $closed_positions[$size_closed_positions - $j - 1];
122 if ( ($position->stats($portfolio)->{'sold'} <=
123 $position->stats($portfolio)->{'bought'})
124 and ($continue eq 1) ) {
125 $bet_size += 1;
126 } else {
127 $continue = 0;
128 }
129 }
130
131 # Initialize bet_size to zero when the last already opened position is not a loser
132 if (($init eq 1) and ($bet_size_from_opened_positions < $size_real_opened_positions)) {
133 $bet_size = 0;
134 }
135  
136 # Summarize both bet size values and add 1
137 $bet_size += $bet_size_from_opened_positions + 1;
138 return ($order->{'quantity'} * 2 ** $bet_size);
139 }
140 }
141  
142 1;