GeniusTrader/Indicators/Generic/Eval.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 71 CC 7 SCAR 29.8
CC
7
Cov
0%
calculate
42.0
initialize
6.0
BEGIN
2.0
CRAP 19.7
1 package Finance::GeniusTrader::Indicators::Generic::Eval;
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(@ISA @NAMES);
11  
12 use Finance::GeniusTrader::Indicators;
13 use Finance::GeniusTrader::Prices;
14 use Finance::GeniusTrader::Tools qw(:math);
15 use Math::Trig;
16  
17 @ISA = qw(Finance::GeniusTrader::Indicators);
18 @NAMES = ("Eval[#*]");
19  
20 =head1 NAME
21  
22 Finance::GeniusTrader::Indicators::Generic::Eval - Evaluate the given expression
23  
24 =head1 DESCRIPTION
25  
26 This indicator evaluates the expression given via its argument.
27 Any indicator is replaced by its current value.
28  
29 Example of accepted argument list :
30  
31 =over
32  
33 =item int({I:RSI})
34  
35 =item 1+1
36  
37 =item {I:Generic:SignalLength {Signals:Prices:Advance 5}}+1
38  
39 =item 100 - {I:RSI 10} * 2
40  
41 =back
42  
43 The argument list is treated via perl's eval function so any standard
44 perl code may be accepted ... but it's only meant for simple single
45 expression.
46  
47 =cut
48 sub initialize {
49 my ($self) = @_;
50  
51 my $nb = $self->{'args'}->get_nb_args();
52 for (my $n = 1; $n <= $nb; $n++) {
53 if (! $self->{'args'}->is_constant($n)) {
54 $self->add_arg_dependency($n, 1);
55 }
56 }
57  
58 }
59  
60 sub calculate {
61 my ($self, $calc, $i) = @_;
62 my $name = $self->get_name;
63
64 return if ($calc->indicators->is_available($name, $i));
65 return if (! $self->check_dependencies($calc, $i));
66  
67 my $nb = $self->{'args'}->get_nb_args();
68 my $expr = "";
69 for (my $n = 1; $n <= $nb; $n++) {
70 if ($self->{'args'}->is_constant($n)) {
71 $expr .= " " . $self->{'args'}->get_arg_constant($n);
72 } else {
73 my $val = $self->{'args'}->get_arg_values($calc, $i, $n);
74 return if (! defined($val));
75 $expr .= " $val";
76 }
77 }
78 my $res = undef;
79 eval "\$res = $expr";
80 if ($@) {
81 warn "$@ : $expr";
82 return;
83 }
84
85 $calc->indicators->set($name, $i, $res);
86 }