GeniusTrader/Indicators/FRAMA.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 188 CC 14 SCAR 38.0
CC
14
Cov
0%
calculate
72.0
calculate_interval
56.0
initialize
2.0
CRAP 44.6
1 package Finance::GeniusTrader::Indicators::FRAMA;
2  
3 # Copyright 2008 Karsten Wippler
4 # Based on and for GeniusTrader (C) 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
5 # This file is distributed under the terms of the General Public License
6 # version 2 or (at your option) any later version.
7  
8 # $Id: FRAMA.pm,v 1.4 2008/03/14 17:07:14 ras Exp ras $
9  
10 # Standards-Version: 1.0
11  
12 use strict;
13 use vars qw(@ISA @NAMES @DEFAULT_ARGS);
14  
15 use Finance::GeniusTrader::Indicators;
16 use Finance::GeniusTrader::Indicators::SMA;
17 use Finance::GeniusTrader::Indicators::Generic::MinInPeriod;
18 use Finance::GeniusTrader::Indicators::Generic::MaxInPeriod;
19  
20 @ISA = qw(Finance::GeniusTrader::Indicators);
21 @NAMES = ("FRAMA[#*]","ALPHA[#1]");
22 @DEFAULT_ARGS = (16, "{I:MEAN}");
23  
24 =head1 NAME
25  
26 Finance::GeniusTrader::Indicators::FRAMA - FRactal Adaprive Moving Average
27  
28 =head1 DESCRIPTION
29  
30 An exponential moving average gives more importance to
31 recent prices ... similarly a Frama uses a variable
32 (adaptive) alpha
33  
34 =head2 Parameters
35  
36 =over
37  
38 =item Period (default 20)
39  
40 The first argument is the period used to calculed the average.
41  
42 =item Other data input
43  
44 The second argument is optional. It can be used to specify an other
45 stream of input data for the average instead of the close prices.
46 This is usually an indicator (detailed via {I:MyIndic <param>}).
47  
48 =back
49  
50 =head2 Calculation
51 The alpha is calculated following
52 http://www.mesasoftware.com/technicalpapers.htm
53 see the self explaining code below :D
54 when knowing the alpha FRAMA is
55 FRAMA[n] = FRAMA[n-1] + alpha * ( INPUT - FRAMA[n-1] )
56  
57 In TA, the first value is often constructed as SMA(N).
58  
59 Note: One criticism could be that the EMA is calculated starting
60 from the designated period. But actually the EMA goes all the way back
61 to the beginning of the available data. But in all tools I checked they
62 start computation from the loaded data on.
63  
64 =head2 Creation
65  
66 Finance::GeniusTrader::Indicators::FRAMA->new()
67 Finance::GeniusTrader::Indicators::FRAMA->new([20])
68  
69 If you need a 30 days FRAMA of the opening prices you can write
70 one of those lines :
71  
72 Finance::GeniusTrader::Indicators::FRAMA->new([30, "{I:Prices OPEN}"])
73  
74 A 10 days EMA of the RSI could be created with :
75  
76 Finance::GeniusTrader::Indicators::FRAMA->new([10, "{I:RSI}"])
77  
78 note!!! The number of days has to be EVEN!!
79  
80 Z<>
81  
82 =cut
83 sub initialize {
84 my ($self) = @_;
85 my $period = $self->{'args'}->get_arg_names(1);
86 $period=$period/2;
87 $self->{'hmin'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ $period, $self->{'args'}->get_arg_names(2) ]);
88 $self->{'hmax'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ $period, $self->{'args'}->get_arg_names(2) ]);
89 $self->{'min'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ $self->{'args'}->get_arg_names(1),
90 $self->{'args'}->get_arg_names(2) ]);
91 $self->{'max'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ $self->{'args'}->get_arg_names(1),
92 $self->{'args'}->get_arg_names(2) ]);
93  
94 $self->{'sma'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names() ]);
95 $self->add_indicator_dependency($self->{'sma'}, 1);
96 $self->add_indicator_dependency($self->{'hmin'}, $self->{'args'}->get_arg_names(1));
97 $self->add_indicator_dependency($self->{'hmax'}, $self->{'args'}->get_arg_names(1));
98 $self->add_indicator_dependency($self->{'min'}, $self->{'args'}->get_arg_names(1));
99 $self->add_indicator_dependency($self->{'max'}, $self->{'args'}->get_arg_names(1));
100 }
101  
102 sub calculate {
103 my ($self, $calc, $i) = @_;
104 my $indic = $calc->indicators;
105 my $name = $self->get_name(0);
106 my $alpha_name = $self->get_name(1);
107 my $hmin_name = $self->{'hmin'}->get_name;
108 my $hmax_name = $self->{'hmax'}->get_name;
109 my $min_name = $self->{'min'}->get_name;
110 my $max_name = $self->{'max'}->get_name;
111 my $before = 1;
112  
113 my $nb = $self->{'args'}->get_arg_values($calc, $i, 1);
114 return if (! defined($nb));
115 my $period=$nb/2;
116  
117 return if ($indic->is_available($name, $i));
118 return if ($before && ! $self->check_dependencies($calc, $i));
119  
120 my $alpha = 0.5;
121  
122 my $oldframa = $indic->get($name, $i - 1);
123 my $frama;
124 if (defined $oldframa) {
125 my $low1 = $indic->get($hmin_name, $i - $period);
126 my $high1 = $indic->get($hmax_name, $i - $period);
127 my $low2 = $indic->get($hmin_name, $i);
128 my $high2 = $indic->get($hmax_name, $i);
129 my $low3 = $indic->get($min_name, $i);
130 my $high3 = $indic->get($max_name, $i);
131 my $N1= ($high1-$low1)/$period;
132 my $N2= ($high2-$low2)/$period;
133 my $N3= ($high3-$low3)/$nb;
134 my $dim;
135 if ($N1>0 && $N2>0 && $N3>0){
136 $dim=(log($N1+$N2)-log($N3))/log(2);
137 }
138 $alpha=exp(-4.6*($dim-1));
139 if ($alpha <0.01){
140 $alpha=0.01;
141 }
142 if ($alpha >1){
143 $alpha=1;
144 }
145 $frama = $alpha * ($self->{'args'}->get_arg_values($calc, $i, 2) - $oldframa) + $oldframa;
146 } else {
147 $frama = $indic->get($self->{'sma'}->get_name, $i);
148 $before = 0;
149 }
150 $indic->set($alpha_name, $i, $alpha);
151 $indic->set($name, $i, $frama);
152  
153 }
154  
155 sub calculate_interval {
156 my ($self, $calc, $first, $last) = @_;
157 my $indic = $calc->indicators;
158 my $name = $self->get_name(0);
159 my $alpha_name = $self->get_name(1);
160 my $hmin_name = $self->{'hmin'}->get_name;
161 my $hmax_name = $self->{'hmax'}->get_name;
162 my $min_name = $self->{'min'}->get_name;
163 my $max_name = $self->{'max'}->get_name;
164  
165 my $nb = $self->{'args'}->get_arg_constant(1);
166 return if (! defined($nb));
167 return if ($indic->is_available_interval($name, $first, $last));
168 # Don't need to calculate all SMA values, just the first data point.
169 $self->{'sma'}->calculate($calc, $first);
170 while (! $self->check_dependencies_interval($calc, $first, $last)) {
171 return if $first == $last;
172 $first++;
173 }
174 my $period=$nb/2;
175 my $alpha = 0.5;
176  
177 $indic->set($name, $first, $indic->get($self->{'sma'}->get_name, $first));
178  
179 for (my $i=$first+1;$i<=$last;$i++) {
180 my $low1 = $indic->get($hmin_name, $i - $period);
181 my $high1 = $indic->get($hmax_name, $i - $period);
182 my $low2 = $indic->get($hmin_name, $i);
183 my $high2 = $indic->get($hmax_name, $i);
184 my $low3 = $indic->get($min_name, $i);
185 my $high3 = $indic->get($max_name, $i);
186 my $N1= ($high1-$low1)/$period;
187 my $N2= ($high2-$low2)/$period;
188 my $N3= ($high3-$low3)/$nb;
189 my $dim;
190 if ($N1>0 && $N2>0 && $N3>0){
191 $dim=(log($N1+$N2)-log($N3))/log(2);
192 }
193 $alpha=exp(-4.6*($dim-1));
194 if ($alpha <0.01){
195 $alpha=0.01;
196 }
197 if ($alpha >1){
198 $alpha=1;
199 }
200 my $oldframa = $indic->get($name, $i - 1);
201 my $frama = $alpha * ($self->{'args'}->get_arg_values($calc, $i, 2) - $oldframa) + $oldframa;
202 $indic->set($alpha_name, $i, $alpha);
203 $indic->set($name, $i, $frama);
204 }
205 }
206  
207 1;