| 1 | package Finance::GeniusTrader::Indicators::EMA; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaël Hertzog, Fabien Fulhaber | ||
| 4 | # Copyright 2008 Thomas Weigert | ||
| 5 | # Based on and for GeniusTrader (C) 2000-2002 Raphaël Hertzog, Fabien Fulhaber | ||
| 6 | # This file is distributed under the terms of the General Public License | ||
| 7 | # version 2 or (at your option) any later version. | ||
| 8 | |||
| 9 | # Standards-Version: 1.0 | ||
| 10 | |||
| 11 | # $Id$ | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 15 | |||
| 16 | use Finance::GeniusTrader::Indicators; | ||
| 17 | use Finance::GeniusTrader::Indicators::SMA; | ||
| 18 | use Finance::GeniusTrader::Eval; | ||
| 19 | |||
| 20 | |||
| 21 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 22 | @NAMES = ("EMA[#*]"); | ||
| 23 | @DEFAULT_ARGS = (20, "{I:Prices CLOSE}"); | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | Finance::GeniusTrader::Indicators::EMA - Exponential Moving Average | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | An exponential moving average gives more importance to | ||
| 32 | recent prices ... | ||
| 33 | |||
| 34 | =head2 Parameters | ||
| 35 | |||
| 36 | =over | ||
| 37 | |||
| 38 | =item Period (default 20) | ||
| 39 | |||
| 40 | The first argument is the period used to calculate the average. | ||
| 41 | |||
| 42 | =item Other data input | ||
| 43 | |||
| 44 | The second argument is optional. It can be used to specify an other | ||
| 45 | stream of input data for the average instead of the close prices. | ||
| 46 | This is usually an indicator (detailed via {I:MyIndic <param>}). | ||
| 47 | |||
| 48 | =item Start data input | ||
| 49 | |||
| 50 | The third argument is optional. It can be used to specify the | ||
| 51 | stream of input data to compute the starting point of the moving | ||
| 52 | average. The default is computed by the SMA of the given period. | ||
| 53 | |||
| 54 | If a very long period is required, it may be advisable to set this | ||
| 55 | to {I:PRICES CLOSE} (or whatever data stream is used as the input | ||
| 56 | for the EMA) to avoid excessive history data being required just to | ||
| 57 | compute the starting value. Using the first value of the input series | ||
| 58 | does not result in a large error and requires no dependencies. | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | =head2 Calculation | ||
| 63 | |||
| 64 | alpha = 2 / ( N + 1 ) | ||
| 65 | EMA[n] = EMA[n-1] + alpha * ( INPUT - EMA[n-1] ) | ||
| 66 | |||
| 67 | In TA, the first value is often constructed as SMA(N). | ||
| 68 | |||
| 69 | Note: One criticism could be that the EMA is calculated starting | ||
| 70 | from the designated period. But actually the EMA goes all the way back | ||
| 71 | to the beginning of the available data. But in all tools I checked they | ||
| 72 | start computation from the loaded data on. | ||
| 73 | |||
| 74 | =head2 Creation | ||
| 75 | |||
| 76 | Finance::GeniusTrader::Indicators::EMA->new() | ||
| 77 | Finance::GeniusTrader::Indicators::EMA->new([20]) | ||
| 78 | |||
| 79 | If you need a 30 days EMA of the opening prices you can write | ||
| 80 | one of those lines : | ||
| 81 | |||
| 82 | Finance::GeniusTrader::Indicators::EMA->new([30, "{I:Prices OPEN}"]) | ||
| 83 | |||
| 84 | A 10 days EMA of the RSI could be created with : | ||
| 85 | |||
| 86 | Finance::GeniusTrader::Indicators::EMA->new([10, "{I:RSI}"]) | ||
| 87 | |||
| 88 | Z<> | ||
| 89 | |||
| 90 | =cut | ||
| 91 | sub initialize { | ||
| 92 | my ($self) = @_; | ||
| 93 | |||
| 94 | my $start = $self->{'args'}->get_arg_names(3); | ||
| 95 | if ($start) { | ||
| 96 | if ($start =~ /^\s*{(.*)}\s*$/) { | ||
| 97 | $start = $1; | ||
| 98 | } | ||
| 99 | if ($start =~ /^\s*(\S+)\s*(.*)\s*$/) { | ||
| 100 | $self->{'start'} = create_standard_object("$1", $2); | ||
| 101 | } else { | ||
| 102 | die "Inappropriate argument $start given to ".$self->get_name." .\n"; | ||
| 103 | } | ||
| 104 | } else { | ||
| 105 | $self->{'start'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names() ]); | ||
| 106 | } | ||
| 107 | $self->add_indicator_dependency($self->{'start'}, 1); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub calculate { | ||
| 111 | my ($self, $calc, $i) = @_; | ||
| 112 | my $indic = $calc->indicators; | ||
| 113 | my $name = $self->get_name; | ||
| 114 | my $before = 1; | ||
| 115 | |||
| 116 | my $nb = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 117 | return if (! defined($nb)); | ||
| 118 | |||
| 119 | return if ($indic->is_available($name, $i)); | ||
| 120 | return if ($before && ! $self->check_dependencies($calc, $i)); | ||
| 121 | |||
| 122 | my $alpha = 2 / ($nb + 1); | ||
| 123 | |||
| 124 | my $oldema = $indic->get($name, $i - 1); | ||
| 125 | my $ema; | ||
| 126 | if (defined $oldema) { | ||
| 127 | $ema = $alpha * ($self->{'args'}->get_arg_values($calc, $i, 2) - $oldema) + $oldema; | ||
| 128 | } else { | ||
| 129 | $ema = $indic->get($self->{'start'}->get_name, $i); | ||
| 130 | $before = 0; | ||
| 131 | } | ||
| 132 | $indic->set($name, $i, $ema); | ||
| 133 | |||
| 134 | } | ||
| 135 | |||
| 136 | sub calculate_interval { | ||
| 137 | my ($self, $calc, $first, $last) = @_; | ||
| 138 | my $indic = $calc->indicators; | ||
| 139 | my $name = $self->get_name; | ||
| 140 | |||
| 141 | my $nb = $self->{'args'}->get_arg_constant(1); | ||
| 142 | return if (! defined($nb)); | ||
| 143 | |||
| 144 | return if ($indic->is_available_interval($name, $first, $last)); | ||
| 145 | # Don't need to calculate all SMA values, just the first data point. | ||
| 146 | $self->{'start'}->calculate($calc, $first); | ||
| 147 | |||
| 148 | while (! $self->check_dependencies_interval($calc, $first, $last)) { | ||
| 149 | return if $first == $last; | ||
| 150 | $first++; | ||
| 151 | } | ||
| 152 | |||
| 153 | my $alpha = 2 / ($nb + 1); | ||
| 154 | |||
| 155 | $indic->set($name, $first, $indic->get($self->{'start'}->get_name, $first)); | ||
| 156 | |||
| 157 | for (my $i=$first+1;$i<=$last;$i++) { | ||
| 158 | my $oldema = $indic->get($name, $i - 1); | ||
| 159 | my $ema = $alpha * ($self->{'args'}->get_arg_values($calc, $i, 2) - $oldema) + $oldema; | ||
| 160 | $indic->set($name, $i, $ema); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | 1; |