GeniusTrader/Signals/Generic/Repeated.pm untested

filter: Statementstmt 0.0% 0 / 29 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 56 CC 6 SCAR 28.2
CC
6
Cov
0%
detect
30.0
initialize
6.0
BEGIN
2.0
CRAP 16.8
1 package Finance::GeniusTrader::Signals::Generic::Repeated;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog
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::Signals;
13  
14 @ISA = qw(Finance::GeniusTrader::Signals);
15 @NAMES = ("Repeated[#*]");
16  
17 =head1 NAME
18  
19 Finance::GeniusTrader::Signals::Generic::Repeated - Detect repetition of a given signal
20  
21 =head2 DESCRIPTION
22  
23 This generic Signal will give a positive signal when the mentionned signal
24 has been positive for the last X days (where X is the second parameter of this
25 signal with a default value of 2).
26  
27 =head2 EXAMPLE
28  
29 You can check if the RSI has been above 80 for the last 3 days with this
30 signal:
31  
32 S:Generic:Repeated {S:Generic:Above {I:RSI} 80} 3
33  
34 =cut
35 sub initialize {
36 my ($self) = @_;
37 warn "Bad number of arguments given to S:Generic:Repeated !" if ($self->{'args'}->get_nb_args() < 1 || $self->{'args'}->get_nb_args() > 2);
38 }
39  
40 sub detect {
41 my ($self, $calc, $i) = @_;
42 my $signals = $calc->signals;
43
44 return if ($calc->signals->is_available($self->get_name, $i));
45  
46 my $defined = 0;
47 my $nb = ($self->{'args'}->get_nb_args() > 1) ? $self->{'args'}->get_arg_values($calc, $i, 2) : 2;
48 for (my $n = 0; $n < $nb; $n++)
49 {
50 my $value = $self->{'args'}->get_arg_values($calc, $i - $n, 1);
51 if (! (defined($value) && $value)) {
52 $signals->set($self->get_name, $i, 0);
53 return;
54 }
55 $defined = 1;
56 }
57 if ($defined) {
58 $signals->set($self->get_name, $i, 1);
59 }
60 }
61  
62 1;