| 1 | package Finance::GeniusTrader::Indicators::StandardError; | ||
| 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 | # requires Thomas Weigert revision to Finance::GeniusTrader::Indicators::LinearRegression | ||
| 9 | # $Id$ | ||
| 10 | |||
| 11 | # Standards-Version: 1.0 | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 15 | |||
| 16 | use Finance::GeniusTrader::Indicators; | ||
| 17 | use Finance::GeniusTrader::Indicators::LinearRegression; | ||
| 18 | use Finance::GeniusTrader::Prices; | ||
| 19 | |||
| 20 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 21 | @NAMES = ("StandardError[#1, #2]"); | ||
| 22 | @DEFAULT_ARGS = (20, "{I:Prices CLOSE}"); | ||
| 23 | |||
| 24 | =pod | ||
| 25 | |||
| 26 | =head1 Finance::GeniusTrader::Indicators::StandardError | ||
| 27 | |||
| 28 | =head2 Overview | ||
| 29 | |||
| 30 | Standard Error is a statistical measure of volatility. Standard Error is | ||
| 31 | typically used as a component of other indicators, rather than as a | ||
| 32 | stand-alone indicator. For example, Kirshenbaum Bands are calculated by | ||
| 33 | adding a security's Standard Error to an exponential moving average. | ||
| 34 | |||
| 35 | =head2 Calculation | ||
| 36 | |||
| 37 | Calculate the L-Period linear regression line, using today's Close as the | ||
| 38 | endpoint of the line. Note : The term "linear regression" is the same as | ||
| 39 | "least squares" or "best fit" line in some textbooks. | ||
| 40 | |||
| 41 | Calculate d1, d2, d3, ..., dL as the distance from the line to the Close | ||
| 42 | of each bar which was used to derive the line. That is, d(i) = Distance | ||
| 43 | from Regression Line to each bar's Close. | ||
| 44 | |||
| 45 | Average of squared errors (AE) = (d1² + d2² + d3² + ... + dL²) / L | ||
| 46 | Standard Error = Square Root of AE | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub initialize { | ||
| 51 | my $self = shift; | ||
| 52 | |||
| 53 | # Linear regression of the CLOSE against the sequence number | ||
| 54 | $self->{'linear_regression_line'} = Finance::GeniusTrader::Indicators::LinearRegression->new([ $self->{'args'}->get_arg_names(1), $self->{'args'}->get_arg_names(2) ]); | ||
| 55 | |||
| 56 | $self->add_indicator_dependency($self->{'linear_regression_line'}, $self->{'args'}->get_arg_constant(1)); | ||
| 57 | $self->add_prices_dependency($self->{'args'}->get_arg_constant(1)); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub calculate { | ||
| 61 | my ($self, $calc, $i) = @_; | ||
| 62 | my $indic = $calc->indicators; | ||
| 63 | my $period = $self->{'args'}->get_arg_constant(1); | ||
| 64 | my $linear_regression_coefficient_a_name = $self->{'linear_regression_line'}->get_name(1); | ||
| 65 | my $linear_regression_coefficient_b_name = $self->{'linear_regression_line'}->get_name(2); | ||
| 66 | my $standard_error_name = $self->get_name; | ||
| 67 | my $sum_of_the_squared_errors = 0; | ||
| 68 | |||
| 69 | return if ($indic->is_available($standard_error_name, $i)); | ||
| 70 | return if (! $self->check_dependencies($calc, $i)); | ||
| 71 | |||
| 72 | # Calculate and get the linear regression line coefficients | ||
| 73 | my $a = $indic->get($linear_regression_coefficient_a_name, $i); | ||
| 74 | my $b = $indic->get($linear_regression_coefficient_b_name, $i); | ||
| 75 | |||
| 76 | # Calculate the Standard Error | ||
| 77 | for (my $n = $i - $period + 1; $n <= $i; $n++) { | ||
| 78 | |||
| 79 | # Calculate the linear regression line value | ||
| 80 | my $linear_regression_line_value = $a * $n + $b; | ||
| 81 | |||
| 82 | # Calculate the distance from the linear regression line | ||
| 83 | my $d = $self->{'args'}->get_arg_values($calc, $n, 2) - $linear_regression_line_value; | ||
| 84 | |||
| 85 | # Calculate the sum of the squared errors | ||
| 86 | $sum_of_the_squared_errors += ($d ** 2); | ||
| 87 | } | ||
| 88 | |||
| 89 | # Calculate the average of the squared errors | ||
| 90 | my $average_of_the_squared_errors = $sum_of_the_squared_errors / $period; | ||
| 91 | |||
| 92 | # Calculate the standard error | ||
| 93 | my $standard_error = sqrt($average_of_the_squared_errors); | ||
| 94 | |||
| 95 | # Return the results | ||
| 96 | $indic->set($standard_error_name, $i, $standard_error); | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; |