| 1 | package Finance::GeniusTrader::Indicators::BOL; | ||
| 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::StandardDeviation; | ||
| 15 | use Finance::GeniusTrader::Prices; | ||
| 16 | |||
| 17 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 18 | @NAMES = ("BOL[#1,#3]","BOLSup[#1,#2,#3]","BOLInf[#1,#2,#3]"); | ||
| 19 | @DEFAULT_ARGS = (20, 2, "{I:Prices CLOSE}"); | ||
| 20 | |||
| 21 | =head2 Finance::GeniusTrader::Indicators::BOL | ||
| 22 | |||
| 23 | Bollinger Bands are similar to moving average envelopes. The difference between Bollinger Bands and envelopes is envelopes are plotted at a fixed percentage above and below a moving average, whereas Bollinger Bands are plotted at standard deviation levels above and below a moving average. | ||
| 24 | |||
| 25 | The standard Bolling Bands (BOL 20-2) can be called like that : Finance::GeniusTrader::Indicators::BOL->new() | ||
| 26 | |||
| 27 | If you need a non standard BOL : | ||
| 28 | Finance::GeniusTrader::Indicators::BOL->new([25, 2.5]) | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | sub initialize { | ||
| 33 | my ($self) = @_; | ||
| 34 | |||
| 35 | if ($self->{'args'}->is_constant(1)) { | ||
| 36 | $self->add_prices_dependency($self->{'args'}->get_arg_constant(1)); | ||
| 37 | } | ||
| 38 | |||
| 39 | $self->{'sma'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names(1), | ||
| 40 | $self->{'args'}->get_arg_names(3) ]); | ||
| 41 | $self->{'sd'} = Finance::GeniusTrader::Indicators::StandardDeviation->new([ $self->{'args'}->get_arg_names(1), | ||
| 42 | $self->{'args'}->get_arg_names(3) ]); | ||
| 43 | |||
| 44 | if ($self->{'args'}->is_constant(1)) { | ||
| 45 | $self->add_prices_dependency($self->{'args'}->get_arg_constant(1)); | ||
| 46 | $self->add_indicator_dependency($self->{'sma'}, 1); | ||
| 47 | $self->add_indicator_dependency($self->{'sd'}, 1); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | =head2 Finance::GeniusTrader::Indicators::BOL::calculate($calc, $day) | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub calculate { | ||
| 56 | my ($self, $calc, $i) = @_; | ||
| 57 | my $indic = $calc->indicators; | ||
| 58 | my $period = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 59 | my $nsd = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 60 | my $sma = $self->{'sma'}; | ||
| 61 | my $sma_name = $sma->get_name; | ||
| 62 | my $sd = $self->{'sd'}; | ||
| 63 | my $sd_name = $sd->get_name; | ||
| 64 | my $bol_name = $self->get_name(0); | ||
| 65 | my $bolsup_name = $self->get_name(1); | ||
| 66 | my $bolinf_name = $self->get_name(2); | ||
| 67 | |||
| 68 | return if ($indic->is_available($bol_name, $i) && | ||
| 69 | $indic->is_available($bolsup_name, $i) && | ||
| 70 | $indic->is_available($bolinf_name, $i)); | ||
| 71 | return if (! $self->check_dependencies($calc, $i)); | ||
| 72 | |||
| 73 | # Get SMA value | ||
| 74 | my $sma_value = $indic->get($sma_name, $i); | ||
| 75 | |||
| 76 | # Get Standard Deviation value | ||
| 77 | my $sd_value = $indic->get($sd_name, $i); | ||
| 78 | |||
| 79 | # Bollinger Band Sup is equal to the value of the moving average + standard deviation | ||
| 80 | my $bolsup_value = $sma_value + ($nsd * $sd_value); | ||
| 81 | |||
| 82 | # Bollinger Band Inf is equal to the value of the moving average - standard deviation | ||
| 83 | my $bolinf_value = $sma_value - ($nsd * $sd_value); | ||
| 84 | |||
| 85 | # Return the results | ||
| 86 | $indic->set($bol_name, $i, $sma_value); | ||
| 87 | $indic->set($bolsup_name, $i, $bolsup_value); | ||
| 88 | $indic->set($bolinf_name, $i, $bolinf_value); | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; |