GeniusTrader/Indicators/AROON.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 92 CC 5 SCAR 26.1
CC
5
Cov
0%
calculate
30.0
initialize
2.0
BEGIN
2.0
CRAP 13.7
1 package Finance::GeniusTrader::Indicators::AROON;
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::Generic::MinInPeriod;
14 use Finance::GeniusTrader::Indicators::Generic::MaxInPeriod;
15 use Finance::GeniusTrader::Prices;
16  
17 @ISA = qw(Finance::GeniusTrader::Indicators);
18 @NAMES = ("AroonUp[#*]","AroonDown[#*]","AroonOsc[#*]");
19 @DEFAULT_ARGS = (25, "{I:Prices HIGH}", "{I:Prices LOW}");
20  
21 =head2 Finance::GeniusTrader::Indicators::AROON
22  
23 =head2 Overview
24  
25 Developed by Tushar Chande in 1995, the Aroon is an indicator system that can be used to determine whether a stock is trending or not and how strong the trend is. "Aroon" means "Dawn's Early Light" in Sanskrit and Chande choose that name for this indicator since it is designed to reveal the beginning of a new trend.
26  
27 The Aroon indicator consists of two lines, Aroon(up) and Aroon(down). The Aroon Oscillator is a single line that is defined as the difference between Aroon(up) and Aroon(down). All three take a single parameter which is the number of time periods to use in the calculation. Since Aroon(up) and Aroon(down) both oscillate between 0 and +100, the Aroon Oscillator ranges from -100 to +100 with zero serving as the crossover line.
28  
29 =head2 Calculation
30  
31 Aroon(up) for a given time period is calculated by determining how much time (on a percentage basis) elapsed between the start of the time period and the point at which the highest closing price during that time period occurred. When the stock is setting new highs for the time period, Aroon(up) will be 100. If the stock has moved lower every day during the time period, Aroon(up) will be zero. Aroon(down) is calculated in just the opposite manner, looking for new lows instead of new highs.
32  
33 =head2 Examples
34  
35 Finance::GeniusTrader::Indicators::AROON->new()
36 Finance::GeniusTrader::Indicators::AROON->new([20])
37  
38 =head2 Validation
39  
40 This indicators is validated by the values from comdirect.de.
41 The stock used was the DAX (data from yahoo) at the 04.06.2003:
42  
43 AroonUp[25] [2003-06-04] = 100.0000 (comdirect: 100.0)
44 AroonDown[25] [2003-06-04] = 76.0000 (comdirect: 76.0)
45 AroonOsc[25] [2003-06-04] = 24.0000 (comdirect: 24.0)
46  
47 =head2 Links
48  
49 http://stockcharts.com/education/resources/glossary/aroon.html
50 http://www.paritech.com/education/technical/indicators/trend/aroon.asp
51 http://www.geocities.com/WallStreet/Floor/1035/aroon.htm
52  
53 =cut
54  
55 sub initialize {
56 my $self = shift;
57  
58 my $mstr = "{I:Generic:Eval " .$self->{'args'}->get_arg_names(1) . " + 1}";
59 $self->{'min'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ $mstr,
60 $self->{'args'}->get_arg_names(3) ] );
61 $self->{'max'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ $mstr,
62 $self->{'args'}->get_arg_names(2) ] );
63  
64 $self->add_indicator_dependency($self->{'min'}, 1);
65 $self->add_indicator_dependency($self->{'max'}, 1);
66 }
67  
68 =head2 Finance::GeniusTrader::Indicators::AROON::calculate($calc, $day)
69  
70 =cut
71 sub calculate {
72 my ($self, $calc, $i) = @_;
73 my $indic = $calc->indicators;
74 my $prices = $calc->prices;
75 my $period = $self->{'args'}->get_arg_values($calc, $i, 1);
76 my $aroon_up_name = $self->get_name(0);
77 my $aroon_down_name = $self->get_name(1);
78 my $aroon_osc_name = $self->get_name(2);
79 my $min = $self->{'min'};
80 my $max = $self->{'max'};
81 my $last_period_high = 0;
82 my $last_period_low = 0;
83  
84 return if ($indic->is_available($aroon_up_name, $i) &&
85 $indic->is_available($aroon_down_name, $i) &&
86 $indic->is_available($aroon_osc_name, $i));
87  
88 $self->remove_volatile_dependencies();
89 $self->add_volatile_arg_dependency(2,$period+1);
90 $self->add_volatile_arg_dependency(3,$period+1);
91 return if (! $self->check_dependencies($calc, $i));
92
93 # Get Min and Max
94 my $min_value = $indic->get($min->get_name, $i);
95 my $max_value = $indic->get($max->get_name, $i);
96
97 for (my $n = $i - $period; $n <= $i; $n++) {
98  
99 # Last period high
100 if ($self->{'args'}->get_arg_values($calc, $n, 2) eq $max_value) {
101 $last_period_high = $n;
102 }
103 # Last period low
104 if ($self->{'args'}->get_arg_values($calc, $n, 3) eq $min_value) {
105 $last_period_low = $n;
106 }
107 }
108  
109 my $aroon_up_value = (($period - ($i - $last_period_high)) / $period * 100);
110 my $aroon_down_value = (($period - ($i - $last_period_low)) / $period * 100);
111 my $aroon_osc_value = $aroon_up_value - $aroon_down_value;
112  
113 # Return the results
114 $indic->set($aroon_up_name, $i, $aroon_up_value);
115 $indic->set($aroon_down_name, $i, $aroon_down_value);
116 $indic->set($aroon_osc_name, $i, $aroon_osc_value);
117 }
118  
119 1;