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

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 84 CC 2 SCAR 9.3
CC
2
Cov
0%
proc
6.0
longname
2.0
manual
2.0
CRAP 2.5
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::TRIX;
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::EMAx3;
27  
28  
29 # http://www.incrediblecharts.com/indicators/trix_indicator.php
30 # Also called "Trix Oscillator".
31  
32  
33 sub longname { __('TRIX') }
34 *shortname = \&longname;
35 sub manual { __p('manual-node','TRIX') }
36  
37 use constant
38 { type => 'indicator',
39 units => 'price_slope',
40 hlines => [ 0 ],
41 parameter_info => [ { name => __('Days'),
42 key => 'trix_days',
43 type => 'integer',
44 minimum => 1,
45 default => 20 } ],
46 };
47  
48 sub new {
49 my ($class, $parent, $N) = @_;
50 ### TRIX->new: \@_
51  
52 $N //= parameter_info()->[0]->{'default'};
53 ($N > 0) || croak "TRIX bad N: $N";
54  
55 return $class->SUPER::new
56 (parent => $parent,
57 parameters => [ $N ],
58 arrays => { values => [] },
59 array_aliases => { });
60 }
61 sub warmup_count {
62 my ($self_or_class, $N) = @_;
63 return 1 + App::Chart::Series::Derived::EMAx3->warmup_count($N);
64 }
65 sub proc {
66 my ($class_or_self, $N) = @_;
67 my $ema3_proc = App::Chart::Series::Derived::EMAx3->proc($N);
68 my $prev;
69  
70 return sub {
71 my ($value) = @_;
72 my $e3 = $ema3_proc->($value);
73 my $ret;
74 if (defined $prev && $prev != 0) {
75 $ret = 100 * ($e3 - $prev) / $prev;
76 }
77 $prev = $e3;
78 return $ret;
79 };
80 }
81  
82 1;
83 __END__