GeniusTrader/TradeFilters/MaxOpenTrades.pm untested

filter: Statementstmt 0.0% 0 / 22 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 33 CC 4 SCAR 25.1
CC
4
Cov
0%
accept_trade
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 12.3
1 package Finance::GeniusTrader::TradeFilters::MaxOpenTrades;
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 # Standards-Version: 1.0
8  
9 use strict;
10 use vars qw(@NAMES @ISA @DEFAULT_ARGS);
11  
12 use Finance::GeniusTrader::TradeFilters;
13  
14 @NAMES = ("MaxOpenTrades[#1]");
15 @ISA = qw(Finance::GeniusTrader::TradeFilters);
16 @DEFAULT_ARGS = (4);
17  
18 =head1 NAME
19  
20 Finance::GeniusTrader::TradeFilters::MaxOpenTrades - Refuse more than N trades
21  
22 =head1 DESCRIPTION
23  
24 This filter refuses a new trade if more than N positions are open. It will
25 however accept it if it's in the process of being closed.
26  
27 =cut
28  
29 sub accept_trade {
30 my ($self, $order, $i, $calc, $portfolio) = @_;
31
32 my @pos = $portfolio->list_open_positions($order->source);
33 my $trades = 0;
34 foreach (@pos)
35 {
36 if ($_->code eq $order->code)
37 {
38 if (! $_->being_closed)
39 {
40 $trades++;
41 }
42 }
43 }
44  
45 if ($trades >= $self->{'args'}->get_arg_constant(1)) {
46 return 0;
47 }
48  
49 return 1;
50 }
51  
52 1;