GeniusTrader/Indicators/LinearRegression.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 95 CC 7 SCAR 32.0
CC
7
Cov
0%
calculate
42.0
initialize
6.0
BEGIN
2.0
CRAP 24.5
1 package Finance::GeniusTrader::Indicators::LinearRegression;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
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 # $Id$
9  
10 # Standards-Version: 1.0
11  
12 use strict;
13 use vars qw(@ISA @NAMES);
14  
15 use Finance::GeniusTrader::Indicators;
16  
17 @ISA = qw(Finance::GeniusTrader::Indicators);
18 @NAMES = ("LinearRegressionLine[#1,#2,#3]","LinearRegressionCoefficientA[#1,#2,#3]","LinearRegressionCoefficientB[#1,#2,#3]");
19  
20 =head1 Finance::GeniusTrader::Indicators::LinearRegression
21  
22 This function will calculate an L-Period linear regression line. Note that
23 the term "linear regression" is the same as a "least squares" or "best
24 fit" line.
25  
26 The linear regression value is "a * i + b". i is the day number. a and b
27 are also provided by the indicator if you want to calculate the value of
28 the linear regression for other days.
29  
30 =head2 Parameters
31  
32 Takes 2 or 3 parameters. The first is the period over which the regression
33 is calculated. The following parameters indicate the series that are being
34 compared. If there is only a second parameter, this parameter forms the
35 dependent variables, while the numerical sequence is the independent
36 parameter. If there are both a second and a third parameter, the former is
37 the independent and the latter the dependent parameter.
38
39 =cut
40  
41 sub initialize {
42 my ($self) = @_;
43  
44 # No default value for the first arg on purpose
45 # It means use all the values for the regression and not only the XX
46 # latest
47 if (defined $self->{'args'}[0]) {
48 $self->add_prices_dependency($self->{'args'}->get_arg_constant(1));
49 }
50 }
51  
52 sub calculate {
53 my ($self, $calc, $i) = @_;
54 my $linear_regression_line_name = $self->get_name(0);
55 my $linear_regression_coefficient_a_name = $self->get_name(1);
56 my $linear_regression_coefficient_b_name = $self->get_name(2);
57 my $period = $self->{'args'}->get_arg_values($calc, $i, 1);
58 my ($sum_x, $sum_y, $sum_xy) = 0;
59 my ($variance_x, $variance_y) = 0;
60
61 return if ($calc->indicators->is_available($linear_regression_line_name, $i) &&
62 $calc->indicators->is_available($linear_regression_coefficient_a_name, $i) &&
63 $calc->indicators->is_available($linear_regression_coefficient_b_name, $i));
64  
65 $self->remove_volatile_dependencies();
66 $self->add_volatile_arg_dependency(2, $period);
67 $self->add_volatile_arg_dependency(3, $period);
68  
69 return if (! $self->check_dependencies($calc, $i));
70  
71 # Initialize $period to $i if nothing was previously defined
72 if (!defined $period) {
73 $period = $i;
74 }
75
76 # Calculate the average of (x), (y) and (xy)
77 for(my $n = $i - $period + 1; $n <= $i; $n++) {
78  
79 my $x = $self->{'args'}->get_arg_values($calc, $n, 2);
80 my $y;
81 if ( defined( $self->{'args'}->get_arg_object(3) ) ) {
82 my $y = $self->{'args'}->get_arg_values($calc, $n, 3);
83 } else {
84 $y = $x;
85 $x = $n;
86 }
87  
88 $sum_x += $x;
89 $sum_y += $y;
90 $sum_xy += ($x * $y);
91 }
92
93 my $average_x = $sum_x / $period;
94 my $average_y = $sum_y / $period;
95 my $average_xy = $sum_xy / $period;
96  
97 # Calculate the variance of (x) and (y)
98 for(my $n = $i - $period + 1; $n <= $i; $n++) {
99  
100 my $x = $self->{'args'}->get_arg_values($calc, $n, 2);
101 my $y;
102 if ( defined( $self->{'args'}->get_arg_object(3) ) ) {
103 my $y = $self->{'args'}->get_arg_values($calc, $n, 3);
104 } else {
105 $y = $x;
106 $x = $n;
107 }
108  
109 $variance_x += (($x - $average_x) ** 2);
110 $variance_y += (($y - $average_y) ** 2);
111 }
112 $variance_x /= $period;
113 $variance_y /= $period;
114  
115 # Calculate the covariance(x,y)
116 my $covariance = $average_xy - $average_x * $average_y;
117
118 # Calculate the linear regression coefficients
119 my $a = $covariance / $variance_x;
120 my $b = $average_y - $a * $average_x;
121  
122 # Calculate the linear regression line value
123 my $linear_regression_line_value = $a * $i + $b;
124
125 # Return the results
126 $calc->indicators->set($linear_regression_line_name, $i, $linear_regression_line_value);
127 $calc->indicators->set($linear_regression_coefficient_a_name, $i, $a);
128 $calc->indicators->set($linear_regression_coefficient_b_name, $i, $b);
129 }
130  
131 1;