App/Chart/Series/Derived/NATR.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 78 CC 2 SCAR 9.2
CC
2
Cov
0%
proc
6.0
longname
2.0
shortname
2.0
CRAP 2.5
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::NATR;
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::ATR;
27 use App::Chart::Series::Derived::WilliamsR;
28  
29 # http://www.traders.com/Documentation/FEEDbk_docs/Archive/052006/TradersTips/TradersTips.html
30 # TASC Trader's Tips May 2006, various formulas.
31 #
32 # http://www.theessentialsoftrading.com/Blog/index.php/category/technical-analysis/
33 # Blog by John Forman, sample of S&P500 back to 1980.
34 #
35 # http://www.trade2win.com/knowledge/articles/general_articles/average-true-range-indicator/
36 # Article by John Forman (2 pages), sample of monthly S&P 500 from 1998
37 # to 2007 comparing ATR to NATR (seems to be 14-period smoothing).
38 # http://www.trade2win.com/knowledge/articles/general_articles/average-true-range-indicator/page2
39 # Continuing that article, sample of monthly S&P 500 from 1986 to 2006
40 # comparing ATR to NATR (again seems to be 14-period smoothing).
41 #
42 # http://www.theessentialsoftrading.com/
43 # John Forman's web site.
44 #
45  
46  
47 sub longname { __('NATR - Normalized ATR') }
48 sub shortname { __('NATR') }
49 sub manual { __p('manual-node','Normalized ATR') }
50  
51 use constant
52 { type => 'indicator',
53 units => 'natr', # percentage, but only a small one
54 minimum => 0,
55 parameter_info => [ { name => __('Days'),
56 key => 'atr_days', # shared with ATR.pm
57 type => 'integer',
58 minimum => 1,
59 default => 14 } ],
60 };
61  
62 sub new {
63 my ($class, $parent, $N) = @_;
64  
65 $N //= parameter_info()->[0]->{'default'};
66 ($N > 0) || croak "NATR bad N: $N";
67  
68 return $class->SUPER::new
69 (parent => $parent,
70 parameters => [ $N ],
71 arrays => { values => [] },
72 array_aliases => { });
73 }
74  
75 # Return a procedure which calculates a normalized average true range over
76 # an accumulated exponential moving average of $N days.
77 #
78 # Each call $proc->($high, $low, $close) enters a new day into the window,
79 # and the return is the NATR for that day, or undef if not enough points yet.
80 #
81 # $high and/or $low can be undef in each call, in which case $close is used.
82 # $closeq cannot be undef.
83 #
84 # A NATR is in theory influenced by all preceding data, but warmup_count()
85 # is designed to determine a warmup count.
86 #
87 sub proc {
88 my ($class_or_self, $N) = @_;
89 my $atr_proc = App::Chart::Series::Derived::ATR->proc ($N);
90  
91 return sub {
92 my ($high, $low, $close) = @_;
93 my $atr = $atr_proc->($high, $low, $close);
94 return ($close == 0 ? undef : 100 * $atr / $close);
95 };
96 }
97 *warmup_count = \&App::Chart::Series::Derived::ATR::warmup_count;
98 *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part;
99  
100 1;
101 __END__