GeniusTrader/Indicators/Generic/SignalLength.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 64 CC 6 SCAR 31.7
CC
6
Cov
0%
calculate
42.0
initialize
2.0
BEGIN
2.0
CRAP 23.8
1 package Finance::GeniusTrader::Indicators::Generic::SignalLength;
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  
15 @ISA = qw(Finance::GeniusTrader::Indicators);
16 @NAMES = ("SignalLength[#1]");
17  
18 =head1 NAME
19  
20 Finance::GeniusTrader::Indicators::Generic::SignalLength - Length of any signal
21  
22 =head1 DESCRIPTION
23  
24 This indicator returns the number of consecutive periods where the signal
25 in parameter has been detected.
26  
27 =cut
28 sub initialize {
29 my ($self) = @_;
30  
31 }
32  
33 sub calculate {
34 my ($self, $calc, $i) = @_;
35 my $name = $self->get_name;
36 my $res = 0;
37
38 return if ($calc->indicators->is_available($name, $i));
39 my $sig_value = $self->{'args'}->get_arg_values($calc, $i, 1);
40 return if (! defined $sig_value);
41  
42 if ($sig_value) {
43 my $sum_yesterday = undef;
44 if ($i >= 1 and $calc->indicators->is_available($name, $i - 1)) {
45 $sum_yesterday = $calc->indicators->get($name, $i - 1);
46 $res = $sum_yesterday + 1;
47 } else {
48 my $n = $i - 1;
49 $res = 1;
50 while ($n >= 0) {
51 my $value = $self->{'args'}->get_arg_values($calc, $n, 1);
52 if (defined($value) && $value) {
53 $res++;
54 $n--;
55 } else {
56 last;
57 }
58 }
59 }
60 } else {
61 $res = 0;
62 }
63
64 $calc->indicators->set($name, $i, $res);
65 }