| 1 | package Finance::GeniusTrader::MoneyManagement::CheckVolumeAverage; | ||
| 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::Indicators; | ||
| 12 | use Finance::GeniusTrader::Indicators::SMA; | ||
| 13 | |||
| 14 | @NAMES = ("CheckVolumeAverage[#1,#2]"); | ||
| 15 | @ISA = qw(Finance::GeniusTrader::MoneyManagement); | ||
| 16 | |||
| 17 | =head1 Finance::GeniusTrader::MoneyManagement::CheckVolumeAverage | ||
| 18 | |||
| 19 | =head2 Overview | ||
| 20 | |||
| 21 | This money management rule will keep an eye to the size of each trade to | ||
| 22 | remain them below a fixed percentage of the n-days volume average. | ||
| 23 | |||
| 24 | =head2 Parameters | ||
| 25 | |||
| 26 | By default, we will accept all trades representing less than 1 % of the 5 | ||
| 27 | days average volume and reject others. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | my $type = shift; | ||
| 33 | my $class = ref($type) || $type; | ||
| 34 | my $args = shift; | ||
| 35 | |||
| 36 | my $self = { 'args' => defined($args) ? $args : [ 5, 0.01 ] }; | ||
| 37 | |||
| 38 | $args->[0] = 5 if (! defined($args->[0])); | ||
| 39 | $args->[1] = 0.01 if (! defined($args->[1])); | ||
| 40 | |||
| 41 | return manage_object(\@NAMES, $self, $class, $self->{'args'}, ''); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub initialize { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | $self->{'sma'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}[0] ], "Volume Average", $GET_VOLUME); | ||
| 48 | |||
| 49 | $self->add_indicator_dependency($self->{'sma'}, 1); | ||
| 50 | $self->add_prices_dependency(1); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub manage_quantity { | ||
| 54 | my ($self, $order, $i, $calc, $portfolio) = @_; | ||
| 55 | my $volume_average = $self->{'sma'}; | ||
| 56 | my $volume_average_name = $volume_average->get_name; | ||
| 57 | my $percentage = $self->{'args'}[1]; | ||
| 58 | my $indic = $calc->indicators; | ||
| 59 | |||
| 60 | # Return if all required dependencies aren't already calculated | ||
| 61 | return if (! $self->check_dependencies($calc, $i)); | ||
| 62 | |||
| 63 | # Get N-Days Average Volume value | ||
| 64 | my $volume_average_value = $indic->get($volume_average_name, $i); | ||
| 65 | |||
| 66 | # Calculate the required volume to play the trade | ||
| 67 | my $required_volume = $volume_average_value * $percentage; | ||
| 68 | |||
| 69 | if (defined($order->{'quantity'})) { | ||
| 70 | if ($order->{'quantity'} < $required_volume) { | ||
| 71 | return $order->{'quantity'}; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; |