GeniusTrader/Indicators/CCI.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 66 CC 3 SCAR 16.7
CC
3
Cov
0%
calculate
12.0
initialize
2.0
BEGIN
2.0
CRAP 5.3
1 package Finance::GeniusTrader::Indicators::CCI;
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::SMA;
14 use Finance::GeniusTrader::Indicators::TP;
15  
16 @ISA = qw(Finance::GeniusTrader::Indicators);
17 @NAMES = ("CCI[#*]");
18 @DEFAULT_ARGS = (20, "{I:Prices HIGH}", "{I:Prices LOW}", "{I:Prices CLOSE}");
19  
20 =head1 Finance::GeniusTrader::Indicators::CCI
21  
22 =head2 Overview
23  
24 Developed by Donald Lambert, the Commodity Channel Index (CCI) was designed to identify cyclical turns in commodities. The assumption behind the indicator is that commodities (or stocks or bonds) move in cycles, with highs and lows coming at periodic intervals.
25  
26 =head2 Calculation
27  
28 There are 4 steps involved in the calculation of the CCI :
29 1. Calculate the last period's Typical Price (TP) = (H+L+C)/3 where H = high, L = low, and C = close.
30 2. Calculate the 20-period Simple Moving Average of the Typical Price (SMATP).
31 3. Calculate the Mean Deviation. First, calculate the absolute value of the difference between the last period's SMATP and the typical price for each of the past 20 periods. Add all of these absolute values together and divide by 20 to find the Mean Deviation.
32 4. The final step is to apply the Typical Price (TP), the Simple Moving Average of the Typical Price (SMATP), the Mean Deviation and a Constant (.015) to the following formula :
33  
34 CCI = ( (Typical Price - Simple Moving Average of the Typical Price) / (0.015 * Mean Deviation))
35  
36 =head2 Parameters
37  
38 Lambert recommended using 1/3 of a complete cycle (low to low or high to high) as a time frame for the CCI. Note that the determination of the cycle's length is independent of the CCI. If the cycle runs 60 days (a low about every 60 days), then a 20-day CCI would be recommended.
39  
40 =head2 Example
41  
42 Finance::GeniusTrader::Indicators::CCI->new()
43 Finance::GeniusTrader::Indicators::CCI->new([25])
44  
45 =head2 Note
46  
47 Traders and investors use the CCI to help identify price reversals, price extremes and trend strength. As with most indicators, the CCI should be used in conjunction with other aspects of technical analysis. CCI fits into the momentum category of oscillators.
48  
49 =head2 Validation
50  
51 This Indicator was validated by the data available from comdirect.de:
52 The DAX at 04.06.2003 (data from yahoo.com) had a CCI of 158.71
53 This is consistent with this indicator: 158.7057
54  
55 =head2 Links
56  
57 http://www.equis.com/free/taaz/cci.html
58 http://www.stockcharts.com/education/What/IndicatorAnalysis/indic_CCI.html
59 http://www.finance-net.com/apprendre/techniques/cci.phtml
60  
61 =cut
62  
63 sub initialize {
64 my $self = shift;
65  
66 my $tp = "{I:TP " . $self->{'args'}->get_arg_names(2) . " " .
67 $self->{'args'}->get_arg_names(3) . " " .
68 $self->{'args'}->get_arg_names(4) . "}";
69 $self->{'tp'} = Finance::GeniusTrader::Indicators::TP->new( [ $self->{'args'}->get_arg_names(2),
70 $self->{'args'}->get_arg_names(3),
71 $self->{'args'}->get_arg_names(4)
72 ] );
73 $self->{'sma'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names(1), $tp ]);
74 }
75  
76 =head2 Finance::GeniusTrader::Indicators::CCI::calculate($calc, $day)
77  
78 =cut
79 sub calculate {
80 my ($self, $calc, $i) = @_;
81 my $indic = $calc->indicators;
82 my $period = $self->{'args'}->get_arg_values($calc, $i, 1);
83 my $tp = $self->{'tp'};
84 my $sma = $self->{'sma'};
85 my $tp_name = $tp->get_name;
86 my $sma_name = $sma->get_name;
87 my $cci_name = $self->get_name(0);
88 my $tp_value = 0;
89 my $sum_of_diff = 0;
90  
91 $self->remove_volatile_dependencies();
92 $self->add_volatile_indicator_dependency( $self->{'sma'}, $period );
93 $self->add_volatile_indicator_dependency( $self->{'tp'}, $period );
94 $self->add_volatile_prices_dependency($period * 2);
95  
96 return if ($indic->is_available($cci_name, $i));
97 return if (! $self->check_dependencies($calc, $i));
98
99 # Get the Simple Moving Average of the Typical Price
100 my $smatp = $indic->get($sma_name, $i);
101  
102 for (my $m = $i - $period + 1; $m <= $i; $m++) {
103 # Get the Typical Price
104 $tp_value = $indic->get($tp_name, $m);
105 $smatp = $indic->get($sma_name, $m);
106
107 # Calculate the difference between the Typical Price and today's Simple Moving Average of the Typical Price
108 $sum_of_diff += abs($tp_value - $smatp);
109 }
110  
111 # Calculate the Mean Deviation
112 my $mean_deviation = $sum_of_diff / ($period);
113  
114 # Calculate the CCI
115 my $cci = ($tp_value - $smatp) / (0.015 * ($mean_deviation));
116  
117 # Return the result
118 $indic->set($cci_name, $i, $cci);
119  
120 }
121  
122 1;