GeniusTrader/Indicators/MASS.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 81 CC 6 SCAR 22.9
CC
6
Cov
0%
new
20.0
calculate
12.0
initialize
2.0
CRAP 9.8
1 package Finance::GeniusTrader::Indicators::MASS;
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 use strict;
8 use vars qw(@ISA @NAMES);
9  
10 use Finance::GeniusTrader::Indicators;
11 use Finance::GeniusTrader::Indicators::EMA;
12 use Finance::GeniusTrader::Prices;
13  
14 @ISA = qw(Finance::GeniusTrader::Indicators);
15 @NAMES = ("MASS[#1,#2]");
16  
17 =head1 Finance::GeniusTrader::Indicators::MASS
18  
19 =head2 Overview
20  
21 The Mass Index was designed to identify trend reversals by measuring the narrowing and widening of the range between the high and low prices. As this range widens, the Mass Index increases; as the range narrows the Mass Index decreases.
22  
23 The Mass Index was developed by Donald Dorsey.
24  
25 =head2 Calculation
26  
27 Mass Index = A-day sum of the ratio between the B-day EMA of (High - Low) and the B-day EMA of the B-day EMA of (High - Low)
28  
29 =head2 Parameters
30  
31 The standard Mass Index is calculated with : A = 25 and B = 9
32  
33 =head2 Examples
34  
35 Finance::GeniusTrader::Indicators::MASS->new()
36 Finance::GeniusTrader::Indicators::MASS->new([30, 14])
37  
38 =head2 Links
39  
40 http://www.equis.com/free/taaz/massindex.html
41 http://www.charthelp.com/reports/c21.htm
42  
43 =cut
44 sub new {
45 my $type = shift;
46 my $class = ref($type) || $type;
47 my ($args) = @_;
48 my $self = { 'args' => defined($args) ? $args : [25, 9] };
49  
50 $args->[0] = 25 if (! defined($args->[0]));
51 $args->[1] = 9 if (! defined($args->[1]));
52
53 return manage_object(\@NAMES, $self, $class, $self->{'args'}, "");
54 }
55  
56 sub initialize {
57 my $self = shift;
58
59 # Initialize the fast moving average
60 $self->{'fast_moving_average'} = Finance::GeniusTrader::Indicators::EMA->new(
61 [ $self->{'args'}[1] ],
62 "HIGH - LOW",
63 sub {
64 $_[0]->prices->at($_[1])->[$HIGH] -
65 $_[0]->prices->at($_[1])->[$LOW]
66 });
67  
68 # Initialize the slow moving average
69 $self->{'slow_moving_average'} = Finance::GeniusTrader::Indicators::EMA->new(
70 [ $self->{'args'}[1] ],
71 "Slow Moving Average",
72 sub {
73 $_[0]->indicators->get($self->{'fast_moving_average'}->get_name, $_[1])
74 });
75  
76 $self->add_indicator_dependency($self->{'fast_moving_average'},
77 $self->{'args'}[0] + $self->{'args'}[1]);
78 $self->add_indicator_dependency($self->{'slow_moving_average'},
79 $self->{'args'}[0]);
80 }
81  
82 =head2 Finance::GeniusTrader::Indicators::MASS::calculate($calc, $day)
83  
84 =cut
85 sub calculate {
86 my ($self, $calc, $i) = @_;
87 my $indic = $calc->indicators;
88 my $fast_moving_average_name = $self->{'fast_moving_average'}->get_name;
89 my $slow_moving_average_name = $self->{'slow_moving_average'}->get_name;
90 my $mass_index_name = $self->get_name(0);
91 my $sum_period = $self->{'args'}[0];
92 my $ema_period = $self->{'args'}[1];
93 my $mass_index_value = 0;
94
95 return if ($indic->is_available($mass_index_name, $i));
96 return if (! $self->check_dependencies($calc, $i));
97  
98 for (my $n = 0; $n < $sum_period; $n++) {
99  
100 # Get fast and slow moving average values
101 my $fast_moving_average_value = $indic->get($fast_moving_average_name, $i - $n);
102 my $slow_moving_average_value = $indic->get($slow_moving_average_name, $i - $n);
103
104 # Calculate the Mass Index (= sum of ($fast_moving_average / $slow_moving_average))
105 $mass_index_value += ($fast_moving_average_value / $slow_moving_average_value);
106  
107 }
108  
109 # Return the results
110 $indic->set($mass_index_name, $i, $mass_index_value);
111 }
112  
113 1;