App-Chart
view release on metacpan or search on metacpan
lib/App/Chart/Series/Derived/KAMA.pm view on Meta::CPAN
# Copyright 2006, 2007, 2009, 2024 Kevin Ryde
# This file is part of Chart.
#
# Chart is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3, or (at your option) any later version.
#
# Chart is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Chart. If not, see <http://www.gnu.org/licenses/>.
package App::Chart::Series::Derived::KAMA;
use 5.010;
use strict;
use warnings;
use Carp;
use List::Util qw(min max);
use Locale::TextDomain ('App-Chart');
use base 'App::Chart::Series::Indicator';
use App::Chart::Series::Calculation;
use App::Chart::Series::Derived::KAMAalpha;
use constant DEBUG => 0;
# http://www.traders.com/Documentation/FEEDbk_docs/Archive/0398/TradersTips/Tips9803.html
# Traders tips March 1998: metastock and easylanguage code
#
# http://www.perrykaufman.com
# Perry Kaufman's web site (no formulas).
#
# http://www.tssupport.com/support/base/?action=article&id=1769
# Short tradestation article, incl EFS code.
#
# Code uses close() to close(-period), so N differences and N+1 total
# points.
#
sub longname { __('KAMA - Kaufmann Adaptive MA') }
sub shortname { __('KAMA') }
sub manual { __p('manual-node','Kaufman Adaptive Moving Average') }
use constant
{ type => 'average',
parameter_info => App::Chart::Series::Derived::KAMAalpha::parameter_info(),
};
sub new {
my ($class, $parent, $N) = @_;
$N //= parameter_info()->[0]->{'default'};
($N > 0) || croak "KAMA bad N: $N";
return $class->SUPER::new
(parent => $parent,
parameters => [ $N ],
arrays => { values => [] },
array_aliases => { });
}
# warmup_count() gives a fixed amount, based on the worst-case EMA alphas
# all the slowest possible. It ends up being 1656 which is hugely more than
# needed in practice.
#
# warmup_count_for_position() calculates a value on actual data, working
# backwards. In practice it's as little as about 100.
#
use constant WORST_EMA_WARMUP =>
App::Chart::Series::Derived::EMA->warmup_count
(App::Chart::Series::Derived::EMA::alpha_to_N
(App::Chart::Series::Derived::KAMAalpha->minimum));
if (DEBUG) {
print "KAMA minimum alpha ",App::Chart::Series::Derived::KAMAalpha->minimum, "\n";
print " worst N ",(App::Chart::Series::Derived::EMA::alpha_to_N
(App::Chart::Series::Derived::KAMAalpha->minimum)), "\n";
print " WORST_EMA_WARMUP @{[WORST_EMA_WARMUP]}\n";
}
sub warmup_count {
my ($self_or_class, $N) = @_;
return (WORST_EMA_WARMUP
+ App::Chart::Series::Derived::KAMAalpha->warmup_count($N));
}
sub warmup_count_for_position {
return warmup_count_for_position_alphaclass
(@_, 'App::Chart::Series::Derived::KAMAalpha');
}
sub warmup_count_for_position_alphaclass {
( run in 0.747 second using v1.01-cache-2.11-cpan-39bf76dae61 )