GeniusTrader/Indicators/BPCorrelation.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 101 CC 9 SCAR 31.6
CC
9
Cov
0%
initialize
30.0
calculate
30.0
BEGIN
2.0
CRAP 23.5
1 package Finance::GeniusTrader::Indicators::BPCorrelation;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber, Oliver Bossert
4 # standards upgrade Copyright 2005 Thomas Weigert
5 # This file is distributed under the terms of the General Public License
6 # version 2 or (at your option) any later version.
7  
8 # includes ras hack to protect against division by zero
9 # detection of missing or invalid arguments
10 # $Id$
11  
12 # Standards-Version: 1.0
13  
14 use strict;
15 use vars qw(@ISA @NAMES);
16  
17 use Finance::GeniusTrader::Indicators;
18  
19 @ISA = qw(Finance::GeniusTrader::Indicators);
20 @NAMES = ("BPCorrelation[#1,#2,#3]");
21  
22 =head2 Finance::GeniusTrader::Indicators::BPCorrelation (Bravais-Pearson Correlation Coefficient)
23  
24 This function will calculate the Bravais-Pearson Correlation Coefficient.
25 Correlation analysis measures the relationship between two items and shows
26 if changes in one item will result in changes in the other item.
27  
28 this indicator requires three arguments, and provides no default values
29 for any of them.
30  
31 the first argument is the number of intervals in the period,
32 it can be a constant or a data series. the period is used as
33 the number of data values used in each computation
34  
35 the second and third arguments must be functions (e.g. data series
36 or data objects?)
37  
38 the indicator will validate that the arguments are provided and are
39 of the correct type.
40  
41 =head2 examples (display_indicator)
42
43 % display_indicator.pl I:BPCorrelation 13000 \
44 '20 {I:Prices OPEN} {I:Prices CLOSE}'
45  
46 % display_indicator.pl I:BPCorrelation 13000 \
47 '14 {I:G:Cum 1} {I:Prices CLOSE}'
48
49  
50 =cut
51 sub initialize {
52 my ($self) = @_;
53  
54 if ($self->{'args'}->is_constant(1)) {
55 $self->add_prices_dependency($self->{'args'}->get_arg_constant(1));
56 } else {
57 $self->add_prices_dependency(20);
58 }
59  
60 # indicator requires 3 arguments
61 my $err = 0;
62 ( my $msg_n = $NAMES[0] ) =~ s/^(.*)\[.*/$1/;
63 my $msg = "$msg_n: argument error\n";
64 if ( ! defined($self->{'args'}[1]) ) {
65 $msg .= join "", "\targ #1 must not be null\n";
66 ++$err;
67 }
68 # indicator requires functions for 2nd and 3rd arguments
69 # skip self and first
70 for ( my $i = 2; $i <= 3; ++$i ) {
71 if ( ! defined($self->{'args'}[$i])
72 || $self->{'args'}->is_constant($i)) {
73 my $msg_txt = "must be a function (series)\n";
74 $msg .= join "", "\targ #$i \"$self->{'args'}[$i]\" $msg_txt";
75 ++$err;
76 }
77 }
78 die "$msg" if ( $err );
79 }
80  
81 =head2 Finance::GeniusTrader::Indicators::Correlation::calculate($calc, $day)
82  
83 =cut
84 sub calculate {
85 my ($self, $calc, $i) = @_;
86 my $name = $self->get_name;
87 my $period = $self->{'args'}->get_arg_constant(1);
88 my $average_x = 0;
89 my $average_y = 0;
90 my $sum_y = 0;
91 my $sum_x = 0;
92 my $sum_xy = 0;
93
94 return if ($calc->indicators->is_available($name, $i));
95  
96 $self->remove_volatile_dependencies();
97 $self->add_volatile_arg_dependency(2, $period);
98 $self->add_volatile_arg_dependency(3, $period);
99  
100 return if (! $self->check_dependencies($calc, $i));
101 return if (defined($period) && ($i + 1 < $period));
102  
103 if (!defined $period) {
104 $period = $i;
105 }
106
107 for(my $n = $i - $period + 1; $n <= $i; $n++) {
108  
109 $average_x += $self->{'args'}->get_arg_values($calc, $n, 2);
110 $average_y += $self->{'args'}->get_arg_values($calc, $n, 3);
111 }
112
113 $average_x /= $period;
114 $average_y /= $period;
115
116 for(my $n = $i - $period + 1; $n <= $i; $n++) {
117 $sum_x += ($self->{'args'}->get_arg_values($calc, $n, 2) - $average_x) ** 2;
118 $sum_y += ($self->{'args'}->get_arg_values($calc, $n, 3) - $average_y) ** 2;
119 $sum_xy += ($self->{'args'}->get_arg_values($calc, $n, 2) - $average_x)
120 * ($self->{'args'}->get_arg_values($calc, $n, 3) - $average_y);
121 }
122  
123 # Calculate the Bravais-Pearson Correlation Coefficient
124 # protect against division by zero
125 my $den = ($sum_x * $sum_y) ** 0.5 || 0.0000001;
126 my $correlation = $sum_xy / $den;
127
128 # Return the result
129 $calc->indicators->set($name, $i, $correlation);
130 }
131  
132 1;