| 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::EMAx3; | ||
| 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::Derived::EMA; | ||
| 27 | use App::Chart::Series::Derived::EMAx2; | ||
| 28 | |||
| 29 | sub longname { __('EMA of EMA of EMA') } | ||
| 30 | sub shortname { __('EMAx3') } | ||
| 31 | sub manual { __p('manual-node','EMA of EMA of EMA') } | ||
| 32 | |||
| 33 | use constant | ||
| 34 | { type => 'average', | ||
| 35 | priority => -10, | ||
| 36 | parameter_info => [ { name => __('Days'), | ||
| 37 | key => 'ema3_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 "EMA3 bad N: $N"; | ||
| 50 | |||
| 51 | return $class->SUPER::new | ||
| 52 | (parent => $parent, | ||
| 53 | parameters => [ $N ], | ||
| 54 | arrays => { values => [] }, | ||
| 55 | array_aliases => { }); | ||
| 56 | } | ||
| 57 | sub proc { | ||
| 58 | my ($class_or_self, $N) = @_; | ||
| 59 | my $ema_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 60 | my $ema2_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 61 | my $ema3_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 62 | return sub { $ema3_proc->($ema2_proc->($ema_proc->($_[0]))) }; | ||
| 63 | } | ||
| 64 | |||
| 65 | # By priming an EMA-3 accumulator with warmup_count() many values, the next | ||
| 66 | # call will have an omitted weight of no more than 0.1% of the total. | ||
| 67 | # Omitting 0.1% should be negligable, unless past values are ridiculously | ||
| 68 | # bigger than recent ones. | ||
| 69 | # | ||
| 70 | # The implementation here does a binary search for the first i satisfying | ||
| 71 | # R(i)<=0.001, so it's not very fast. Perhaps there'd be a direct | ||
| 72 | # closed-form solution to that equation, but f^i*(quadratic in i) doesn't | ||
| 73 | # look like it can rearrange. | ||
| 74 | # | ||
| 75 | sub warmup_count { | ||
| 76 | my ($self_or_class, $N) = @_; | ||
| 77 | if ($N <= 1) { return 0; } | ||
| 78 | |||
| 79 | my $f = App::Chart::Series::Derived::EMA::N_to_f ($N); | ||
| 80 | return App::Chart::Series::Derived::EMAx2::bsearch_first_true | ||
| 81 | (sub { | ||
| 82 | my ($i) = @_; | ||
| 83 | return (ema3_omitted($f,$i) | ||
| 84 | <= App::Chart::Series::Derived::EMA::WARMUP_OMITTED_FRACTION) }, | ||
| 85 | $N); | ||
| 86 | } | ||
| 87 | |||
| 88 | # ema3_omitted() returns the fraction, between 0 and 1, of weight omitted | ||
| 89 | # by stopping an EMA of EMA of EMA at the f^k term, which means taking the | ||
| 90 | # first k+1 terms. | ||
| 91 | # | ||
| 92 | # The total weight up to that term is, | ||
| 93 | # | ||
| 94 | # W(k) = (1-f)^3 * ( 1 + 3f + 6f^2 + 10f^3 + ... + T(k+1)*f^k ) | ||
| 95 | # | ||
| 96 | # where T(k)=k*(k+1)/2 is a triangle number. Multiplying the (1-f)^3 | ||
| 97 | # through leads to the middle terms cancelling, because | ||
| 98 | # | ||
| 99 | # T(k+1) - 3*T(k) + 3*T(k-1) - T(k-2) = 0 | ||
| 100 | # | ||
| 101 | # Which leaves just three at the end, | ||
| 102 | # | ||
| 103 | # W(k) = 1 + (-3*T(k+1) + 3*T(k) - T(k-1)) * f^(k+1) | ||
| 104 | # + ( 3*T(k+1) - T(k) ) * f^(k+2) | ||
| 105 | # + ( - T(k+1)) * f^(k+3) | ||
| 106 | # | ||
| 107 | # The omitted part is 1 - W(k), so the "1 +" is dropped and the rest | ||
| 108 | # negated. The triangle number terms simplify to quadratics in i, | ||
| 109 | # | ||
| 110 | # R(k) = f^(k+1) * 1/2 * ( k^2 + 5*k + 6 | ||
| 111 | # + f * ( -2*k^2 - 8*k - 6 | ||
| 112 | # + f * (k+1)*(k+2))) | ||
| 113 | # | ||
| 114 | # See devel/ema-omitted.pl for automated checking of this calculation. | ||
| 115 | # | ||
| 116 | sub ema3_omitted { | ||
| 117 | my ($f, $k) = @_; | ||
| 118 | return $f**($k+1) | ||
| 119 | * 0.5 | ||
| 120 | * ($k * ($k + 5) + 6 # k^2 + 5*k + 6 | ||
| 121 | + $f * ($k * (-2*$k - 8) - 6 # -2*k^2 - 8*k - 6 | ||
| 122 | + $f * ($k + 1) * ($k + 2))); # (k+1)*(k+2) | ||
| 123 | } | ||
| 124 | |||
| 125 | 1; | ||
| 126 | __END__ |