| 1 | # Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Math::Moving::EMA; | ||
| 18 | require 5; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use POSIX (); | ||
| 23 | |||
| 24 | use vars '@ISA'; | ||
| 25 | @ISA = ('App::Chart::Math::Moving'); | ||
| 26 | |||
| 27 | use constant type => 'average'; | ||
| 28 | use constant parameter_info_array => | ||
| 29 | [ { name => 'N', | ||
| 30 | share_key => 'ema_N', | ||
| 31 | type => 'float', | ||
| 32 | minimum => 1, | ||
| 33 | default => 20, | ||
| 34 | decimals => 0, | ||
| 35 | step => 1, | ||
| 36 | }, | ||
| 37 | ]; | ||
| 38 | |||
| 39 | sub warmup_omitted_fraction { | ||
| 40 | my ($self) = @_; | ||
| 41 | return $self->{'warmup_omitted_fraction'} || 0.001; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my $class = shift; | ||
| 46 | my $self = SUPER::new (@_); | ||
| 47 | $self->{'f'} = $self->N_to_f ($self->{'N'}); | ||
| 48 | $self->{'alpha'} = $self->N_to_alpha ($self->{'N'}); | ||
| 49 | $self->{'sum'} = 0; | ||
| 50 | $self->{'weight'} = 0; | ||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub next { | ||
| 55 | my ($self, $value) = @_; | ||
| 56 | |||
| 57 | # $sum is v0 + v1*f + v2*f^2 + v3*f^3 + ... + vk*f^k, for as many $value's | ||
| 58 | # as so far entered | ||
| 59 | # | ||
| 60 | # $weight is the corresponding 1 + f + f^2 + ... + f^k. This approaches | ||
| 61 | # 1/(1-f), but on the first few outputs it's much smaller, so must | ||
| 62 | # calculate it explicitly. | ||
| 63 | |||
| 64 | return ($self->{'sum'} | ||
| 65 | = $self->{'sum'} * $self->{'f'} + $value * $self->{'alpha'}) | ||
| 66 | / ($self->{'weight'} | ||
| 67 | = $self->{'weight'} * $self->{'f'} + $self->{'alpha'}); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub warmup_count { | ||
| 71 | my ($self) = @_; | ||
| 72 | if ($self->{'N'} <= 1) { | ||
| 73 | return 0; | ||
| 74 | } else { | ||
| 75 | return _ema_omitted_search ($self->{'f'}, | ||
| 76 | $self->warmup_omitted_fraction) - 1 ; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | # _ema_omitted_search() returns the number of terms t needed in an EMA to | ||
| 81 | # have an omitted part <= TARGET, where target is a proportion between 0 and | ||
| 82 | # 1. This means | ||
| 83 | # | ||
| 84 | # Omitted(t-1) <= target | ||
| 85 | # f^t <= target | ||
| 86 | # t >= log(target) / log(f) | ||
| 87 | # | ||
| 88 | # Can have f==0 when count==1 (a degenerate EMA, which just follows the | ||
| 89 | # given points exactly). log(0) isn't supported on guile 1.6, hence the | ||
| 90 | # special case. | ||
| 91 | # | ||
| 92 | # Actually log(f) approaches -2/N as N increases, but it's easy enough to | ||
| 93 | # do the calculation exactly. | ||
| 94 | # | ||
| 95 | sub _ema_omitted_search { | ||
| 96 | my ($f, $target) = @_; | ||
| 97 | if ($f == 0) { | ||
| 98 | return 0; | ||
| 99 | } else { | ||
| 100 | return POSIX::ceil (log($target) / log($f)); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | # ema_omitted() returns the fraction (between 0 and 1) of weight omitted by | ||
| 105 | # stopping an EMA at the f^k term, which means the first k+1 terms. | ||
| 106 | # | ||
| 107 | # The weight, out of a total 1, in those first terms | ||
| 108 | # | ||
| 109 | # W(k) = (1-f) (1 + f + f^2 + ... + f^k) | ||
| 110 | # | ||
| 111 | # multiplying through makes the middle terms cancel, leaving | ||
| 112 | # | ||
| 113 | # W(k) = 1 - f^(k+1) | ||
| 114 | # | ||
| 115 | # The omitted part is then O = 1-W, | ||
| 116 | # | ||
| 117 | # Omitted(k) = f^(k+1) | ||
| 118 | # | ||
| 119 | sub ema_omitted { | ||
| 120 | my ($f, $k) = @_; | ||
| 121 | return $f ** ($k + 1); | ||
| 122 | } | ||
| 123 | |||
| 124 | # alpha=2/(N+1) | ||
| 125 | sub N_to_alpha { | ||
| 126 | my ($class, $N) = @_; | ||
| 127 | return 2 / ($N + 1); | ||
| 128 | } | ||
| 129 | # f=1-2/(N+1), rearranged to f=(N-1)/(N+1). | ||
| 130 | sub N_to_f { | ||
| 131 | my ($class, $N) = @_; | ||
| 132 | return ($N - 1) / ($N + 1); | ||
| 133 | } | ||
| 134 | # N = 2/alpha - 1 | ||
| 135 | sub alpha_to_N { | ||
| 136 | my ($class, $alpha) = @_; | ||
| 137 | return 2 / $alpha - 1; | ||
| 138 | } | ||
| 139 | # convert a $N in J. Welles Wilder's reckoning to one in the standard form | ||
| 140 | # Wilder alpha=1/W, alpha=2/(N+1), so N=2*W-1 | ||
| 141 | sub N_from_Wilder_N { | ||
| 142 | my ($class, $W) = @_; | ||
| 143 | return 2*$W - 1; | ||
| 144 | } | ||
| 145 | sub N_to_Wilder_N { | ||
| 146 | my ($class, $N) = @_; | ||
| 147 | return ($N+1)/2; | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | __END__ |