| 1 | package Finance::GeniusTrader::Indicators::StandardDeviation; | ||
| 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::Prices; | ||
| 15 | |||
| 16 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 17 | @NAMES = ("StandardDeviation[#*]"); | ||
| 18 | @DEFAULT_ARGS = (20, "{I:Prices CLOSE}"); | ||
| 19 | |||
| 20 | =pod | ||
| 21 | |||
| 22 | =head1 Finance::GeniusTrader::Indicators::StandardDeviation | ||
| 23 | |||
| 24 | =head2 Overview | ||
| 25 | |||
| 26 | Standard Deviation is a statistical measure of volatility. Standard Deviation is typically used as a component of other indicators, rather than as a stand-alone indicator. For example, Bollinger Bands are calculated by adding a security's Standard Deviation to a moving average. | ||
| 27 | |||
| 28 | =head2 Interpretation | ||
| 29 | |||
| 30 | High Standard Deviation values occur when the data item being analyzed (e.g., prices or an indicator) is changing dramatically. Similarly, low Standard Deviation values occur when prices are stable. | ||
| 31 | |||
| 32 | Many analysts feel that major tops are accompanied with high volatility as investors struggle with both euphoria and fear. Major bottoms are expected to be calmer as investors have few expectations of profits. | ||
| 33 | |||
| 34 | =head2 Links | ||
| 35 | |||
| 36 | http://www.equis.com/free/taaz/standardevia.html | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub initialize { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | $self->{'sma'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names() ]); | ||
| 44 | |||
| 45 | } | ||
| 46 | |||
| 47 | sub calculate { | ||
| 48 | my ($self, $calc, $i) = @_; | ||
| 49 | my $indic = $calc->indicators; | ||
| 50 | my $period = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 51 | my $sma_name = $self->{'sma'}->get_name; | ||
| 52 | my $sd_name = $self->get_name(0); | ||
| 53 | my $sum = 0; | ||
| 54 | my $sd_value = 0; | ||
| 55 | |||
| 56 | $self->remove_volatile_dependencies(); | ||
| 57 | $self->add_volatile_indicator_dependency( $self->{'sma'}, $period ); | ||
| 58 | $self->add_volatile_arg_dependency( 2, $period ); | ||
| 59 | |||
| 60 | return if ($indic->is_available($sd_name, $i)); | ||
| 61 | return if (! $self->check_dependencies($calc, $i)); | ||
| 62 | |||
| 63 | # Get the SMA value | ||
| 64 | my $sma_value = $indic->get($sma_name, $i); | ||
| 65 | |||
| 66 | # Calculate the Standard Deviation | ||
| 67 | for (my $n = 0; $n < $period; $n++) { | ||
| 68 | $sum += ( $self->{'args'}->get_arg_values($calc, ($i-$n), 2) - $sma_value )**2; | ||
| 69 | } | ||
| 70 | $sd_value = sqrt($sum/$period); | ||
| 71 | |||
| 72 | # Return the results | ||
| 73 | $indic->set($sd_name, $i, $sd_value); | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; |