GeniusTrader/CloseStrategy.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 131 CC 7 SCAR 16.5
CC
7
Cov
0%
new
20.0
get_indicative_stop
6.0
position_opened
6.0
CRAP 5.2
1 package Finance::GeniusTrader::CloseStrategy;
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::ArgsTree;
15 use Finance::GeniusTrader::Registry;
16 use Finance::GeniusTrader::Dependency;
17 #ALL# use Log::Log4perl qw(:easy);
18  
19 =head1 NAME
20  
21 Finance::GeniusTrader::CloseStrategy - Manages opened positions
22  
23 =head1 DESCRIPTION
24  
25 A CloseStrategy is more really a position manager. Once a system has
26 opened a position, it's managed by a CloseStrategy. Managing means
27 updating the stop and deciding when to close the position.
28  
29 =over
30  
31 =cut
32  
33 sub new {
34 my ($type, $args, $key) = @_;
35 my $class = ref($type) || $type;
36  
37 no strict "refs";
38
39 my $self = { };
40 if (defined($args)) {
41 if ( $#{$args} < $#{"$class\::DEFAULT_ARGS"} ) {
42 for (my $n=($#{$args}+1); $n<=$#{"$class\::DEFAULT_ARGS"}; $n++) {
43 push @{$args}, ${"$class\::DEFAULT_ARGS"}[$n];
44 }
45 }
46 $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(@{$args});
47 } elsif (defined (@{"$class\::DEFAULT_ARGS"})) {
48 $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(@{"$class\::DEFAULT_ARGS"});
49 } else {
50 $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(); # no args
51 }
52  
53 return manage_object(\@{"$class\::NAMES"}, $self, $class, $self->{'args'}, $key);
54 }
55  
56 =item C<< $cs->get_indicative_stop($calc, $i, $order, $pf_man, $sys_man) >>
57  
58 =item C<< $cs->get_indicative_long_stop($calc, $i, $order, $pf_man, $sys_man) >>
59  
60 =item C<< $cs->get_indicative_short_stop($calc, $i, $order, $pf_man, $sys_man) >>
61  
62 This function returns an indicative stop level that should be set for the
63 indicated day. It is used before a position is opened to evaluate
64 a stop level that may be used by a MoneyManagement rule.
65  
66 =item C<< $cs->position_opened($calc, $i, $position, $pf_man, $sys_man) >>
67  
68 =item C<< $cs->short_position_opened($calc, $i, $position, $pf_man, $sys_man) >>
69  
70 =item C<< $cs->long_position_opened($calc, $i, $position, $pf_man, $sys_man) >>
71  
72 Those functions are callback that are launched when a position has been
73 opened. It can be used to place order on a target that will be valid until
74 they are executed (ie no_discard=1). $cs->position_opened will
75 call the right callback depending on the the position (short or long).
76 It can also be used to set an initial stop level.
77  
78 =item C<< $cs->manage_position($calc, $i, $position, $pf_man, $sys_man) >>
79  
80 =item C<< $cs->manage_short_position($calc, $i, $position, $pf_man, $sys_man) >>
81  
82 =item C<< $cs->manage_long_position($calc, $i, $position, $pf_man, $sys_man) >>
83  
84 Manage an open position of the corresponding type. The position may be
85 augmented or reduced by sending new orders modified by
86 $manager->set_order_partial(...). The stop may be updated with
87 $position->set_stop(...).
88  
89 =cut
90 sub get_indicative_stop {
91 my ($self, $calc, $i, $order, $pf_man, $sys_man) = @_;
92  
93 #WAR# WARN "position is defined" if ( defined($order));
94
95 if ($order->is_buy_order) {
96 return $self->get_indicative_long_stop($calc, $i, $order,
97 $pf_man, $sys_man);
98 } else {
99 return $self->get_indicative_short_stop($calc, $i, $order,
100 $pf_man, $sys_man);
101 }
102 }
103  
104 sub position_opened {
105 my ($self, $calc, $i, $position, $pf_man, $sys_man) = @_;
106
107 #WAR# WARN "position is defined" if ( defined($position));
108 #WAR# WARN "position quantity is positive" if ( $position->{'quantity'} > 0);
109
110 if ($position->is_long) {
111 $self->long_position_opened($calc, $i, $position, $pf_man, $sys_man);
112 } else {
113 $self->short_position_opened($calc, $i, $position, $pf_man, $sys_man);
114 }
115 return;
116 }
117  
118 sub manage_position {
119 my ($self, $calc, $i, $position, $pf_man, $sys_man) = @_;
120
121 #WAR# WARN "position is defined" if ( defined($position));
122 #WAR# WARN "position quantity is positive" if ( $position->{'quantity'} > 0);
123
124 if ($position->is_long) {
125 $self->manage_long_position($calc, $i, $position, $pf_man, $sys_man);
126 } else {
127 $self->manage_short_position($calc, $i, $position, $pf_man, $sys_man);
128 }
129 return;
130 }
131  
132 =item C<< $system->precalculate_all($calc) >>
133  
134 =item C<< $system->precalculate_interval($calc, $first, $last) >>
135  
136 If you run a system on a long period of time you may want to precalculate
137 all the indicators in order to benefit of possible optimizations. This is
138 the role of those 2 functions.
139  
140 =cut
141 # Helper functions to optimize calculations
142 sub precalculate_all {
143 my ($self, $calc) = @_;
144 $self->precalculate_interval($calc, 0, $calc->prices->count - 1);
145 return;
146 }
147 sub precalculate_interval {
148 my ($self, $calc, $first, $last) = @_;
149 # Can do nothing, I don't know which indicators are used
150 # This function must be overrident by specific close strategies
151 return;
152 }
153  
154 # Default functions so that they are not mandatory
155 sub get_indicative_long_stop { return 0; }
156 sub get_indicative_short_stop { return 0; }
157 sub long_position_opened { 1; }
158 sub short_position_opened { 1; }
159 sub manage_long_position { 1; }
160 sub manage_short_position { 1; }
161 sub initialize { 1; }
162  
163 =back
164  
165 =head2 Functions to manage a repository of close strategies
166  
167 Finance::GeniusTrader::CloseStrategy::get_registered_object($name);
168 Finance::GeniusTrader::CloseStrategy::register_object($name, $object);
169 Finance::GeniusTrader::CloseStrategy::get_or_register_object($name, $object);
170 Finance::GeniusTrader::CloseStrategy::manage_object(\@NAMES, $object, $class, $args, $key);
171  
172 =cut
173 sub get_registered_object {
174 Finance::GeniusTrader::Registry::get_registered_object(\%OBJECT_REPOSITORY, @_);
175 }
176 sub register_object {
177 Finance::GeniusTrader::Registry::register_object(\%OBJECT_REPOSITORY, @_);
178 }
179 sub get_or_register_object {
180 Finance::GeniusTrader::Registry::get_or_register_object(\%OBJECT_REPOSITORY, @_);
181 }
182 sub manage_object {
183 Finance::GeniusTrader::Registry::manage_object(\%OBJECT_REPOSITORY, @_);
184 }
185  
186 1;