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

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 152 CC 5 SCAR 21.2
CC
5
Cov
0%
warmup_count_for_position
30.0
longname
2.0
shortname
2.0
CRAP 8.4
1 # Copyright 2006, 2007, 2009, 2010, 2011 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::FRAMA;
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::Calculation;
27 use App::Chart::Series::Derived::KAMA;
28 use App::Chart::Series::Derived::FRAMAalpha;
29  
30  
31 # http://www.mesasoftware.com/technicalpapers.htm
32 # http://www.mesasoftware.com/Papers/FRAMA.pdf
33 # John Ehler's paper on FRAMA.
34 #
35 # http://www.traders.com/Documentation/FEEDbk_docs/Archive/102005/TradersTips/TradersTips.html
36 # Traders Tips October 2005.
37 #
38 # http://trader.online.pl/MSZ/e-w-Moving_Average_Fractal_Adaptive_(FAMA).html
39 #
40 # http://www.tradingsolutions.com/download/tip0510.zip
41 # Formulas.
42 #
43  
44 sub longname { __('FRAMA - Fractal Adaptive MA') }
45 sub shortname { __('FRAMA') }
46 sub manual { __p('manual-node','Fractal Adaptive Moving Average') }
47  
48 use constant
49 { type => 'average',
50 parameter_info => App::Chart::Series::Derived::FRAMAalpha::parameter_info(),
51 };
52  
53 sub new {
54 my ($class, $parent, $N) = @_;
55  
56 $N //= parameter_info()->[0]->{'default'};
57  
58 return $class->SUPER::new
59 (parent => $parent,
60 parameters => [ $N ],
61 arrays => { values => [] },
62 array_aliases => { });
63 }
64  
65 sub warmup_count {
66 my ($self_or_class, $N) = @_;
67 # extreme worst case guess, ends up about $N+687
68 return $N + App::Chart::Series::Derived::EMA->warmup_count
69 (App::Chart::Series::Derived::EMA::alpha_to_N(0.01));
70 }
71 ### FRAMA warmup_count(): __PACKAGE__->warmup_count(0)
72  
73 sub warmup_count_for_position {
74 my ($self, $lo) = @_;
75 my $parent = $self->{'parent'};
76 my $p = $parent->values_array;
77 my $ph = $parent->array('highs') || $p;
78 my $pl = $parent->array('lows') || $p;
79  
80 my $p_lo = $lo + 1; # initial fill
81 my $chunk = 16;
82  
83 my $alpha_proc = App::Chart::Series::Derived::FRAMAalpha->proc(@{$self->{'parameters'}});
84 my $alpha_warmup = App::Chart::Series::Derived::FRAMAalpha->warmup_count(@{$self->{'parameters'}});
85 my $tail = 1;
86  
87 my $i = $lo;
88 for ( ; $i >= 0; $i--) {
89 if ($i < $p_lo) {
90 my $new_p_lo = min ($i, $p_lo - $chunk);
91 $chunk *= 2;
92 $parent->fill ($new_p_lo, $i);
93 $p_lo = $new_p_lo;
94 }
95 my $value = $p->[$i] // next;
96 $alpha_proc->($ph->[$i], $pl->[$i], $value) // next;
97 last if --$alpha_warmup <= 0;
98 }
99 for ( ; $i >= 0; $i--) {
100 if ($i < $p_lo) {
101 my $new_p_lo = min ($i, $p_lo - $chunk);
102 $chunk *= 2;
103 $parent->fill ($new_p_lo, $i);
104 $p_lo = $new_p_lo;
105 }
106 my $value = $p->[$i] // next;
107 my $alpha = $alpha_proc->($ph->[$i], $pl->[$i], $value) // next;
108 $tail *= (1 - $alpha);
109 if ($tail <= App::Chart::Series::Derived::EMA::WARMUP_OMITTED_FRACTION) {
110 last;
111 }
112 }
113 ### FRAMA warmup_count_for_position(): "for $lo is ".($lo-$i)
114 return $lo - $i;
115 }
116  
117 sub proc {
118 my ($class, $N) = @_;
119 my $alpha_proc = App::Chart::Series::Derived::FRAMAalpha->proc($N);
120 my $ama_proc = App::Chart::Series::Derived::KAMA->adaptive_ema_proc();
121  
122 return sub {
123 my ($high, $low, $close) = @_;
124 my $alpha = $alpha_proc->(@_) // return undef;
125  
126 # ENHANCE-ME: pending configurable selection for this sort of thing ...
127 # operate on John Ehlers preferred midpoint ...
128 # my $value = (($high // $close) + ($low // $close)) / 2;
129  
130 return $ama_proc->($alpha, $close);
131 };
132 }
133 *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part; # HLC
134  
135 1;
136 __END__