| 1 | # Copyright 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::REMA; | ||
| 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 | use Math::Trig (); | ||
| 25 | |||
| 26 | use base 'App::Chart::Series::Indicator'; | ||
| 27 | use App::Chart::Series::Derived::EMA; | ||
| 28 | |||
| 29 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/072003/Abstracts_new/Satchwell/satchwell.html | ||
| 30 | # Start of TASC August 2003 article by Chris Satchwell. | ||
| 31 | # | ||
| 32 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/072003/TradersTips/TradersTips.html | ||
| 33 | # TASC Traders' Tips August 2003, regularization formulas. | ||
| 34 | # | ||
| 35 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/092003/Letters/Letters.html | ||
| 36 | # TASC letters September 2003, Chris Satchwell clarifying derivation of | ||
| 37 | # formula. | ||
| 38 | # | ||
| 39 | # http://www.equis.com/Customer/Resources/TASC/Article.aspx?Id=50 | ||
| 40 | # Metastock code for regularized EMA and regularized momentum. | ||
| 41 | # | ||
| 42 | # http://trader.online.pl/ELZ/t-i-Regularization.html | ||
| 43 | # Easylanguage code, copy of trader's tips. | ||
| 44 | # | ||
| 45 | |||
| 46 | sub longname { __('REMA - Regularized EMA') } | ||
| 47 | sub shortname { __('REMA') } | ||
| 48 | sub manual { __p('manual-node','Regularized Exponential Moving Average') } | ||
| 49 | |||
| 50 | use constant | ||
| 51 | { type => 'average', | ||
| 52 | parameter_info => [ { name => __('Days'), | ||
| 53 | key => 'rema_days', | ||
| 54 | type => 'integer', | ||
| 55 | minimum => 1, | ||
| 56 | default => 21 }, | ||
| 57 | { name => __('Days'), | ||
| 58 | key => 'rema_lambda', | ||
| 59 | type => 'float', | ||
| 60 | decimals => 1, | ||
| 61 | minimum => 0, | ||
| 62 | default => 0.5, | ||
| 63 | step => 0.1 }], | ||
| 64 | }; | ||
| 65 | |||
| 66 | sub new { | ||
| 67 | my ($class, $parent, $N, $lambda) = @_; | ||
| 68 | |||
| 69 | $N //= parameter_info()->[0]->{'default'}; | ||
| 70 | ($N > 0) || croak "REMA bad N: $N"; | ||
| 71 | |||
| 72 | $lambda //= parameter_info()->[1]->{'default'}; | ||
| 73 | |||
| 74 | return $class->SUPER::new | ||
| 75 | (parent => $parent, | ||
| 76 | parameters => [ $N, $lambda ], | ||
| 77 | arrays => { values => [] }, | ||
| 78 | array_aliases => { }); | ||
| 79 | } | ||
| 80 | |||
| 81 | # A REMA is in theory influenced by all preceding data, but warmup_count() | ||
| 82 | # is designed to determine a warmup count. After calling $proc with | ||
| 83 | # warmup_count() many values, the next call will have an omitted weight of | ||
| 84 | # no more than 0.1% of the total. Omitting 0.1% should be negligable, | ||
| 85 | # unless past values are ridiculously bigger than recent ones. | ||
| 86 | # | ||
| 87 | # FIXME: probably shorter than the full EMA when lambda > 0 | ||
| 88 | # | ||
| 89 | sub warmup_count { | ||
| 90 | my ($class_or_self, $N, $lambda) = @_; | ||
| 91 | return 2 * App::Chart::Series::Derived::EMA->warmup_count ($N / $lambda); | ||
| 92 | } | ||
| 93 | |||
| 94 | # The formula | ||
| 95 | # | ||
| 96 | # Rprev * (2L+1) + alpha*(close - Rprev) - L*Rprevprev | ||
| 97 | # REMA = ---------------------------------------------------- | ||
| 98 | # L+1 | ||
| 99 | # | ||
| 100 | # is turned into the followering, in the style of the AmiBroker code in | ||
| 101 | # Trader's Tips above, | ||
| 102 | # | ||
| 103 | # a*close + b*Rprev + c*Rprevprev | ||
| 104 | # REMA = ------------------------------- | ||
| 105 | # L+1 | ||
| 106 | # | ||
| 107 | # with constants | ||
| 108 | # | ||
| 109 | # alpha 2*L+1-alpha -L | ||
| 110 | # a = ----- b = ----------- c = --- | ||
| 111 | # L+1 L+1 L+1 | ||
| 112 | # | ||
| 113 | sub proc { | ||
| 114 | my ($class, $N, $lambda) = @_; | ||
| 115 | |||
| 116 | my $alpha = App::Chart::Series::Derived::EMA::N_to_alpha ($N); | ||
| 117 | my $den = $lambda + 1; | ||
| 118 | my $a = $alpha / $den; | ||
| 119 | my $b = (2*$lambda + 1 - $alpha) / $den; | ||
| 120 | my $c = -$lambda / $den; | ||
| 121 | |||
| 122 | my $rP = 0; | ||
| 123 | my $rP_weight = 0; | ||
| 124 | my $rPP = 0; | ||
| 125 | my $rPP_weight = 0; | ||
| 126 | |||
| 127 | return sub { | ||
| 128 | my ($value) = @_; | ||
| 129 | |||
| 130 | my $r = $a*$value + $b*$rP + $c*$rPP; | ||
| 131 | my $r_weight = $a + $b*$rP_weight + $c*$rPP_weight; | ||
| 132 | |||
| 133 | $rPP = $rP; | ||
| 134 | $rPP_weight = $rP_weight; | ||
| 135 | $rP = $r; | ||
| 136 | $rP_weight = $r_weight; | ||
| 137 | |||
| 138 | return $r / $r_weight; | ||
| 139 | }; | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; | ||
| 143 | __END__ |