| 1 | # Copyright 2003, 2004, 2005, 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::ATR; | ||
| 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::TrueRange; | ||
| 27 | use App::Chart::Series::Derived::EMA; | ||
| 28 | use App::Chart::Series::Derived::WilliamsR; | ||
| 29 | |||
| 30 | # http://www.incrediblecharts.com/indicators/average_true_range.php | ||
| 31 | # | ||
| 32 | # http://www.trade2win.com/knowledge/articles/general_articles/average-true-range-indicator/ | ||
| 33 | # Sample S&P 500 Sep2005 to Feb2006, seems to be 14-day smoothing. | ||
| 34 | |||
| 35 | |||
| 36 | sub longname { __('ATR - Average True Range') } | ||
| 37 | sub shortname { __('ATR') } | ||
| 38 | sub manual { __p('manual-node','Average True Range') } | ||
| 39 | |||
| 40 | use constant | ||
| 41 | { type => 'indicator', | ||
| 42 | units => 'price', | ||
| 43 | minimum => 0, | ||
| 44 | parameter_info => [ { name => __('Days'), | ||
| 45 | key => 'atr_days', | ||
| 46 | type => 'integer', | ||
| 47 | minimum => 1, | ||
| 48 | default => 14 } ], | ||
| 49 | }; | ||
| 50 | |||
| 51 | sub new { | ||
| 52 | my ($class, $parent, $N) = @_; | ||
| 53 | |||
| 54 | $N //= parameter_info()->[0]->{'default'}; | ||
| 55 | ($N > 0) || croak "ATR bad N: $N"; | ||
| 56 | |||
| 57 | return $class->SUPER::new | ||
| 58 | (parent => $parent, | ||
| 59 | parameters => [ $N ], | ||
| 60 | arrays => { values => [] }, | ||
| 61 | array_aliases => { }); | ||
| 62 | } | ||
| 63 | sub warmup_count { | ||
| 64 | my ($self_or_class, $N) = @_; | ||
| 65 | |||
| 66 | $N = App::Chart::Series::Derived::EMA::N_from_Wilder_N ($N); | ||
| 67 | return (App::Chart::Series::Derived::TrueRange->warmup_count() | ||
| 68 | + App::Chart::Series::Derived::EMA->warmup_count($N)); | ||
| 69 | } | ||
| 70 | sub proc { | ||
| 71 | my ($class_or_self, $N) = @_; | ||
| 72 | $N = App::Chart::Series::Derived::EMA::N_from_Wilder_N ($N); | ||
| 73 | my $tr_proc = App::Chart::Series::Derived::TrueRange->proc(); | ||
| 74 | my $ema_proc = App::Chart::Series::Derived::EMA->proc($N); | ||
| 75 | return sub { | ||
| 76 | my ($high, $low, $close) = @_; | ||
| 77 | return $ema_proc->($tr_proc->($high, $low, $close)); | ||
| 78 | }; | ||
| 79 | } | ||
| 80 | *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part; | ||
| 81 | |||
| 82 | 1; | ||
| 83 | __END__ |