GeniusTrader/Dependency.pm untested

filter: Statementstmt 0.0% 0 / 166 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 286 CC 30 SCAR 23.5
CC
30
Cov
0%
get_prices_dependency
20.0
add_prices_dependency
12.0
add_volatile_prices_dependency
12.0
CRAP 10.5
1 package Finance::GeniusTrader::Dependency;
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();
9  
10 #ALL# use Log::Log4perl qw(:easy);
11  
12 =head1 NAME
13  
14 Finance::GeniusTrader::Dependency - A dependency system for indicators/signals/systems.
15  
16 =head1 DESCRIPTION
17  
18 This module is inheritated by any object which needs a Dependency
19 mechanism. That's why it's listed in @ISA of Finance::GeniusTrader::Indicators, Finance::GeniusTrader::Signals,
20 GT:Systems and several other modules ...
21  
22 =over
23  
24 =item C<< $object->add_indicator_dependency($indic, $nbdays) >>
25  
26 =item C<< $object->add_signal_dependency($signal, $nbdays) >>
27  
28 =item C<< $object->add_prices_dependency($nbdays) >>
29  
30 =item C<< $object->add_arg_dependency($argnum, $nbdays) >>
31  
32 =item C<< $object->add_volatile_indicator_dependency($indic, $nbdays) >>
33  
34 =item C<< $object->add_volatile_signal_dependency($signal, $nbdays) >>
35  
36 =item C<< $object->add_volatile_prices_dependency($nbdays) >>
37  
38 =item C<< $object->add_volatile_arg_dependency($argnum, $nbdays) >>
39  
40 Add a dependency on a precise indicator or signal. The current object
41 needs nbdays of history (current day included) on the given indicator
42 or signal to be able to produce a result. A prices dependency indicates
43 the number of days of history prices
44  
45 A volatile dependency will be removed by $object->remove_volatile_dependencies().
46 It is used for adding last-minute dependencies whose values are known late
47 because they are computed based on other data.
48  
49 =cut
50 sub add_indicator_dependency {
51 my ($self, $indicator, $nbdays) = @_;
52 #ERR# ERROR "Bad dependency" if ( ref($indicator) =~ /Finance::GeniusTrader::Indicators/);
53  
54 if ($self->{'indic_depends'})
55 {
56 push @{$self->{'indic_depends'}}, { 'indicator' => $indicator,
57 'nbdays' => $nbdays };
58 } else {
59 $self->{'indic_depends'}[0] = { 'indicator' => $indicator,
60 'nbdays' => $nbdays };
61 }
62  
63 return;
64 }
65 sub add_signal_dependency {
66 my ($self, $signal, $nbdays) = @_;
67 #ERR# ERROR "Bad dependency" if ( ref($signal) =~ /Finance::GeniusTrader::Signals/);
68
69 if ($self->{'sig_depends'})
70 {
71 push @{$self->{'sig_depends'}}, { 'signal' => $signal,
72 'nbdays' => $nbdays };
73 } else {
74 $self->{'sig_depends'}[0] = { 'signal' => $signal,
75 'nbdays' => $nbdays };
76 }
77  
78 return;
79 }
80 sub add_prices_dependency {
81 my ($self, $nbdays) = @_;
82  
83 if (defined($self->{'price_depends'}))
84 {
85 $self->{'price_depends'} = ($nbdays > $self->{'price_depends'}) ?
86 $nbdays : $self->{'price_depends'};
87 } else {
88 $self->{'price_depends'} = $nbdays;
89 }
90
91 return;
92 }
93 sub add_arg_dependency {
94 my ($self, $argnum, $nbdays) = @_;
95 my $object = $self->{'args'}->get_arg_object($argnum);
96 ref($object) =~ /^Finance::GeniusTrader::Indicators/ and $self->add_indicator_dependency($object, $nbdays);
97 ref($object) =~ /^Finance::GeniusTrader::Signals/ and $self->add_signal_dependency($object, $nbdays);
98 return;
99 }
100 sub add_volatile_indicator_dependency {
101 my ($self, $indicator, $nbdays) = @_;
102 #ERR# ERROR "Bad dependency" if ( ref($indicator) =~ /Finance::GeniusTrader::Indicators/);
103
104 if ($self->{'volatile_indic_depends'})
105 {
106 push @{$self->{'volatile_indic_depends'}}, { 'indicator' => $indicator,
107 'nbdays' => $nbdays };
108 } else {
109 $self->{'volatile_indic_depends'}[0] = { 'indicator' => $indicator,
110 'nbdays' => $nbdays };
111 }
112  
113 return;
114 }
115 sub add_volatile_signal_dependency {
116 my ($self, $signal, $nbdays) = @_;
117 #ERR# ERROR "Bad dependency" if ( ref($signal) =~ /Finance::GeniusTrader::Signals/);
118  
119 if ($self->{'volatile_sig_depends'})
120 {
121 push @{$self->{'volatile_sig_depends'}}, { 'signal' => $signal,
122 'nbdays' => $nbdays };
123 } else {
124 $self->{'volatile_sig_depends'}[0] = { 'signal' => $signal,
125 'nbdays' => $nbdays };
126 }
127  
128 return;
129 }
130 sub add_volatile_prices_dependency {
131 my ($self, $nbdays) = @_;
132  
133 if (defined($self->{'volatile_price_depends'}))
134 {
135 $self->{'volatile_price_depends'} = ($nbdays > $self->{'volatile_price_depends'}) ?
136 $nbdays : $self->{'volatile_price_depends'};
137 } else {
138 $self->{'volatile_price_depends'} = $nbdays;
139 }
140
141 return;
142 }
143 sub add_volatile_arg_dependency {
144 my ($self, $argnum, $nbdays) = @_;
145 my $object = $self->{'args'}->get_arg_object($argnum);
146 ref($object) =~ /^Finance::GeniusTrader::Indicators/ and $self->add_volatile_indicator_dependency($object, $nbdays);
147 ref($object) =~ /^Finance::GeniusTrader::Signals/ and $self->add_volatile_signal_dependency($object, $nbdays);
148 return;
149 }
150  
151 =item C<< $object->get_prices_dependency() >>
152  
153 =item C<< $object->get_signal_dependencies() >>
154  
155 =item C<< $object->get_indicator_dependencies() >>
156  
157 Return the dependency or list of dependencies.
158  
159 =cut
160 sub get_prices_dependency {
161 my ($self) = @_;
162 my $max = 1;
163
164 if ($self->{'price_depends'})
165 {
166 $max = $self->{'price_depends'};
167 }
168 if ($self->{'volatile_price_depends'})
169 {
170 my $v = $self->{'volatile_price_depends'};
171 $max = ($v > $max) ? $v : $max;
172 }
173 return $max;
174 }
175 sub get_indicator_dependencies {
176 my ($self) = @_;
177 my @dep = ();
178 push @dep, @{$self->{'indic_depends'}} if (defined($self->{'indic_depends'}));
179 push @dep, @{$self->{'volatile_indic_depends'}} if (defined($self->{'volatile_indic_depends'}));
180 return @dep;
181 }
182 sub get_signal_dependencies {
183 my ($self) = @_;
184 my @dep = ();
185 push @dep, @{$self->{'sig_depends'}} if (defined($self->{'sig_depends'}));
186 push @dep, @{$self->{'volatile_sig_depends'}} if (defined($self->{'volatile_sig_depends'}));
187 return @dep;
188 }
189
190 =item C<< $object->remove_volatile_dependencies() >>
191  
192 Removes all volatile dependencies.
193  
194 =cut
195 sub remove_volatile_dependencies {
196 my ($self) = @_;
197 $self->{'volatile_indic_depends'} = [];
198 $self->{'volatile_sig_depends'} = [];
199 $self->{'volatile_prices_depends'} = 0;
200 return;
201 }
202  
203 =item C<< $object->days_required >>
204  
205 Returns the number of days required so that the object can produce a result.
206  
207 =cut
208 sub days_required {
209 my ($self) = @_;
210 my $max = $self->get_prices_dependency;
211
212 foreach ($self->get_indicator_dependencies())
213 {
214 my $tmp = $_->{'indicator'}->days_required + $_->{'nbdays'} - 1;
215 $max = ($tmp > $max) ? $tmp : $max;
216 }
217 foreach ($self->get_signal_dependencies())
218 {
219 my $tmp = $_->{'signal'}->days_required + $_->{'nbdays'} - 1;
220 $max = ($tmp > $max) ? $tmp : $max;
221 }
222  
223 return $max;
224 }
225  
226 =item C<< ($first, $last) = $object->update_interval($calc, $first, $last) >>
227  
228 Check the limits of the interval. Return new limits. The interval
229 is contained in the first interval but all days will produce a result.
230 The new interval may be equal to the given interval.
231  
232 =cut
233 sub update_interval {
234 my ($self, $calc, $first, $last) = @_;
235  
236 if ($first + 1 < $self->days_required)
237 {
238 $first = $self->days_required - 1;
239 }
240 if ($last + 1 > $calc->prices->count)
241 {
242 $last = $calc->prices->count - 1;
243 }
244  
245 return ($first, $last);
246 }
247  
248 =item C<< $object->check_dependencies($calc, $i) >>
249  
250 =item C<< $object->check_dependencies_interval($calc, $first, $last) >>
251  
252 Check that there is enough data available. If there isn't return false.
253 Otherwise make sure the required data are computed and return true.
254  
255 =cut
256 sub check_dependencies {
257 my ($self, $calc, $i) = @_;
258  
259 if ($i + 1 < $self->days_required)
260 {
261 return 0;
262 }
263 if (! $self->dependencies_are_available($calc, $i))
264 {
265 $self->compute_dependencies($calc, $i);
266 } else {
267 return 1;
268 }
269 return $self->dependencies_are_available($calc, $i);
270 }
271 sub check_dependencies_interval {
272 my ($self, $calc, $first, $last) = @_;
273
274 if ($first + 1 < $self->days_required)
275 {
276 return 0;
277 }
278 if (! $self->dependencies_are_available_interval($calc, $first, $last))
279 {
280 $self->compute_dependencies_interval($calc, $first, $last);
281 } else {
282 return 1;
283 }
284 return $self->dependencies_are_available_interval($calc, $first, $last);
285 }
286  
287 =item C<< $object->dependencies_are_available($calc, $i) >>
288  
289 =item C<< $object->dependencies_are_available_interval($calc, $first, $last) >>
290  
291 Check if all dependencies have been computed.
292  
293 =cut
294 # XXX: it would be besser if we could check for the availability
295 # of the interval instead of checking each day
296 #
297 # The function does assume that if the first value provided by
298 # the indicator is available, then all others are available
299 sub dependencies_are_available {
300 my ($self, $calc, $i) = @_;
301  
302 # Availibility of indicators
303 foreach ($self->get_indicator_dependencies())
304 {
305 my $name = $_->{'indicator'}->get_name;
306 for (my $n = $i - $_->{'nbdays'} + 1; $n <= $i; $n++)
307 {
308 if (! $calc->indicators->is_available($name, $n))
309 {
310 return 0;
311 }
312 }
313 }
314  
315 # Availability of signals
316 foreach ($self->get_signal_dependencies())
317 {
318 my $name = $_->{'signal'}->get_name;
319 for (my $n = $i - $_->{'nbdays'} + 1; $n <= $i; $n++)
320 {
321 if (! $calc->signals->is_available($name, $n))
322 {
323 return 0;
324 }
325 }
326 }
327
328 return 1;
329 }
330 sub dependencies_are_available_interval {
331 my ($self, $calc, $first, $last) = @_;
332
333 # Availibility of indicators
334 foreach ($self->get_indicator_dependencies())
335 {
336 my $name = $_->{'indicator'}->get_name;
337 for (my $n = $first - $_->{'nbdays'} + 1; $n <= $last; $n++)
338 {
339 if (! $calc->indicators->is_available($name, $n))
340 {
341 return 0;
342 }
343 }
344 }
345  
346 # Availability of signals
347 foreach ($self->get_signal_dependencies())
348 {
349 my $name = $_->{'signal'}->get_name;
350 for (my $n = $first - $_->{'nbdays'} + 1; $n <= $last; $n++)
351 {
352 if (! $calc->signals->is_available($name, $n))
353 {
354 return 0;
355 }
356 }
357 }
358
359 return 1;
360 }
361  
362 =item C<< $object->compute_dependencies($calc, $i) >>
363  
364 =item C<< $object->compute_dependencies_interval($calc, $first, $last) >>
365  
366 Calculate all dependent indicators and detect all dependent signals.
367  
368 =cut
369 sub compute_dependencies {
370 my ($self, $calc, $i) = @_;
371  
372 # Compute indicators
373 foreach ($self->get_indicator_dependencies())
374 {
375 #DEB# DEBUG "Compute dependencies for $self : $_->{'indicator'}\n";
376 if ($_->{'nbdays'} > 1)
377 {
378 $_->{'indicator'}->calculate_interval($calc,
379 $i - $_->{'nbdays'} + 1, $i);
380 } else {
381 $_->{'indicator'}->calculate($calc, $i);
382 }
383 }
384
385 # Compute signals
386 foreach ($self->get_signal_dependencies())
387 {
388 #DEB# DEBUG "Compute dependencies for $self : $_->{'signal'}\n";
389 if ($_->{'nbdays'} > 1)
390 {
391 $_->{'signal'}->detect_interval($calc,
392 $i - $_->{'nbdays'} + 1, $i);
393 } else {
394 $_->{'signal'}->detect($calc, $i);
395 }
396 }
397  
398 return;
399 }
400 sub compute_dependencies_interval {
401 my ($self, $calc, $first, $last) = @_;
402
403 # Compute indicators
404 foreach ($self->get_indicator_dependencies())
405 {
406 #DEB# DEBUG "Compute dependencies for $self : $_->{'indicator'}\n";
407 $_->{'indicator'}->calculate_interval($calc,
408 $first - $_->{'nbdays'} + 1, $last);
409 }
410
411 # Compute signals
412 foreach ($self->get_signal_dependencies())
413 {
414 #DEB# DEBUG "Compute dependencies for $self : $_->{'signal'}\n";
415 $_->{'signal'}->detect_interval($calc,
416 $first - $_->{'nbdays'} + 1, $last);
417 }
418  
419 return;
420 }
421  
422 =pod
423  
424 =back
425  
426 =cut
427 1;