| 1 | # Copyright 2006, 2007, 2009, 2010 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::Series::Derived::ZLEMA; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use Locale::TextDomain 1.17; # for __p() | ||
| 23 | use Locale::TextDomain ('App-Chart'); | ||
| 24 | |||
| 25 | use base 'App::Chart::Series::Indicator'; | ||
| 26 | use App::Chart::Series::Calculation; | ||
| 27 | use App::Chart::Series::Derived::EMA; | ||
| 28 | |||
| 29 | # http://www.linnsoft.com/tour/techind/movAvg.htm | ||
| 30 | # Showing as EMA of "2*price-price[lag]", where lag=(n-1)/2 | ||
| 31 | # | ||
| 32 | # http://www.mesasoftware.com/technicalpapers.htm | ||
| 33 | # http://www.mesasoftware.com/Papers/ZERO%20LAG.pdf | ||
| 34 | # John Ehlers on zero lag, with graphs of frequency response. | ||
| 35 | # | ||
| 36 | |||
| 37 | sub longname { __('ZLEMA - Zero Lag EMA') } | ||
| 38 | sub shortname { __('ZLEMA') } | ||
| 39 | sub manual { __p('manual-node','Zero-Lag Exponential Moving Average') } | ||
| 40 | |||
| 41 | use constant | ||
| 42 | { type => 'average', | ||
| 43 | parameter_info => [ { name => __('Days'), | ||
| 44 | key => 'zlema_days', | ||
| 45 | type => 'integer', | ||
| 46 | minimum => 0, | ||
| 47 | default => 20 } ], | ||
| 48 | }; | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my ($class, $parent, $N) = @_; | ||
| 52 | |||
| 53 | $N //= parameter_info()->[0]->{'default'}; | ||
| 54 | ($N > 0) or croak "ZLEMA bad N: $N"; | ||
| 55 | |||
| 56 | return $class->SUPER::new | ||
| 57 | (parent => $parent, | ||
| 58 | parameters => [ $N ], | ||
| 59 | N => $N, | ||
| 60 | lag => int (($N - 1) / 2), # (N-1)/2 | ||
| 61 | arrays => { values => [] }, | ||
| 62 | array_aliases => { }); | ||
| 63 | } | ||
| 64 | |||
| 65 | # Lag calculation: | ||
| 66 | # | ||
| 67 | # Taking a decreasing sequence with today price 0, yesterday price 1, the | ||
| 68 | # day before 2, etc, then the EMA today using the power formula is | ||
| 69 | # | ||
| 70 | # EMA = (1-f) * (0 + 1*f + 2*f^2 + 3*f^3 + 4*f^4 + ...) | ||
| 71 | # | ||
| 72 | # Multiplying through gives | ||
| 73 | # | ||
| 74 | # EMA = 0 + 1*f + 2*f^2 + 3*f^3 + 4*f^4 + ... | ||
| 75 | # - 0*f - 1*f^2 - 2*f^2 - 3*f^4 - ... | ||
| 76 | # | ||
| 77 | # = f + f^2 + f^3 + f^4 + ... | ||
| 78 | # | ||
| 79 | # = f * 1/(1-f) | ||
| 80 | # | ||
| 81 | # And with f=1-2/(N+1) meaning 1-f=2/(N+1), and also f=(N-1)/(N+1), | ||
| 82 | # | ||
| 83 | # N-1 N+1 | ||
| 84 | # EMA = --- * --- | ||
| 85 | # N+1 2 | ||
| 86 | # | ||
| 87 | # So EMA = (N-1)/2. Ie. the EMA is the value as at (N-1)/2 days ago, which | ||
| 88 | # is the lag. | ||
| 89 | # | ||
| 90 | sub N_to_lag { | ||
| 91 | my ($N) = @_; | ||
| 92 | return int (($N - 1) / 2); | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | # A ZLEMA is in theory influenced by all preceding data, but warmup_count() | ||
| 97 | # is designed to determine a warmup count. The next point will have an | ||
| 98 | # omitted weight of no more than 0.1% of the total. Omitting 0.1% should be | ||
| 99 | # negligable, unless past values are ridiculously bigger than recent ones. | ||
| 100 | # | ||
| 101 | # ENHANCE-ME: This is almost certainly an over-estimate since some of the | ||
| 102 | # EMA and its prev terms cancel out. | ||
| 103 | # | ||
| 104 | sub warmup_count { | ||
| 105 | my ($self_or_class, $N) = @_; | ||
| 106 | return N_to_lag($N) + App::Chart::Series::Derived::EMA->warmup_count($N); | ||
| 107 | } | ||
| 108 | sub proc { | ||
| 109 | my ($class_or_self, $N) = @_; | ||
| 110 | my $lag = N_to_lag ($N); | ||
| 111 | my $delay_proc = App::Chart::Series::Calculation->delay ($lag); | ||
| 112 | my $ema_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 113 | |||
| 114 | # FIXME: should still be able to follow weights when no $prev yet | ||
| 115 | return sub { | ||
| 116 | my ($value) = @_; | ||
| 117 | my $ema = $ema_proc->($value); | ||
| 118 | my $prev = $delay_proc->($ema) // $ema; | ||
| 119 | return 2*$ema - $prev; | ||
| 120 | }; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | __END__ |