| 1 | package Finance::GeniusTrader::Indicators::KAMA; | ||
| 2 | |||
| 3 | # Copyright 2003 Oliver Bossert | ||
| 4 | # This file is distributed under the terms of the General Public License | ||
| 5 | # version 2 or (at your option) any later version. | ||
| 6 | |||
| 7 | # Standards-Version: 1.0 | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 11 | |||
| 12 | use Finance::GeniusTrader::Indicators; | ||
| 13 | |||
| 14 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 15 | @NAMES = ("KAMA[#*]"); | ||
| 16 | @DEFAULT_ARGS = (21, 2, 30, "{I:Prices CLOSE}"); | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Finance::GeniusTrader::Indicators::KAMA - Perry Kaufmanns Adaptive Moving Average | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This Indicator was developed by Perry Kaufmann and presented in the | ||
| 25 | book "Trading Systems and Methods, 3rd Ed." in 1998. The KAMA is | ||
| 26 | automatically adapted to the volatility of the market. | ||
| 27 | |||
| 28 | The interpretation is similar to the classic SMA. | ||
| 29 | |||
| 30 | =over | ||
| 31 | |||
| 32 | =item Period (default 21) | ||
| 33 | |||
| 34 | The first argument is the period used to calculed the average. | ||
| 35 | |||
| 36 | =item Fastest period (default 30) | ||
| 37 | |||
| 38 | The fastest period to be considered. | ||
| 39 | |||
| 40 | =item Slowest period (default 2) | ||
| 41 | |||
| 42 | The slowest period to be considered. | ||
| 43 | |||
| 44 | =back | ||
| 45 | |||
| 46 | =head2 Creation | ||
| 47 | |||
| 48 | Finance::GeniusTrader::Indicators::KAMA->new() | ||
| 49 | Finance::GeniusTrader::Indicators::KAMA->new([10]) | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub initialize { | ||
| 54 | my ($self) = @_; | ||
| 55 | if ($self->{'args'}->is_constant(1) && ($self->{'args'}->get_nb_args() > 1)) { | ||
| 56 | $self->add_arg_dependency(4, $self->{'args'}->get_arg_constant(1)); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub calculate { | ||
| 61 | my ($self, $calc, $i) = @_; | ||
| 62 | my $nb = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 63 | my $fast = $self->{'args'}->get_arg_constant(2); | ||
| 64 | my $slow = $self->{'args'}->get_arg_constant(3); | ||
| 65 | my $name = $self->get_name; | ||
| 66 | my $kama = 0; | ||
| 67 | |||
| 68 | return if (! defined($nb)); | ||
| 69 | |||
| 70 | $self->remove_volatile_dependencies(); | ||
| 71 | $self->add_volatile_arg_dependency(4, $nb); | ||
| 72 | |||
| 73 | return if ($calc->indicators->is_available($name, $i)); | ||
| 74 | return if (! $self->check_dependencies($calc, $i)); | ||
| 75 | |||
| 76 | my $efratio = 1; | ||
| 77 | my $noise = 0; | ||
| 78 | for(my $n = $i - $nb + 1; $n <= $i; $n++) | ||
| 79 | { | ||
| 80 | $noise += abs( $self->{'args'}->get_arg_values($calc, $n-1, 4) - | ||
| 81 | $self->{'args'}->get_arg_values($calc, $n, 4)); | ||
| 82 | } | ||
| 83 | my $signal = abs( $self->{'args'}->get_arg_values($calc, $i-$nb+1, 4) - | ||
| 84 | $self->{'args'}->get_arg_values($calc, $i, 4)); | ||
| 85 | |||
| 86 | if ($noise != 0) | ||
| 87 | { | ||
| 88 | $efratio = $signal / $noise; | ||
| 89 | } | ||
| 90 | my $fastsc = 2/($fast+1); | ||
| 91 | my $slowsc = 2/($slow+1); | ||
| 92 | my $ssc = $efratio * ($fastsc-$slowsc) + $slowsc; | ||
| 93 | my $smooth = $ssc * $ssc; | ||
| 94 | |||
| 95 | my $ama; | ||
| 96 | if ($calc->indicators->is_available($name, $i-1)) | ||
| 97 | { | ||
| 98 | $ama = $calc->indicators->get($name, $i-1); | ||
| 99 | $kama = $ama + $smooth * ($self->{'args'}->get_arg_values($calc, $i, 4)-$ama); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $kama = $self->{'args'}->get_arg_values($calc, $i-1, 4); | ||
| 103 | } | ||
| 104 | |||
| 105 | $calc->indicators->set($name, $i, $kama); | ||
| 106 | } | ||
| 107 |