| line | stmt | bran | cond | sub | pod | time | code | 
| 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::DEMA; | 
| 18 | 1 |  |  | 1 |  | 572 | use 5.010; | 
|  | 1 |  |  |  |  | 3 |  | 
| 19 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 19 |  | 
| 20 | 1 |  |  | 1 |  | 8 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 44 |  | 
| 21 | 1 |  |  | 1 |  | 7 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 68 |  | 
| 22 | 1 |  |  | 1 |  | 391 | use Locale::TextDomain 1.17; # for __p() | 
|  | 1 |  |  |  |  | 23169 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 23 | 1 |  |  | 1 |  | 8101 | use Locale::TextDomain ('App-Chart'); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 1 |  |  | 1 |  | 28 | use base 'App::Chart::Series::Indicator'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 460 |  | 
| 26 |  |  |  |  |  |  | use App::Chart::Series::Derived::EMA; | 
| 27 |  |  |  |  |  |  | use App::Chart::Series::Derived::EMAx2; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # http://www.stockworm.com/help/manual/double-ema.html | 
| 30 |  |  |  |  |  |  | #     Example graph PFE (what year?). | 
| 31 |  |  |  |  |  |  | # | 
| 32 |  |  |  |  |  |  | # http://trader.online.pl/ELZ/t-i-Double_Smoothed_Exponential_Moving_Average.html | 
| 33 |  |  |  |  |  |  | #     Tradestation example ^SPC 2001. | 
| 34 |  |  |  |  |  |  | # | 
| 35 |  |  |  |  |  |  | # http://store.traders.com/-v12-c01-smoothi-pdf.html | 
| 36 |  |  |  |  |  |  | #     Intro of TA S&C magazine, describing as faster. | 
| 37 |  |  |  |  |  |  | # | 
| 38 |  |  |  |  |  |  | # http://www.fmlabs.com/reference/DEMA.htm | 
| 39 |  |  |  |  |  |  | #     Formula only. | 
| 40 |  |  |  |  |  |  | # | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | sub longname   { __('DEMA - Double EMA') } | 
| 44 |  |  |  |  |  |  | sub shortname  { __('DEMA') } | 
| 45 |  |  |  |  |  |  | sub manual     { __p('manual-node','Double and Triple Exponential Moving Average') } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | use constant | 
| 48 |  |  |  |  |  |  | { type       => 'average', | 
| 49 |  |  |  |  |  |  | priority   => -10, | 
| 50 |  |  |  |  |  |  | parameter_info => [ { name    => __('Days'), | 
| 51 |  |  |  |  |  |  | key     => 'dema_days', | 
| 52 |  |  |  |  |  |  | type    => 'integer', | 
| 53 |  |  |  |  |  |  | minimum => 0, | 
| 54 |  |  |  |  |  |  | default => 20 } ], | 
| 55 |  |  |  |  |  |  | }; | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | sub new { | 
| 58 |  |  |  |  |  |  | my ($class, $parent, $N) = @_; | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | $N //= parameter_info()->[0]->{'default'}; | 
| 61 |  |  |  |  |  |  | ($N > 0) or croak "DEMA bad N: $N"; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | return $class->SUPER::new | 
| 64 |  |  |  |  |  |  | (parent     => $parent, | 
| 65 |  |  |  |  |  |  | parameters => [ $N ], | 
| 66 |  |  |  |  |  |  | N          => $N, | 
| 67 |  |  |  |  |  |  | arrays     => { values => [] }, | 
| 68 |  |  |  |  |  |  | array_aliases => { }); | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | sub proc { | 
| 71 |  |  |  |  |  |  | my ($class_or_self, $N) = @_; | 
| 72 |  |  |  |  |  |  | my $ema_proc = App::Chart::Series::Derived::EMA->proc($N); | 
| 73 |  |  |  |  |  |  | my $ema2_proc = App::Chart::Series::Derived::EMA->proc($N); | 
| 74 |  |  |  |  |  |  | return sub { | 
| 75 |  |  |  |  |  |  | my ($value) = @_; | 
| 76 |  |  |  |  |  |  | my $e = $ema_proc->($value); | 
| 77 |  |  |  |  |  |  | my $e2 = $ema2_proc->($e); | 
| 78 |  |  |  |  |  |  | return 2*$e - $e2; | 
| 79 |  |  |  |  |  |  | }; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | # A DEMA is in theory influenced by all preceding data, but warmup_count() | 
| 82 |  |  |  |  |  |  | # is designed to determine a warmup count.  The next point will have an | 
| 83 |  |  |  |  |  |  | # omitted weight of no more than 0.1% of the total.  Omitting 0.1% should be | 
| 84 |  |  |  |  |  |  | # negligable, unless past values are ridiculously bigger than recent ones. | 
| 85 |  |  |  |  |  |  | # | 
| 86 |  |  |  |  |  |  | # The implementation here does a binary search for the first i satisfying | 
| 87 |  |  |  |  |  |  | # Omitted(i)<=0.001, so it's only moderately fast. | 
| 88 |  |  |  |  |  |  | # | 
| 89 |  |  |  |  |  |  | sub warmup_count { | 
| 90 |  |  |  |  |  |  | my ($self_or_class, $N) = @_; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | if ($N <= 1) { return 0; } | 
| 93 |  |  |  |  |  |  | my $f = App::Chart::Series::Derived::EMA::N_to_f ($N); | 
| 94 |  |  |  |  |  |  | return App::Chart::Series::Derived::EMAx2::bsearch_first_true | 
| 95 |  |  |  |  |  |  | (sub { | 
| 96 |  |  |  |  |  |  | my ($i) = @_; | 
| 97 |  |  |  |  |  |  | return (dema_omitted($N,$f,$i) | 
| 98 |  |  |  |  |  |  | <= App::Chart::Series::Derived::EMA::WARMUP_OMITTED_FRACTION) }, | 
| 99 |  |  |  |  |  |  | $N); | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | # dema-omitted() returns the fraction (between 0 and 1) of absolute weight | 
| 103 |  |  |  |  |  |  | # omitted by stopping a DEMA at the f^k term, which means the first k+1 | 
| 104 |  |  |  |  |  |  | # terms. | 
| 105 |  |  |  |  |  |  | # | 
| 106 |  |  |  |  |  |  | # The EMA and EMAofEMA omitted totals are | 
| 107 |  |  |  |  |  |  | # | 
| 108 |  |  |  |  |  |  | #     Q(k) = f^(k+1) | 
| 109 |  |  |  |  |  |  | #     R(k) = f^(k+1) * (k+2 - f * (k+1)) | 
| 110 |  |  |  |  |  |  | # | 
| 111 |  |  |  |  |  |  | # thus for the DEMA the net signed amount, implemented in | 
| 112 |  |  |  |  |  |  | # dema_omitted_signed(), is | 
| 113 |  |  |  |  |  |  | # | 
| 114 |  |  |  |  |  |  | #     S(k) = 2*Q(k) - R(k) | 
| 115 |  |  |  |  |  |  | #          = f^(k+1) * (f*(k+1) - k) | 
| 116 |  |  |  |  |  |  | # | 
| 117 |  |  |  |  |  |  | # This grows above 1 up to the f^N term, then the terms go negative and it | 
| 118 |  |  |  |  |  |  | # decreases towards 1. | 
| 119 |  |  |  |  |  |  | # | 
| 120 |  |  |  |  |  |  | # The position of the negative/positive transition is always at f^N.  The | 
| 121 |  |  |  |  |  |  | # coefficient of that f^N term is | 
| 122 |  |  |  |  |  |  | # | 
| 123 |  |  |  |  |  |  | #     2 - (1-f)*(N+1) = 2 - (1-(N-1)/(N+1))*(N+1) | 
| 124 |  |  |  |  |  |  | #                     = 2 - (N+1-(N-1)) | 
| 125 |  |  |  |  |  |  | #                     = 2 - N - 1 + N - 1 | 
| 126 |  |  |  |  |  |  | #                     = 0 | 
| 127 |  |  |  |  |  |  | # | 
| 128 |  |  |  |  |  |  | # An absolute omitted weight is calculated from the signed omitted amount. | 
| 129 |  |  |  |  |  |  | # When k>N we can just negate the signed omitted.  When k<N we add the | 
| 130 |  |  |  |  |  |  | # negative terms past N in twice, first to cancel the negative then to add | 
| 131 |  |  |  |  |  |  | # in as positive. | 
| 132 |  |  |  |  |  |  | # | 
| 133 |  |  |  |  |  |  | # The total of all the negatives beyond N is -S(N), | 
| 134 |  |  |  |  |  |  | # | 
| 135 |  |  |  |  |  |  | #     tail = -S(N) = f^(N+1) * (f*(N+1) - N) | 
| 136 |  |  |  |  |  |  | #                  = f^(N+1) * ((N-1)/(N+1) * (N+1) - N) | 
| 137 |  |  |  |  |  |  | #                  = f^(N+1) * (N-1 - N) | 
| 138 |  |  |  |  |  |  | #                  = f^(N+1) | 
| 139 |  |  |  |  |  |  | # | 
| 140 |  |  |  |  |  |  | # Thus the absolute omitted, | 
| 141 |  |  |  |  |  |  | # | 
| 142 |  |  |  |  |  |  | #     T(k) = / - S(k)             if k >= N | 
| 143 |  |  |  |  |  |  | #            \ S(k) + 2*f^(N+1)   if k < N | 
| 144 |  |  |  |  |  |  | # | 
| 145 |  |  |  |  |  |  | # This is out of a total which is the positive and negative parts added as | 
| 146 |  |  |  |  |  |  | # abolute values.  Knowing pos+neg=1 and neg=-f^(N+1), | 
| 147 |  |  |  |  |  |  | # | 
| 148 |  |  |  |  |  |  | #     total absolute weight = 1 + 2*f(N+1) | 
| 149 |  |  |  |  |  |  | # | 
| 150 |  |  |  |  |  |  | # And that total is applied as a divisor, so the return from `dema-omitted' | 
| 151 |  |  |  |  |  |  | # is between 0 and 1.  (It works to call it with k=-1 for no terms omitted, | 
| 152 |  |  |  |  |  |  | # the result is 1.0.) | 
| 153 |  |  |  |  |  |  | # | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | sub dema_omitted { | 
| 156 |  |  |  |  |  |  | my ($N, $f, $k) = @_; | 
| 157 |  |  |  |  |  |  | my $tail = $f ** ($N + 1); | 
| 158 |  |  |  |  |  |  | my $num = dema_omitted_signed ($f, $k); | 
| 159 |  |  |  |  |  |  | if ($k >= $N) { | 
| 160 |  |  |  |  |  |  | $num = -$num; | 
| 161 |  |  |  |  |  |  | } else { | 
| 162 |  |  |  |  |  |  | $num += 2 * $tail; | 
| 163 |  |  |  |  |  |  | } | 
| 164 |  |  |  |  |  |  | return $num / (2*$tail + 1); | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | sub dema_omitted_signed { | 
| 168 |  |  |  |  |  |  | my ($f, $k) = @_; | 
| 169 |  |  |  |  |  |  | return $f**($k+1) * ($f*($k+1) -  $k); | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | 1; | 
| 173 |  |  |  |  |  |  | __END__ | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | # =head1 NAME | 
| 176 |  |  |  |  |  |  | # | 
| 177 |  |  |  |  |  |  | # App::Chart::Series::Derived::DEMA -- double-exponential moving average | 
| 178 |  |  |  |  |  |  | # | 
| 179 |  |  |  |  |  |  | # =head1 SYNOPSIS | 
| 180 |  |  |  |  |  |  | # | 
| 181 |  |  |  |  |  |  | #  my $series = $parent->DEMA($N); | 
| 182 |  |  |  |  |  |  | # | 
| 183 |  |  |  |  |  |  | # =head1 DESCRIPTION | 
| 184 |  |  |  |  |  |  | # | 
| 185 |  |  |  |  |  |  | # ... | 
| 186 |  |  |  |  |  |  | # | 
| 187 |  |  |  |  |  |  | # =head1 SEE ALSO | 
| 188 |  |  |  |  |  |  | # | 
| 189 |  |  |  |  |  |  | # L<App::Chart::Series>, L<App::Chart::Series::Derived::EMA> | 
| 190 |  |  |  |  |  |  | # | 
| 191 |  |  |  |  |  |  | # =cut |