| 1 | package Finance::GeniusTrader::MoneyManagement::AntiMartingale; | ||
| 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 = ("AntiMartingale"); | ||
| 14 | @ISA = qw(Finance::GeniusTrader::MoneyManagement); | ||
| 15 | |||
| 16 | =head1 Finance::GeniusTrader::MoneyManagement::AntiMartingale | ||
| 17 | |||
| 18 | =head2 Introduction | ||
| 19 | |||
| 20 | Before reading this you should look at the Martingale system. This system | ||
| 21 | is designed for use on the even chance bets on a roulette table but can be | ||
| 22 | used on any even chance bets. | ||
| 23 | |||
| 24 | =head2 Concept | ||
| 25 | |||
| 26 | Playing the Anti-Martingale is precisely what the name suggests, it's the | ||
| 27 | Martingale in reverse. Instead of doubling your bets after a losing spin | ||
| 28 | you double your bet after a winning spin. Basically you place a bet on an | ||
| 29 | even chance and `let it ride' for a set number of spins. Before starting | ||
| 30 | the system you must decide how many spins you are going to play for, | ||
| 31 | alternatively you may decide to remove your winnings when your nerve | ||
| 32 | breaks or even remove part of the winnings after a number of spins and let | ||
| 33 | the remaining bet continue for a little bit longer. | ||
| 34 | |||
| 35 | =head2 Links | ||
| 36 | |||
| 37 | http://www.casino-help.com/systems/martingale.shtml | ||
| 38 | http://roulette.casino.com/article.pl/aid=mathematical_systems_part_1 | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my $type = shift; | ||
| 44 | my $class = ref($type) || $type; | ||
| 45 | my $args = shift; | ||
| 46 | |||
| 47 | my $self = { 'args' => defined($args) ? $args : [ ] }; | ||
| 48 | |||
| 49 | return manage_object(\@NAMES, $self, $class, $self->{'args'}, ''); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub manage_quantity { | ||
| 53 | my ($self, $order, $i, $calc, $portfolio) = @_; | ||
| 54 | my $prices = $calc->prices; | ||
| 55 | my $code = $order->code; | ||
| 56 | my $source = $order->source; | ||
| 57 | my $no_bad_long_position = 0; | ||
| 58 | my $no_bad_short_position = 0; | ||
| 59 | my $bet_size_from_opened_positions = 0; | ||
| 60 | my $bet_size = 0; | ||
| 61 | my $continue = 1; | ||
| 62 | my $init = 0; | ||
| 63 | |||
| 64 | if (defined($order->{'quantity'})) { | ||
| 65 | |||
| 66 | # Initialize @closed_positions with the list of history positions | ||
| 67 | # already closed and @openning_positions with the list of | ||
| 68 | # positions currently opened; calculte their size. | ||
| 69 | |||
| 70 | my @closed_positions = $portfolio->list_history_positions($code, $source); | ||
| 71 | my @opened_positions = $portfolio->get_position($code, $source); | ||
| 72 | my $size_closed_positions = scalar @closed_positions; | ||
| 73 | my $size_opened_positions = scalar @opened_positions; | ||
| 74 | my $size_real_opened_positions = 0; | ||
| 75 | |||
| 76 | for (my $j = 0; $j < $size_opened_positions; $j++) { | ||
| 77 | |||
| 78 | my $position = $opened_positions[$size_opened_positions - $j - 1]; | ||
| 79 | if ($position) { | ||
| 80 | |||
| 81 | $size_real_opened_positions += 1; | ||
| 82 | if (($i < ($prices->count() - 1)) and ($continue eq 1)) { | ||
| 83 | if ($position->{'long'} eq 1) { | ||
| 84 | if (($prices->at($i+1)->[$OPEN] - $position->{'open_price'}) > 0) { | ||
| 85 | |||
| 86 | # Increase $bet_size because of this loosing long position | ||
| 87 | $bet_size_from_opened_positions += 1; | ||
| 88 | $no_bad_long_position = 0; | ||
| 89 | } else { | ||
| 90 | $no_bad_long_position = 1; | ||
| 91 | } | ||
| 92 | } else { | ||
| 93 | if (($position->{'open_price'} - $prices->at($i+1)->[$OPEN]) > 0) { | ||
| 94 | |||
| 95 | # Increase $bet_size because of this loosing short position | ||
| 96 | $bet_size_from_opened_positions += 1; | ||
| 97 | $no_bad_short_position = 0; | ||
| 98 | } else { | ||
| 99 | $no_bad_short_position = 1; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | if (($no_bad_long_position eq 1) and ($no_bad_short_position eq 1)) { | ||
| 103 | $continue = 0; | ||
| 104 | } else { | ||
| 105 | $init = 1 if ($j eq 0); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | for (my $j = 0; $j < $size_closed_positions; $j++) { | ||
| 112 | |||
| 113 | # Increase the bet size according to the number of previous | ||
| 114 | # loosing trades we made. | ||
| 115 | my $position = $closed_positions[$size_closed_positions - $j - 1]; | ||
| 116 | if ( ($position->stats->{'sold'} > $position->stats->{'bought'}) and ($continue eq 1) ) { | ||
| 117 | $bet_size += 1; | ||
| 118 | } else { | ||
| 119 | $continue = 0; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | # Initialize bet_size to zero when the last already opened position is not a loser | ||
| 124 | if (($init eq 1) and ($bet_size_from_opened_positions < $size_real_opened_positions)) { | ||
| 125 | $bet_size = 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | # Summarize both bet size values and add 1 | ||
| 129 | $bet_size += $bet_size_from_opened_positions + 1; | ||
| 130 | return ($order->{'quantity'} * 2 ** $bet_size); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | 1; |