| 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::T3; | ||
| 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 | |||
| 28 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/0298/TradersTips/Tips9802.html | ||
| 29 | # Traders Tips Feb 1998, implementations of T3 and GD. | ||
| 30 | # | ||
| 31 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/0898/Letters9808/Letters9808.html | ||
| 32 | # TASC letters Oct 1998, George Herbert on GD as weighted average of | ||
| 33 | # EMA and DEMA. | ||
| 34 | # | ||
| 35 | # http://trader.online.pl/ELZ/t-i-T3.html | ||
| 36 | # Formula in terms of GD. Sample S&P 500 (symbol ^GSPC) from 2001, | ||
| 37 | # with N=6 and v=0.7. | ||
| 38 | # | ||
| 39 | # http://www.linnsoft.com/tour/techind/t3.htm | ||
| 40 | # | ||
| 41 | |||
| 42 | sub longname { __('T3 Moving Average') } | ||
| 43 | sub shortname { __('T3') } | ||
| 44 | sub manual { __p('manual-node','T3 Moving Average') } | ||
| 45 | |||
| 46 | use constant | ||
| 47 | { type => 'average', | ||
| 48 | parameter_info => [ { name => __('Days'), | ||
| 49 | key => 't3_days', | ||
| 50 | type => 'integer', | ||
| 51 | minimum => 0, | ||
| 52 | default => 20 }, | ||
| 53 | { name => __('Factor'), | ||
| 54 | key => 't3_factor', | ||
| 55 | type => 'float', | ||
| 56 | default => 0.7, | ||
| 57 | step => 0.1, | ||
| 58 | decimals => 1, | ||
| 59 | minimum => 0, | ||
| 60 | maximum => 1 } ], | ||
| 61 | }; | ||
| 62 | |||
| 63 | sub new { | ||
| 64 | my ($class, $parent, $N, $vf) = @_; | ||
| 65 | |||
| 66 | $N //= parameter_info()->[0]->{'default'}; | ||
| 67 | ($N > 0) or croak "T3 bad N: $N"; | ||
| 68 | |||
| 69 | $vf //= parameter_info()->[1]->{'default'}; | ||
| 70 | |||
| 71 | return $class->SUPER::new | ||
| 72 | (parent => $parent, | ||
| 73 | N => $N, | ||
| 74 | parameters => [ $N, $vf ], | ||
| 75 | arrays => { values => [] }, | ||
| 76 | array_aliases => { }); | ||
| 77 | } | ||
| 78 | sub proc { | ||
| 79 | my ($class_or_self, $N, $vf) = @_; | ||
| 80 | |||
| 81 | my $c1 = -($vf**3); | ||
| 82 | my $c2 = 3*($vf**2) + 3*($vf**3); | ||
| 83 | my $c3 = -6*($vf**2) + -3*$vf + -3*($vf**3); | ||
| 84 | my $c4 = 1 + 3*$vf + ($vf**3) + 3*($vf**2); | ||
| 85 | |||
| 86 | my $ema_proc = App::Chart::Series::Derived::EMA->proc($N); | ||
| 87 | my $ema2_proc = App::Chart::Series::Derived::EMA->proc($N); | ||
| 88 | my $ema3_proc = App::Chart::Series::Derived::EMA->proc($N); | ||
| 89 | my $ema4_proc = App::Chart::Series::Derived::EMA->proc($N); | ||
| 90 | my $ema5_proc = App::Chart::Series::Derived::EMA->proc($N); | ||
| 91 | my $ema6_proc = App::Chart::Series::Derived::EMA->proc($N); | ||
| 92 | |||
| 93 | return sub { | ||
| 94 | my ($value) = @_; | ||
| 95 | |||
| 96 | my $a = $ema_proc->($value); | ||
| 97 | my $b = $ema2_proc->($a); | ||
| 98 | my $c = $ema3_proc->($b); | ||
| 99 | my $d = $ema4_proc->($c); | ||
| 100 | my $e = $ema5_proc->($d); | ||
| 101 | my $f = $ema6_proc->($e); | ||
| 102 | |||
| 103 | return ($c4 * $c | ||
| 104 | + $c3 * $d | ||
| 105 | + $c2 * $e | ||
| 106 | + $c1 * $f); | ||
| 107 | }; | ||
| 108 | } | ||
| 109 | |||
| 110 | # T3 is in theory influenced by all preceding data, but warmup_count() is | ||
| 111 | # designed to determine a warmup count. The next point will have an omitted | ||
| 112 | # weight of no more than 0.1% of the total. Omitting 0.1% should be | ||
| 113 | # negligable, unless past values are ridiculously bigger than recent ones. | ||
| 114 | # | ||
| 115 | # FIXME: this is the worst-case of $vf==0 which makes T3 an EMAx3. Less | ||
| 116 | # warmup is needed for a DEMAx3. | ||
| 117 | # | ||
| 118 | sub warmup_count { | ||
| 119 | my ($class_or_self, $N) = @_; | ||
| 120 | require App::Chart::Series::Derived::EMAx3; | ||
| 121 | return App::Chart::Series::Derived::EMAx3->warmup_count ($N); | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; | ||
| 125 | __END__ |