| 1 | # Copyright 2006, 2007, 2009, 2024 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::KAMA; | ||
| 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::Calculation; | ||
| 27 | use App::Chart::Series::Derived::KAMAalpha; | ||
| 28 | |||
| 29 | use constant DEBUG => 0; | ||
| 30 | |||
| 31 | |||
| 32 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/0398/TradersTips/Tips9803.html | ||
| 33 | # Traders tips March 1998: metastock and easylanguage code | ||
| 34 | # | ||
| 35 | # http://www.perrykaufman.com | ||
| 36 | # Perry Kaufman's web site (no formulas). | ||
| 37 | # | ||
| 38 | # http://www.tssupport.com/support/base/?action=article&id=1769 | ||
| 39 | # Short tradestation article, incl EFS code. | ||
| 40 | # | ||
| 41 | # Code uses close() to close(-period), so N differences and N+1 total | ||
| 42 | # points. | ||
| 43 | # | ||
| 44 | |||
| 45 | |||
| 46 | sub longname { __('KAMA - Kaufmann Adaptive MA') } | ||
| 47 | sub shortname { __('KAMA') } | ||
| 48 | sub manual { __p('manual-node','Kaufman Adaptive Moving Average') } | ||
| 49 | |||
| 50 | use constant | ||
| 51 | { type => 'average', | ||
| 52 | parameter_info => App::Chart::Series::Derived::KAMAalpha::parameter_info(), | ||
| 53 | }; | ||
| 54 | |||
| 55 | sub new { | ||
| 56 | my ($class, $parent, $N) = @_; | ||
| 57 | |||
| 58 | $N //= parameter_info()->[0]->{'default'}; | ||
| 59 | ($N > 0) || croak "KAMA bad N: $N"; | ||
| 60 | |||
| 61 | return $class->SUPER::new | ||
| 62 | (parent => $parent, | ||
| 63 | parameters => [ $N ], | ||
| 64 | arrays => { values => [] }, | ||
| 65 | array_aliases => { }); | ||
| 66 | } | ||
| 67 | |||
| 68 | # warmup_count() gives a fixed amount, based on the worst-case EMA alphas | ||
| 69 | # all the slowest possible. It ends up being 1656 which is hugely more than | ||
| 70 | # needed in practice. | ||
| 71 | # | ||
| 72 | # warmup_count_for_position() calculates a value on actual data, working | ||
| 73 | # backwards. In practice it's as little as about 100. | ||
| 74 | # | ||
| 75 | use constant WORST_EMA_WARMUP => | ||
| 76 | App::Chart::Series::Derived::EMA->warmup_count | ||
| 77 | (App::Chart::Series::Derived::EMA::alpha_to_N | ||
| 78 | (App::Chart::Series::Derived::KAMAalpha->minimum)); | ||
| 79 | if (DEBUG) { | ||
| 80 | print "KAMA minimum alpha ",App::Chart::Series::Derived::KAMAalpha->minimum, "\n"; | ||
| 81 | print " worst N ",(App::Chart::Series::Derived::EMA::alpha_to_N | ||
| 82 | (App::Chart::Series::Derived::KAMAalpha->minimum)), "\n"; | ||
| 83 | print " WORST_EMA_WARMUP @{[WORST_EMA_WARMUP]}\n"; | ||
| 84 | } | ||
| 85 | sub warmup_count { | ||
| 86 | my ($self_or_class, $N) = @_; | ||
| 87 | return (WORST_EMA_WARMUP | ||
| 88 | + App::Chart::Series::Derived::KAMAalpha->warmup_count($N)); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub warmup_count_for_position { | ||
| 92 | return warmup_count_for_position_alphaclass | ||
| 93 | (@_, 'App::Chart::Series::Derived::KAMAalpha'); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub warmup_count_for_position_alphaclass { | ||
| 97 | my ($self, $lo, $alpha_class) = @_; | ||
| 98 | my $parent = $self->{'parent'}; | ||
| 99 | my $p = $parent->values_array; | ||
| 100 | |||
| 101 | my $p_lo = $lo + 1; # initial fill | ||
| 102 | my $chunk = 16; | ||
| 103 | |||
| 104 | my $alpha_proc = $alpha_class->proc(@{$self->{'parameters'}}); | ||
| 105 | my $alpha_warmup = $alpha_class->warmup_count(@{$self->{'parameters'}}); | ||
| 106 | my $tail = 1; | ||
| 107 | |||
| 108 | my $i = $lo; | ||
| 109 | for ( ; $i >= 0; $i--) { | ||
| 110 | if ($i < $p_lo) { | ||
| 111 | my $new_p_lo = min ($i, $p_lo - $chunk); | ||
| 112 | $chunk *= 2; | ||
| 113 | $parent->fill ($new_p_lo, $i); | ||
| 114 | $p_lo = $new_p_lo; | ||
| 115 | } | ||
| 116 | my $value = $p->[$i] // next; | ||
| 117 | $alpha_proc->($value) // next; | ||
| 118 | last if --$alpha_warmup <= 0; | ||
| 119 | } | ||
| 120 | for ( ; $i >= 0; $i--) { | ||
| 121 | if ($i < $p_lo) { | ||
| 122 | my $new_p_lo = min ($i, $p_lo - $chunk); | ||
| 123 | $chunk *= 2; | ||
| 124 | $parent->fill ($new_p_lo, $i); | ||
| 125 | $p_lo = $new_p_lo; | ||
| 126 | } | ||
| 127 | my $value = $p->[$i] // next; | ||
| 128 | my $alpha = $alpha_proc->($value) // next; | ||
| 129 | $tail *= (1 - $alpha); | ||
| 130 | if ($tail <= App::Chart::Series::Derived::EMA::WARMUP_OMITTED_FRACTION) { | ||
| 131 | last; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | if (DEBUG) { | ||
| 135 | print "warmup_count_for_position($alpha_class) for $lo is @{[$lo-$i]}\n"; } | ||
| 136 | return $lo - $i; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub proc { | ||
| 140 | my ($class, $N) = @_; | ||
| 141 | my $alpha_proc = App::Chart::Series::Derived::KAMAalpha->proc($N); | ||
| 142 | my $ama_proc = $class->adaptive_ema_proc(); | ||
| 143 | |||
| 144 | return sub { | ||
| 145 | my ($value) = @_; | ||
| 146 | my $alpha = $alpha_proc->($value) // return; | ||
| 147 | return $ama_proc->($alpha, $value); | ||
| 148 | }; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub adaptive_ema_proc { | ||
| 152 | my ($class) = @_; | ||
| 153 | |||
| 154 | my $sum = 0; | ||
| 155 | my $weight = 0; | ||
| 156 | return sub { | ||
| 157 | my ($alpha, $value) = @_; | ||
| 158 | |||
| 159 | $sum = $sum*(1-$alpha) + $value*$alpha; | ||
| 160 | |||
| 161 | # rearranged from | ||
| 162 | # w_next = w*(1-alpha) + 1*alpha | ||
| 163 | # = w - w*alpha + alpha | ||
| 164 | # = w + alpha*(1-w) | ||
| 165 | $weight += $alpha*(1-$weight); | ||
| 166 | |||
| 167 | # can have $weight==0 if first $alpha is 0 | ||
| 168 | return ($weight == 0 ? undef : $sum / $weight); | ||
| 169 | }; | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; | ||
| 173 | __END__ |