| 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::TEMA; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 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 | use App::Chart::Series::Derived::EMAx3; | ||
| 29 | |||
| 30 | sub longname { __('TEMA - Triple EMA') } | ||
| 31 | sub shortname { __('TEMA') } | ||
| 32 | sub manual { __p('manual-node','Double and Triple Exponential Moving Average') } | ||
| 33 | |||
| 34 | use constant | ||
| 35 | { type => 'average', | ||
| 36 | priority => -10, | ||
| 37 | parameter_info => [ { name => __('Days'), | ||
| 38 | key => 'tema_days', | ||
| 39 | type => 'integer', | ||
| 40 | minimum => 0, | ||
| 41 | default => 20 } ], | ||
| 42 | }; | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my ($class, $parent, $N) = @_; | ||
| 46 | |||
| 47 | $N //= parameter_info()->[0]->{'default'}; | ||
| 48 | ($N > 0) or croak "TEMA bad N: $N"; | ||
| 49 | |||
| 50 | return $class->SUPER::new | ||
| 51 | (parent => $parent, | ||
| 52 | parameters => [ $N ], | ||
| 53 | N => $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 | |||
| 63 | return sub { | ||
| 64 | my ($value) = @_; | ||
| 65 | my $e = $ema_proc->($value); | ||
| 66 | my $e2 = $ema2_proc->($e); | ||
| 67 | my $e3 = $ema3_proc->($e2); | ||
| 68 | |||
| 69 | return 3*$e - 3*$e2 + $e3; | ||
| 70 | }; | ||
| 71 | } | ||
| 72 | # A TEMA is in theory influenced by all preceding data, but warmup_count() | ||
| 73 | # is designed to determine a warmup count. The next point will have an | ||
| 74 | # omitted weight of no more than 0.1% of the total. Omitting 0.1% should be | ||
| 75 | # negligable, unless past values are ridiculously bigger than recent ones. | ||
| 76 | # | ||
| 77 | # FIXME: this is almost certainly more than needed, but a calculation like | ||
| 78 | # DEMA warmup_count() is a bit tricky | ||
| 79 | # | ||
| 80 | sub warmup_count { | ||
| 81 | my ($self_or_class, $N) = @_; | ||
| 82 | return App::Chart::Series::Derived::EMAx3->warmup_count($N); | ||
| 83 | |||
| 84 | # return max(App::Chart::Series::Derived::EMA->warmup_count($N), | ||
| 85 | # App::Chart::Series::Derived::EMAx2->warmup_count($N), | ||
| 86 | # ); | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | __END__ |