| 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::ADX; | ||
| 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::DMI; | ||
| 27 | use App::Chart::Series::Derived::WilliamsR; | ||
| 28 | |||
| 29 | # http://www.stockcharts.com/education/IndicatorAnalysis/indic_ADX.html | ||
| 30 | |||
| 31 | |||
| 32 | sub longname { __('ADX - Average Directional Index') } | ||
| 33 | sub shortname { __('ADX') } | ||
| 34 | sub manual { __p('manual-node','Average Directional Index') } | ||
| 35 | |||
| 36 | use constant | ||
| 37 | { type => 'indicator', | ||
| 38 | units => 'percentage', | ||
| 39 | minimum => 0, | ||
| 40 | maximum => 100, | ||
| 41 | }; | ||
| 42 | sub parameter_info { | ||
| 43 | return [ { name => __('Days'), | ||
| 44 | key => 'adx_days', | ||
| 45 | type => 'float', | ||
| 46 | minimum => 1, | ||
| 47 | default => 14, | ||
| 48 | decimals => 0, | ||
| 49 | step => 1 } ]; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my ($class, $parent, $N) = @_; | ||
| 54 | |||
| 55 | $N //= parameter_info()->[0]->{'default'}; | ||
| 56 | ($N > 0) || croak "ADX bad N: $N"; | ||
| 57 | |||
| 58 | return $class->SUPER::new | ||
| 59 | (parent => $parent, | ||
| 60 | parameters => [ $N ], | ||
| 61 | arrays => { values => [] }); | ||
| 62 | } | ||
| 63 | *warmup_count = \&App::Chart::Series::Derived::ATR::warmup_count; # EMA(W)+1 | ||
| 64 | *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part; | ||
| 65 | |||
| 66 | sub proc { | ||
| 67 | my ($class_or_self, $N) = @_; | ||
| 68 | my $dmi_proc = App::Chart::Series::Derived::DMI->proc($N); | ||
| 69 | |||
| 70 | return sub { | ||
| 71 | my ($high, $low, $close) = @_; | ||
| 72 | my ($di_plus, $di_minus) = $dmi_proc->($high, $low); | ||
| 73 | if (! defined $di_plus) { return; } | ||
| 74 | my $sum = $di_plus + $di_minus; | ||
| 75 | return ($sum == 0 ? 0 : 100 * abs($di_plus-$di_minus) / $sum); | ||
| 76 | }; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | 1; | ||
| 81 | __END__ |