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

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 124 CC 7 SCAR 28.8
CC
7
Cov
0%
proc
56.0
longname
2.0
shortname
2.0
CRAP 17.8
1 # Copyright 2006, 2007, 2009, 2010, 2014 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::PFE;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use List::Util qw(min max);
23 use Locale::TextDomain 1.17; # for __p()
24 use Locale::TextDomain ('App-Chart');
25  
26 use base 'App::Chart::Series::Indicator';
27 use App::Chart::Series::Calculation;
28 use App::Chart::Series::Derived::EMA;
29  
30  
31 # http://transcripts.fxstreet.com/2005/09/polarized_fract.html
32 # Presentation on use.
33 #
34 # http://trader.online.pl/MSZ/e-w-Polarized_Fractal_Efficiency.html
35 # Mov(If(C, >, Ref(C,-9),
36 # Sqr(Pwr(Roc(C,9,$),2) + Pwr(10,2))
37 # / Sum(Sqr( Pwr(Roc(C,1,$),2) + 1), 9),
38 # -Sqr(Pwr(Roc(C,9,$),2) + Pwr(10,2))
39 # / Sum(Sqr( Pwr(Roc(C,1,$),2) + 1), 9))
40 # *100, 5, E)
41 #
42 # http://www.traderslog.com/polarized-fractal-efficiency.htm
43 # Sample chart of IBM from 2002. Year not shown, but data is 2002.
44 # Seems based on ROC as percentage, day step as 1 in hypot, and 5-day
45 # EMA smooth.
46 #
47 # http://store.traders.com/-v12-c01-polariz-pdf.html
48 # TASC article for sale.
49 #
50  
51 sub longname { __('PFE - Polarized Fractal Efficiency') }
52 sub shortname { __('PFE') }
53 sub manual { __p('manual-node','Polarized Fractal Efficiency') }
54  
55 use constant
56 { type => 'indicator',
57 units => 'percentage_plus_or_minus_100',
58 minimum => -100,
59 maximum => 100,
60 hlines => [ 0 ],
61 parameter_info => [ { name => __('Days'),
62 key => 'pfe_days',
63 type => 'integer',
64 minimum => 1,
65 default => 10 },
66 { name => __('Smooth'),
67 key => 'pfe_smooth_days',
68 type => 'float',
69 minimum => 0,
70 default => 5,
71 decimals => 0,
72 step => 1 }],
73 };
74  
75 sub new {
76 my ($class, $parent, $N, $smooth_N) = @_;
77  
78 $N //= parameter_info()->[0]->{'default'};
79 ($N >= 1) || croak "PFE bad N: $N";
80  
81 $smooth_N //= parameter_info()->[1]->{'default'};
82 ($smooth_N >= 0) || croak "PFE bad smooth: $smooth_N";
83  
84 return $class->SUPER::new
85 (parent => $parent,
86 parameters => [ $N, $smooth_N ],
87 arrays => { values => [] });
88 }
89 sub warmup_count {
90 my ($class_or_self, $N, $smooth_N) = @_;
91 return $N-1 + App::Chart::Series::Derived::EMA->warmup_count($N);
92 }
93  
94 # hypot(r,e)
95 # h = sqrt(r^2 + e^2)
96 # d = sqrt(r^2 + e^2) - r
97 # d+r = sqrt(r^2 + e^2)
98 # d^2 + 2dr + r^2 = r^2 + e^2
99 # d^2 + 2dr = e^2
100 # d^2 + 2dr - e^2 = 0
101 # d = (-2r +/- sqrt(4r^2 + 4e^2) ) / 2
102 # d = -r +/- sqrt(r^2 + e^2)
103  
104 sub proc {
105 my ($class_or_self, $N, $smooth_N) = @_;
106  
107 # input is how many days, decrement to get how many differences ... is
108 # that right?
109 $N--;
110  
111 my @values; # previous $N many values
112 my $sum_proc = App::Chart::Series::Calculation->sum($N);
113 my $ema_proc = App::Chart::Series::Derived::EMA->proc($smooth_N);
114  
115 return sub {
116 my ($value) = @_;
117 my $pfe;
118  
119 if (@values) {
120 my $prevN = $values[-1];
121 my $rocN = ($prevN == 0 ? 0 : 100 * ($value - $prevN) / $prevN);
122  
123 my $prev1 = $values[0];
124 my $roc1 = ($prev1 == 0 ? 0
125 : 100 * ($value - $prev1) / $prev1);
126 my $sum = $sum_proc->(_hypot($roc1,1));
127  
128 my $raw = ($sum == 0 ? 0
129 : 100 * _hypot($rocN,scalar @values) / $sum);
130 if ($rocN < 0) { $raw = -$raw; }
131 $pfe = $ema_proc->($raw);
132 }
133 unshift @values, $value;
134 if (@values > $N) { pop @values }
135  
136 return $pfe;
137 };
138 }
139  
140 # could use Math::Libm hypot() here for a touch more accuracy, but not sure
141 # how good its portability is on non-Unix systems
142 sub _hypot {
143 my ($x,$y) = @_;
144 return sqrt($x*$x + $y*$y);
145 }
146  
147 1;
148 __END__