| 1 | package Finance::GeniusTrader::MoneyManagement::Basic; | ||
| 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 = ("Basic"); | ||
| 14 | @ISA = qw(Finance::GeniusTrader::MoneyManagement); | ||
| 15 | |||
| 16 | =head1 Finance::GeniusTrader::MoneyManagement::Basic | ||
| 17 | |||
| 18 | Basic and dumb money management rules. Invest all cash available (provided | ||
| 19 | that no marged position block the cash - in this money management rule | ||
| 20 | each dollar invested in marged position requires 1 dollar in cash). | ||
| 21 | |||
| 22 | =cut | ||
| 23 | sub new { | ||
| 24 | my $type = shift; | ||
| 25 | my $class = ref($type) || $type; | ||
| 26 | my $args = shift; | ||
| 27 | |||
| 28 | my $self = { 'args' => defined($args) ? $args : [] }; | ||
| 29 | |||
| 30 | return manage_object(\@NAMES, $self, $class, $self->{'args'}, ''); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub manage_quantity { | ||
| 34 | my ($self, $order, $i, $calc, $portfolio) = @_; | ||
| 35 | |||
| 36 | my $date = $calc->prices->at($i)->[$DATE]; | ||
| 37 | if ($portfolio->has_historic_evaluation($date)) { | ||
| 38 | my ($cash, $eval, $gains, $minvest) = $portfolio->get_historic_evaluation($date); | ||
| 39 | my $avail = $cash + $gains - $minvest; | ||
| 40 | foreach my $pos ($portfolio->list_open_positions()) { | ||
| 41 | # Tread marged positions like usual position | ||
| 42 | if ($pos->being_closed()) { | ||
| 43 | $avail += $portfolio->{'position_evaluation'}{$pos->id}; | ||
| 44 | # Note that we forget cash needed to pay the close order | ||
| 45 | # but hey ... we're already not sure of tomorrow's | ||
| 46 | # evaluation. | ||
| 47 | } | ||
| 48 | } | ||
| 49 | my $price = (defined($order->{'price'})) ? $order->{'price'} : $calc->prices->at($i)->[$LAST]; | ||
| 50 | if ($avail > $price) { | ||
| 51 | return int($avail / $price); | ||
| 52 | } else { | ||
| 53 | return 0; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | # No way to decide following our rules | ||
| 57 | if (defined($order->{'quantity'}) && | ||
| 58 | $order->{'quantity'}) | ||
| 59 | { | ||
| 60 | return $order->{'quantity'}; | ||
| 61 | } | ||
| 62 | return 0; | ||
| 63 | |||
| 64 | } | ||
| 65 |