| 1 | package Finance::GeniusTrader::Indicators::SMA; | ||
| 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 | # $Id$ | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 13 | |||
| 14 | use Finance::GeniusTrader::Indicators; | ||
| 15 | |||
| 16 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 17 | @NAMES = ("SMA[#*]"); | ||
| 18 | @DEFAULT_ARGS = (50, "{I:Prices CLOSE}"); | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Finance::GeniusTrader::Indicators::SMA - Simple Moving Average | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | A simple arithmetic moving average. | ||
| 27 | |||
| 28 | =head2 Parameters | ||
| 29 | |||
| 30 | =over | ||
| 31 | |||
| 32 | =item Period (default 50) | ||
| 33 | |||
| 34 | The first argument is the period used to calculed the average. | ||
| 35 | |||
| 36 | =item Other data input | ||
| 37 | |||
| 38 | The second argument is optional. It can be used to specify an other | ||
| 39 | stream of input data for the average instead of the close prices. | ||
| 40 | This is usually an indicator (detailed via {I:MyIndic| <param>}) but | ||
| 41 | it can also be "{I:Prices OPEN}", "{I:Prices HIGH}", "{I:Prices LOW}", | ||
| 42 | "{I:Prices CLOSE}", "{I:Prices FIRST}" and "{I:Prices LAST}" and in | ||
| 43 | which cases the corresponding prices serie will be used. | ||
| 44 | |||
| 45 | =back | ||
| 46 | |||
| 47 | =head2 Creation | ||
| 48 | |||
| 49 | Finance::GeniusTrader::Indicators::SMA->new() | ||
| 50 | Finance::GeniusTrader::Indicators::SMA->new([20]) | ||
| 51 | |||
| 52 | If you need a 30 days SMA of the opening prices you can write | ||
| 53 | the following line: | ||
| 54 | |||
| 55 | Finance::GeniusTrader::Indicators::SMA->new([30, "{I:Prices OPEN}"]) | ||
| 56 | |||
| 57 | A 10 days SMA of the RSI could be created with : | ||
| 58 | |||
| 59 | Finance::GeniusTrader::Indicators::SMA->new([10, "{I:RSI}"]) | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub initialize { | ||
| 64 | my ($self) = @_; | ||
| 65 | if ($self->{'args'}->is_constant(1) && ($self->{'args'}->get_nb_args() > 1)) { | ||
| 66 | $self->add_arg_dependency(2, $self->{'args'}->get_arg_constant(1)); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | sub calculate { | ||
| 71 | my ($self, $calc, $i) = @_; | ||
| 72 | my $nb = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 73 | my $name = $self->get_name; | ||
| 74 | my $sum = 0; | ||
| 75 | |||
| 76 | return if $calc->indicators->is_available($name, $i); | ||
| 77 | |||
| 78 | return if (! defined($nb) || $nb==0); | ||
| 79 | |||
| 80 | $self->remove_volatile_dependencies(); | ||
| 81 | $self->add_volatile_arg_dependency(2, $nb); | ||
| 82 | |||
| 83 | return if (! $self->check_dependencies($calc, $i)); | ||
| 84 | |||
| 85 | for(my $n = $i - $nb + 1; $n <= $i; $n++) | ||
| 86 | { | ||
| 87 | my $val = $self->{'args'}->get_arg_values($calc, $n, 2); | ||
| 88 | return if (! defined($val)); | ||
| 89 | $sum += $val; | ||
| 90 | } | ||
| 91 | $calc->indicators()->set($name, $i, $sum / $nb); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub calculate_interval { | ||
| 95 | my ($self, $calc, $first, $last) = @_; | ||
| 96 | my $name = $self->get_name; | ||
| 97 | |||
| 98 | ($first, $last) = $self->update_interval($calc, $first, $last); | ||
| 99 | return if ($calc->indicators->is_available_interval($name, $first, $last)); | ||
| 100 | return if (! $self->check_dependencies_interval($calc, $first, $last)); | ||
| 101 | |||
| 102 | if ($self->{'args'}->is_constant(1)) { | ||
| 103 | my $nb = $self->{'args'}->get_arg_constant(1); | ||
| 104 | my $sum = 0; | ||
| 105 | # Calculate the new SMA value from the previous one when possible | ||
| 106 | for(my $i = $first - $nb + 1; $i <= $last; $i++) | ||
| 107 | { | ||
| 108 | my $val = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 109 | next if (! defined($val)); | ||
| 110 | if ($i <= $first) { | ||
| 111 | $sum += $val; | ||
| 112 | if ($i == $first) { | ||
| 113 | $calc->indicators->set($name, $i, $sum / $nb); | ||
| 114 | } | ||
| 115 | } else { | ||
| 116 | $sum += $val - $self->{'args'}->get_arg_values($calc, $i - $nb, 2); | ||
| 117 | $calc->indicators->set($name, $i, $sum / $nb); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } else { | ||
| 121 | # Calculate for each day separately | ||
| 122 | Finance::GeniusTrader::Indicators::calculate_interval(@_); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; |