GeniusTrader/Indicators/REMA.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 85 CC 9 SCAR 40.7
CC
9
Cov
0%
calculate
90.0
initialize
2.0
BEGIN
2.0
CRAP 58.6
1 package Finance::GeniusTrader::Indicators::REMA;
2  
3 # Copyright 2003 Oliver Bossert
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::SMA;
14  
15 @ISA = qw(Finance::GeniusTrader::Indicators);
16 @NAMES = ("REMA[#*]");
17 @DEFAULT_ARGS = (20, 0.5, "{I:Prices CLOSE}");
18  
19 =head1 NAME
20  
21 Finance::GeniusTrader::Indicators::REMA - Regularized Exponential Moving Average
22  
23 =head1 DESCRIPTION
24  
25 Regularized EMA This modification of the classical EMA is described in
26 Stock&Commodities (July 2003). It is an adaption that includes the
27 momentum / second derivation of the value into the MA.
28  
29 It should be used for the calculation of the MACD. The classical EMA
30 is calculated as:
31  
32 F(n+1)=F(n)+A*[G(n+1)-F(n)] --- A(alpha): A=2/(Period+1)
33  
34 The REMA is calculated as followed:
35  
36 F(n+1)={F(n)*(1+2*L)+A*[G(n+1)-F(n)]-L*[F(n-1)]}/(1+L)
37  
38 with L(Lambda) as Regularization Factor.
39 Lambda should be > 0.5
40  
41  
42 =head2 Parameters
43  
44 =over
45  
46 =item Period (default 20)
47  
48 The first argument is the period used to calculed the average.
49  
50 =item Lambda (default 0.5)
51  
52 See above
53  
54 =item Other data input
55  
56 The second argument is optional. It can be used to specify an other
57 stream of input data for the average instead of the close prices.
58 This is usually an indicator (detailed via {I:MyIndic <param>}).
59  
60 =back
61  
62 =cut
63  
64 sub initialize {
65 my ($self) = @_;
66 $self->{'sma'} = Finance::GeniusTrader::Indicators::SMA->new([$self->{'args'}->get_arg_names(1), $self->{'args'}->get_arg_names(3) ]);
67 }
68  
69 sub calculate {
70 my ($self, $calc, $i) = @_;
71 my $nb = $self->{'args'}->get_arg_values($calc, $i, 1);
72 my $lambda = $self->{'args'}->get_arg_values($calc, $i, 2);
73 my $name = $self->get_name;
74
75 return if (! defined($nb));
76
77 $self->remove_volatile_dependencies();
78 $self->add_volatile_indicator_dependency($self->{'sma'}, $nb);
79 $self->add_volatile_arg_dependency(3, $nb);
80
81 return if ($calc->indicators->is_available($name, $i));
82 return if (! $self->check_dependencies($calc, $i));
83  
84 my $alpha = 2 / ($nb + 1);
85  
86 my $ema = $self->{'args'}->get_arg_values($calc, $i - $nb + 1, 3);
87 if ($calc->indicators->is_available($name, $i-$nb))
88 {
89 $ema = $calc->indicators->get($name, $i-$nb);
90 }
91 elsif ($i >= 4*$nb - 1)
92 {
93
94 my @ema = ();
95 for (my $bar = $i - 3*$nb; $bar <= $i-$nb; $bar++)
96 {
97 if (defined ($ema[$bar-$nb]))
98 {
99 $ema = $ema[$bar-$nb];
100 }
101 else
102 {
103 # This may look a bit dodgy but if the next line is omitted, SMA.pm uses calculate_interval
104 # which fails in this context. Actually, this might be a bug in SMA.pm.
105 $self->{'sma'}->calculate($calc, $bar - $nb);
106 $ema = $calc->indicators->get($self->{'sma'}->get_name(0), $bar-$nb);
107 }
108 for (my $n = $bar - $nb + 1; $n <= $bar; $n++)
109 {
110 $ema = ( $ema * (1-$alpha) ) + ( $alpha * $self->{'args'}->get_arg_values($calc, $n, 3) );
111 $ema = $ema + ( (1+2*$lambda) * $self->{'args'}->get_arg_values($calc, $n, 3) )
112 - ( $lambda * $self->{'args'}->get_arg_values($calc, $n-1, 3) );
113 $ema = $ema / (1+$lambda);
114  
115 }
116 $ema[$bar] = $ema;
117 }
118 }
119 elsif ($i >= 2*$nb - 1)
120 {
121
122 $self->{'sma'}->calculate($calc, $i - $nb);
123 $ema = $calc->indicators->get($self->{'sma'}->get_name(0), $i-$nb);
124 }
125
126 return if (! defined($ema));
127
128 for (my $n = $i - $nb + 1; $n <= $i; $n++)
129 {
130 $ema *= (1 - $alpha);
131 $ema += ($alpha * $self->{'args'}->get_arg_values($calc, $n, 3));
132 }
133
134 $calc->indicators->set($name, $i, $ema);
135 }
136  
137 1;