| 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::EMAx2; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use POSIX (); | ||
| 23 | use Locale::TextDomain 1.17; # for __p() | ||
| 24 | use Locale::TextDomain ('App-Chart'); | ||
| 25 | |||
| 26 | use base 'App::Chart::Series::Indicator'; | ||
| 27 | use App::Chart::Series::Derived::EMA; | ||
| 28 | |||
| 29 | sub longname { __('EMA of EMA') } | ||
| 30 | sub shortname { __('EMAofEMA') } | ||
| 31 | sub manual { __p('manual-node','EMA of EMA') } | ||
| 32 | |||
| 33 | use constant | ||
| 34 | { type => 'average', | ||
| 35 | priority => -10, | ||
| 36 | parameter_info => [ { name => __('Days'), | ||
| 37 | key => 'ema2_days', | ||
| 38 | type => 'float', | ||
| 39 | minimum => 1, | ||
| 40 | default => 20, | ||
| 41 | decimals => 0, | ||
| 42 | step => 1 } ], | ||
| 43 | }; | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my ($class, $parent, $N) = @_; | ||
| 47 | |||
| 48 | $N //= parameter_info()->[0]->{'default'}; | ||
| 49 | ($N > 0) or croak "EMA2 bad N: $N"; | ||
| 50 | |||
| 51 | return $class->SUPER::new | ||
| 52 | (parent => $parent, | ||
| 53 | parameters => [ $N ], | ||
| 54 | N => $N, | ||
| 55 | arrays => { values => [] }, | ||
| 56 | array_aliases => { }); | ||
| 57 | } | ||
| 58 | |||
| 59 | # Return a procedure which calculates an EMA of EMA with given $N period | ||
| 60 | # smoothing (on both). | ||
| 61 | # | ||
| 62 | # Each call $proc->($value) enters a new value into the window, and the | ||
| 63 | # return is the EMAofEMA up to (and including) that value. | ||
| 64 | # | ||
| 65 | # An EMA of EMA is in theory influenced by all preceding data, but | ||
| 66 | # warmup_count() below is designed to determine a warmup count. | ||
| 67 | # | ||
| 68 | sub proc { | ||
| 69 | my ($class_or_self, $N) = @_; | ||
| 70 | my $ema_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 71 | my $ema2_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 72 | return sub { $ema2_proc->($ema_proc->($_[0])) }; | ||
| 73 | } | ||
| 74 | |||
| 75 | # By priming an EMA-2 pro with warmup_count() many values, the next call | ||
| 76 | # will have an omitted weight of no more than 0.1% of the total. Omitting | ||
| 77 | # 0.1% should be negligable, unless past values are ridiculously bigger than | ||
| 78 | # recent ones. | ||
| 79 | # | ||
| 80 | # The implementation here does a binary search for the first k satisfying | ||
| 81 | # R(k)<=0.001, so it's only moderately fast. Perhaps there'd be a direct | ||
| 82 | # closed-form solution to the equation R(k)=0.001 below. The inverse of | ||
| 83 | # k*f^k is close to the Lambert W-function. Is there an easy formula for | ||
| 84 | # that? | ||
| 85 | # | ||
| 86 | sub warmup_count { | ||
| 87 | my ($self_or_class, $N) = @_; | ||
| 88 | |||
| 89 | if ($N <= 1) { return 0; } | ||
| 90 | my $f = App::Chart::Series::Derived::EMA::N_to_f ($N); | ||
| 91 | return bsearch_first_true | ||
| 92 | (sub { | ||
| 93 | my ($i) = @_; | ||
| 94 | return (ema2_omitted($f,$i) | ||
| 95 | <= App::Chart::Series::Derived::EMA::WARMUP_OMITTED_FRACTION); | ||
| 96 | }, | ||
| 97 | $N); | ||
| 98 | } | ||
| 99 | |||
| 100 | # ema2_omitted() returns the fraction (between 0 and 1) of weight omitted | ||
| 101 | # by stopping an EMA of EMA at the f^k term, which means the first k+1 | ||
| 102 | # terms. | ||
| 103 | # | ||
| 104 | # The total weight up to that term is, | ||
| 105 | # | ||
| 106 | # W(k) = (1-f)^2 * ( 1 + 2f + 3f^2 + 4f^3 + ... + (k+1)*f^k ) | ||
| 107 | # | ||
| 108 | # Multiplying (1-f)^2 through leads to the middle terms cancelling, leaving | ||
| 109 | # just the 1 at the start, and two terms at the end, | ||
| 110 | # | ||
| 111 | # W(k) = 1 + (-2*(k+1) + k) * f^(k+1) | ||
| 112 | # + (k+1) * f^(k+2) | ||
| 113 | # | ||
| 114 | # The omitted part is 1 - W(k), so the "1 +" is dropped and the rest | ||
| 115 | # negated. The terms simplify to | ||
| 116 | # | ||
| 117 | # R(k) = f^(k+1) * (k+2 - f * (k+1)) | ||
| 118 | # | ||
| 119 | # See devel/ema-omitted.pl for automated checking of this calculation. | ||
| 120 | # | ||
| 121 | sub ema2_omitted { | ||
| 122 | my ($f, $k) = @_; | ||
| 123 | return $f**($k+1) * ($k+2 - $f*($k+1)); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub bsearch_first_true { | ||
| 127 | my ($pred, $incr) = @_; | ||
| 128 | $incr = POSIX::ceil ($incr); | ||
| 129 | |||
| 130 | my $lo = 0; | ||
| 131 | my $hi = $incr; | ||
| 132 | |||
| 133 | # search upwards | ||
| 134 | until ($pred->($hi)) { | ||
| 135 | $lo = $hi + 1; | ||
| 136 | $hi *= 2; | ||
| 137 | } | ||
| 138 | |||
| 139 | # at this point $pred->($lo) is unknown, $pred->($hi) is true | ||
| 140 | if ($pred->($lo)) { return $lo; } | ||
| 141 | |||
| 142 | # binary search loop, with $pred->($lo) false, $pred->($hi) true | ||
| 143 | for (;;) { | ||
| 144 | if ($hi - $lo <= 1) { | ||
| 145 | return $hi; | ||
| 146 | } | ||
| 147 | my $mid = POSIX::floor (($hi + $lo) / 2); | ||
| 148 | if ($pred->($mid)) { | ||
| 149 | $lo = $mid; | ||
| 150 | } else { | ||
| 151 | $hi = $mid; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | 1; | ||
| 157 | __END__ |