| 1 | package Finance::GeniusTrader::MoneyManagement::Alembert; | ||
| 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 = ("Alembert"); | ||
| 14 | @ISA = qw(Finance::GeniusTrader::MoneyManagement); | ||
| 15 | |||
| 16 | =head1 Finance::GeniusTrader::MoneyManagement::Alembert | ||
| 17 | |||
| 18 | =head2 Introduction | ||
| 19 | |||
| 20 | The d'Alembert System is a progression system which tries to win back your | ||
| 21 | losses in small steps instead of all at once like the Martingale. It was | ||
| 22 | designed for use on the even chance bets on a roulette table but can be | ||
| 23 | used on any even chance bet. | ||
| 24 | |||
| 25 | =head2 Concept | ||
| 26 | |||
| 27 | The d'Alembert System works under the assumption that over a period of | ||
| 28 | time there will be an equal number of `reds' and `blacks'. We start the | ||
| 29 | session by placing one unit ($1, $5 or any other value) on one of the even | ||
| 30 | chance bets (e.g. `red'), after a losing spin we increase the next bet by | ||
| 31 | one unit and after a winning bet we decrease the next bet by one unit. So | ||
| 32 | if we were betting oin `red' and the spins were - black, black, black, | ||
| 33 | red, black, red, red, black, red, red, red - then the bets placed would be | ||
| 34 | as follows (the numbers in brackets show the level of your bankroll after | ||
| 35 | the spin): | ||
| 36 | |||
| 37 | 1 (-1), 2 (-3), 3 (-6), 4 (-2), 3 (-5), 4 (-1), 3 (+2), 2 (+0), 3 (+3), 2 | ||
| 38 | (+5), 1 (+6) | ||
| 39 | |||
| 40 | This sequence would end with a win of $6. As you can see, as soon as the | ||
| 41 | number of `reds' is equal to the number of `blacks' plus one then the | ||
| 42 | sequence ends with a win. You may also notice that after the 7th, 9th and | ||
| 43 | 10th spins we were also showing a profit, this is because the bets placed | ||
| 44 | on winning spins are one unit greater than the previous losing spin. | ||
| 45 | Having the possibility of a positive bankroll before the sequence is | ||
| 46 | complete allows us to choose to cut the session short and take a smaller | ||
| 47 | win rather than risking the chance of the session ending badly. | ||
| 48 | |||
| 49 | =head2 Links | ||
| 50 | |||
| 51 | http://www.casino-help.com/systems/dalembert.shtml | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub new { | ||
| 56 | my $type = shift; | ||
| 57 | my $class = ref($type) || $type; | ||
| 58 | my $args = shift; | ||
| 59 | |||
| 60 | my $self = { 'args' => defined($args) ? $args : [ ] }; | ||
| 61 | |||
| 62 | return manage_object(\@NAMES, $self, $class, $self->{'args'}, ''); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub manage_quantity { | ||
| 66 | my ($self, $order, $i, $calc, $portfolio) = @_; | ||
| 67 | my $prices = $calc->prices; | ||
| 68 | my $code = $order->code; | ||
| 69 | my $source = $order->source; | ||
| 70 | my $bet_size = 1; | ||
| 71 | |||
| 72 | if (defined($order->{'quantity'})) { | ||
| 73 | |||
| 74 | # Initialize @closed_positions with the list of history positions | ||
| 75 | # already closed and @openning_positions with the list of | ||
| 76 | # positions currently opened; calculte their size. | ||
| 77 | |||
| 78 | my @closed_positions = $portfolio->list_history_positions($code, $source); | ||
| 79 | my @opened_positions = $portfolio->get_position($code, $source); | ||
| 80 | my $size_closed_positions = scalar @closed_positions; | ||
| 81 | my $size_opened_positions = scalar @opened_positions; | ||
| 82 | |||
| 83 | for (my $j = 0; $j < $size_opened_positions; $j++) { | ||
| 84 | |||
| 85 | my $position = $opened_positions[$size_opened_positions - $j - 1]; | ||
| 86 | if ($position) { | ||
| 87 | |||
| 88 | if ($i < ($prices->count() - 1)) { | ||
| 89 | if ($position->{'long'} eq 1) { | ||
| 90 | if (($prices->at($i+1)->[$OPEN] - $position->{'open_price'}) < 0) { | ||
| 91 | |||
| 92 | # Increase bet_size after this loosing long position | ||
| 93 | $bet_size += 1; | ||
| 94 | } else { | ||
| 95 | |||
| 96 | # Decrease bet_size after this winning long position | ||
| 97 | $bet_size -= 1; | ||
| 98 | } | ||
| 99 | } else { | ||
| 100 | if (($position->{'open_price'} - $prices->at($i+1)->[$OPEN]) < 0) { | ||
| 101 | |||
| 102 | # Increase bet_size after this loosing short position | ||
| 103 | $bet_size += 1; | ||
| 104 | } else { | ||
| 105 | |||
| 106 | # Decrease bet_size after this winning short position | ||
| 107 | $bet_size -= 1; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | for (my $j = 0; $j < $size_closed_positions; $j++) { | ||
| 115 | |||
| 116 | # Increase bet size after each loosing position and decrease | ||
| 117 | # it after each winning position. | ||
| 118 | my $position = $closed_positions[$size_closed_positions - $j - 1]; | ||
| 119 | if ($position->stats->{'sold'} <= $position->stats->{'bought'}) { | ||
| 120 | $bet_size += 1; | ||
| 121 | } | ||
| 122 | if ($position->stats->{'sold'} > $position->stats->{'bought'}) { | ||
| 123 | $bet_size -= 1; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | # Check that bet_size remains alway above one | ||
| 128 | if ($bet_size < 1) { | ||
| 129 | $bet_size = 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | return ($order->{'quantity'} * $bet_size); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; |