GeniusTrader/CloseStrategy/Conditional/TradeFilter.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 90 CC 6 SCAR 15.8
CC
6
Cov
0%
new
6.0
long_position_opened
6.0
short_position_opened
6.0
CRAP 4.9
1 package Finance::GeniusTrader::CloseStrategy::Conditional::TradeFilter;
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 @NAMES);
9  
10 use Finance::GeniusTrader::CloseStrategy;
11 use Finance::GeniusTrader::Eval;
12  
13 @ISA = qw(Finance::GeniusTrader::CloseStrategy);
14 @NAMES = ("ConditionalTF[#*]");
15  
16 =head1 Finance::GeniusTrader::CloseStrategy::OppositeSignal
17  
18 This strategy closes the position once the opposite signal has been emitted
19 by the system. It will will close a long position on a sell signal and
20 close a short position on a buy signal.
21  
22 =head2 Arguments
23  
24 The arguments taken by this object are special. The first argument is
25 the name of a TradeFilter to use as the condition. It may be followed
26 by argument to give to the TradeFilter at creation time. After that,
27 there's the name of the real CloseStrategy to apply. This strategy will
28 only be applied if the trade filter accepts a fake "close order".
29  
30 Examples or arguments :
31  
32 ...->new("TF:AroonTrend", "CS:Stop:SAR");
33 ...->new("TF:FollowTrend", 15, "CS:Stop:SAR", 0.05, 0.02, 0.02);
34  
35 The system detects the end of the arguments of the TradeFilter once it
36 detects "CS:" or "CloseStrategy:" at the beginning of the next argument.
37  
38 =cut
39 sub new {
40 my $type = shift;
41 my $class = ref($type) || $type;
42 my $args = shift;
43  
44 # Complicated hack to build the other objects according to the args
45 my (@tf, @cs);
46 push @tf, shift @{$args};
47 my $elt;
48 while ($elt = shift @{$args})
49 {
50 if ($elt =~ /^(CloseStrategy:|CS:)/)
51 {
52 push @cs, $elt;
53 last;
54 }
55 push @tf, $elt;
56 }
57 push @cs, @{$args};
58
59 my $tf_obj = create_standard_object(@tf);
60 my $cs_obj = create_standard_object(@cs);
61 @tf = split /\s+/, get_standard_name($tf_obj);
62 @cs = split /\s+/, get_standard_name($cs_obj);
63  
64 my $self = { "args" => [ @tf, @cs ],
65 "tf" => $tf_obj, "cs" => $cs_obj };
66  
67 return manage_object(\@NAMES, $self, $class, $self->{'args'}, "");
68 }
69  
70 sub long_position_opened {
71 my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_;
72  
73 my $order = $pf_manager->sell_market_price($calc, $sys_manager->get_name);
74 $order->set_quantity($position->quantity);
75 if ($self->{'tf'}->accept_trade($order, $i, $calc, $pf_manager->portfolio))
76 {
77 $self->{'cs'}->long_position_opened($calc, $i, $position,
78 $pf_manager, $sys_manager);
79 }
80  
81 return;
82 }
83  
84 sub short_position_opened {
85 my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_;
86  
87 my $order = $pf_manager->sell_market_price($calc, $sys_manager->get_name);
88 $order->set_quantity($position->quantity);
89 if ($self->{'tf'}->accept_trade($order, $i, $calc, $pf_manager->portfolio))
90 {
91 $self->{'cs'}->short_position_opened($calc, $i, $position,
92 $pf_manager, $sys_manager);
93 }
94  
95 return;
96 }
97  
98 sub manage_long_position {
99 my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_;
100
101 my $order = $pf_manager->sell_market_price($calc, $sys_manager->get_name);
102 $order->set_quantity($position->quantity);
103 if ($self->{'tf'}->accept_trade($order, $i, $calc, $pf_manager->portfolio))
104 {
105 $self->{'cs'}->manage_long_position($calc, $i, $position,
106 $pf_manager, $sys_manager);
107 }
108  
109 return;
110 }
111  
112 sub manage_short_position {
113 my ($self, $calc, $i, $position, $pf_manager, $sys_manager) = @_;
114
115 my $order = $pf_manager->buy_market_price($calc, $sys_manager->get_name);
116 $order->set_quantity($position->quantity);
117 if ($self->{'tf'}->accept_trade($order, $i, $calc, $pf_manager->portfolio))
118 {
119 $self->{'cs'}->manage_short_position($calc, $i, $position,
120 $pf_manager, $sys_manager);
121 }
122  
123 return;
124 }
125