GeniusTrader/Indicators/ENV.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 51 CC 3 SCAR 17.5
CC
3
Cov
0%
calculate
12.0
initialize
2.0
BEGIN
2.0
CRAP 5.8
1 package Finance::GeniusTrader::Indicators::ENV;
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 @DEFAULT_ARGS);
11  
12 use Finance::GeniusTrader::Indicators;
13 use Finance::GeniusTrader::Indicators::EMA;
14  
15 @ISA = qw(Finance::GeniusTrader::Indicators);
16 @NAMES = ("ENVSup[#*]","ENVInf[#*]");
17 @DEFAULT_ARGS = (25,6,"{I:Prices CLOSE}");
18  
19 =pod
20  
21 =head2 Finance::GeniusTrader::Indicators::ENV
22  
23 An envelope is composed of two moving averages. One moving average is shifted upward and the second moving average is shifted downward.
24 Envelopes define the upper and the lower boundaries of a security's normal trading range.
25  
26 The standard envelope (ENV 25-6) can be called like that : Finance::GeniusTrader::Indicators::ENV->new()
27  
28 If you need a non standard ENV :
29 Finance::GeniusTrader::Indicators::ENV->new([21, 5])
30  
31 =cut
32  
33 sub initialize {
34 my $self = shift;
35  
36 $self->{'ema'} = Finance::GeniusTrader::Indicators::EMA->new([ $self->{'args'}->get_arg_names(1),
37 $self->{'args'}->get_arg_names(3) ]);
38  
39 $self->add_indicator_dependency($self->{'ema'}, 1);
40 }
41  
42 =head2 Finance::GeniusTrader::Indicators::ENV::calculate($calc, $day)
43  
44 =cut
45 sub calculate {
46 my ($self, $calc, $i) = @_;
47 my $indic = $calc->indicators;
48 my $period = $self->{'args'}[0];
49 my $shifted_percentage = $self->{'args'}->get_arg_values($calc, $i, 2);
50 my $envsup_name = $self->get_name(0);
51 my $envinf_name = $self->get_name(1);
52 my $ema = $self->{'ema'};
53  
54 return if ($indic->is_available($envsup_name, $i) &&
55 $indic->is_available($envinf_name, $i));
56 return if (! $self->check_dependencies($calc, $i));
57  
58 # Get the EMA value
59 my $ema_value = $indic->get($ema->get_name, $i);
60
61 # Envelope Band Sup is equal to the value of the exponential moving average + fixed percentage deviation
62 my $envsup_value = $ema_value * (1 + $shifted_percentage / 100);
63
64 # Envelope Band Inf is equal to the value of the exponential moving average - fixed percentage deviation
65 my $envinf_value = $ema_value * (1 - $shifted_percentage / 100);
66
67 # Return the results
68 $indic->set($envsup_name, $i, $envsup_value);
69 $indic->set($envinf_name, $i, $envinf_value);
70 }
71  
72 1;