| 1 | package Finance::GeniusTrader::MoneyManagement; | ||
| 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(@ISA @EXPORT %OBJECT_REPOSITORY); | ||
| 9 | |||
| 10 | require Exporter; | ||
| 11 | @ISA = qw(Exporter Finance::GeniusTrader::Dependency); | ||
| 12 | @EXPORT = qw(&build_object_name &manage_object); | ||
| 13 | |||
| 14 | use Finance::GeniusTrader::Registry; | ||
| 15 | use Finance::GeniusTrader::Dependency; | ||
| 16 | use Finance::GeniusTrader::Prices; | ||
| 17 | #ALL# use Log::Log4perl qw(:easy); | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Finance::GeniusTrader::MoneyManagement - Money management rules (risk management) | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Money management rules decide or modify the sum of money | ||
| 26 | placed on each trade. On the extreme side, they can cancel | ||
| 27 | an order by deciding that 0 shares should be bought/sold. | ||
| 28 | |||
| 29 | =over | ||
| 30 | |||
| 31 | =item C<< $mm_rule->manage_quantity($order, $i, $calc, $portfolio) >> | ||
| 32 | |||
| 33 | Return the quantity that the money management rule would put | ||
| 34 | on the given order. $order->{'quantity'} may be already set by a | ||
| 35 | previous money management rule. Never modify the quantity directly | ||
| 36 | but return the new proposed quantity. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | sub manage_quantity { | ||
| 40 | my ($self, $order, $i, $calc, $portfolio) = @_; | ||
| 41 | |||
| 42 | if (defined($order->{'quantity'}) && | ||
| 43 | $order->{'quantity'}) | ||
| 44 | { | ||
| 45 | return $order->{'quantity'}; | ||
| 46 | } else { | ||
| 47 | if (defined($portfolio->{'initial_sum'})) | ||
| 48 | { | ||
| 49 | if ($order->{'price'}) | ||
| 50 | { | ||
| 51 | return int(($portfolio->{'initial_sum'} ) / | ||
| 52 | $order->{'price'}); | ||
| 53 | } else { | ||
| 54 | return int(($portfolio->{'initial_sum'} ) / | ||
| 55 | $calc->prices->at($i)->[$LAST]); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | return 100; | ||
| 61 | } | ||
| 62 | |||
| 63 | # Default initialize that does nothing | ||
| 64 | sub initialize { 1 } | ||
| 65 | |||
| 66 | # Finance::GeniusTrader::Registry functions | ||
| 67 | sub get_registered_object { | ||
| 68 | Finance::GeniusTrader::Registry::get_registered_object(\%OBJECT_REPOSITORY, @_); | ||
| 69 | } | ||
| 70 | sub register_object { | ||
| 71 | Finance::GeniusTrader::Registry::register_object(\%OBJECT_REPOSITORY, @_); | ||
| 72 | } | ||
| 73 | sub get_or_register_object { | ||
| 74 | Finance::GeniusTrader::Registry::get_or_register_object(\%OBJECT_REPOSITORY, @_); | ||
| 75 | } | ||
| 76 | sub manage_object { | ||
| 77 | Finance::GeniusTrader::Registry::manage_object(\%OBJECT_REPOSITORY, @_); | ||
| 78 | } | ||
| 79 | |||
| 80 | =pod | ||
| 81 | |||
| 82 | =back | ||
| 83 | |||
| 84 | =cut | ||
| 85 | 1; |