GeniusTrader/Indicators/STO.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 179 CC 12 SCAR 32.3
CC
12
Cov
0%
calculate
42.0
calculate_interval
42.0
initialize
6.0
CRAP 25.2
1 package Finance::GeniusTrader::Indicators::STO;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
4 # Copyright 2008 by Karsten Wipple and Thomas Weigert
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 # based on version dated 24 Apr 2005, 7914 bytes
9 # significant improvements Karsten Wipple and Thomas Weigert
10 # $Id$
11  
12 # Standards-Version: 1.0
13  
14 use strict;
15 use vars qw(@ISA @NAMES @DEFAULT_ARGS);
16  
17 use Finance::GeniusTrader::Indicators;
18 use Finance::GeniusTrader::Indicators::SMA;
19 use Finance::GeniusTrader::Indicators::Generic::MinInPeriod;
20 use Finance::GeniusTrader::Indicators::Generic::MaxInPeriod;
21 use Finance::GeniusTrader::Prices;
22  
23 @ISA = qw(Finance::GeniusTrader::Indicators);
24 @NAMES = ("\%K Fast[#1]","\%D Fast[#1,#2]","\%K Slow[#1,#3]","\%D Slow[#1,#3,#4]");
25 @DEFAULT_ARGS = (5, 3, 3, 3, "{I:Prices HIGH}", "{I:Prices LOW}", "{I:Prices CLOSE}" );
26  
27 =pod
28  
29 =head1 Finance::GeniusTrader::Indicators::STO
30  
31 =head2 Overview
32  
33 Developed by George C. Lane in the late 1950s, the Stochastic Oscillator
34 is a momentum indicator that shows the location of the current close
35 relative to the high/low range over a set number of periods. Closing
36 levels that are consistently near the top of the range indicate
37 accumulation (buying pressure) and those near the bottom of the range
38 indicate distribution (selling pressure).
39  
40 =head2 Calculation
41  
42 %K Fast = 100 * ((Last - Lowest Low(n)) / (Highest High(n) - Lowest Low(n)))
43 %D Fast = M-periods SMA of %K Fast
44  
45 %K Slow = A-periods SMA of %K Fast
46 %D Slow = B-periods SMA of %K Slow
47  
48 =head2 possibly helpful information:
49  
50 %K Fast corresponds to STO/1, %D Fast to STO/2, %K Slow to STO/3
51 and %D Slow to STO/4
52  
53 %K Slow may also be known as the stochastic oscillator
54 %D Slow is also known as the signal line
55
56 =head2 arguments and defaults for STO
57  
58 STO accepts 7 arguments, the defaults are, in order:
59 5, 3, 3, 3, {I:Prices HIGH}, {I:Prices LOW}, {I:Prices CLOSE}
60  
61 The first argument is the Period n used in the formula above and for
62 each of the subsequent SMA periods.
63  
64 The second argument is M-periods, the third is A-periods, fourth is B-periods.
65  
66
67 By default the data used is price, which can be changed by specifying
68 different indicators. the order is High(n), Low(n), Last in the %K Fast
69 formula above.
70  
71 Note that Metastock calculates the slowing via a sum of the last M-periods,
72 rather than a SMA. Metastock displays %K Slow and %D Slow.
73  
74 =head2 Examples
75  
76 Finance::GeniusTrader::Indicators::STO->new()
77 Finance::GeniusTrader::Indicators::STO->new([14, 3, 3, 3])
78  
79 =head2 Links
80  
81 http://www.stockcharts.com/education/What/IndicatorAnalysis/indic_stochasticOscillator.html
82 http://www.equis.com/free/taaz/stochasticosc.html
83  
84 =cut
85  
86 sub initialize {
87 my $self = shift;
88
89 # We need to call MIN and MAX first
90 $self->{'min'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ $self->{'args'}->get_arg_names(1),
91 $self->{'args'}->get_arg_names(6) ]);
92 $self->{'max'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ $self->{'args'}->get_arg_names(1),
93 $self->{'args'}->get_arg_names(5) ]);
94  
95 # Initialize %D Fast
96 $self->{'%d_fast'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names(2),
97 "{I:Generic:ByName " . $self->get_name(0) . "}" ]);
98  
99 # Initialize %K Slow
100 $self->{'%k_slow'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names(3),
101 "{I:Generic:ByName " . $self->get_name(0) . "}" ]);
102  
103 # Initialize %D Slow
104 $self->{'%d_slow'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names(4),
105 "{I:Generic:ByName " . $self->{'%k_slow'}->get_name(0) . "}" ]);
106  
107 # Now add dependencies
108 my $nb_days = ($self->{'args'}->get_arg_constant(2) > $self->{'args'}->get_arg_constant(3)+$self->{'args'}->get_arg_constant(4)) ?
109 $self->{'args'}->get_arg_constant(2) : $self->{'args'}->get_arg_constant(3)+$self->{'args'}->get_arg_constant(4);
110 $self->add_indicator_dependency($self->{'min'}, $nb_days);
111 $self->add_indicator_dependency($self->{'max'}, $nb_days);
112 $self->add_arg_dependency(7, $nb_days + $self->{'args'}->get_arg_constant(1) - 1);
113  
114 }
115  
116 =pod
117  
118 =head2 Finance::GeniusTrader::Indicators::STO::calculate($calc, $day)
119  
120 =cut
121  
122 sub calculate {
123 my ($self, $calc, $i) = @_;
124 my $indic = $calc->indicators;
125 my $prices = $calc->prices;
126 my $min_name = $self->{'min'}->get_name;
127 my $max_name = $self->{'max'}->get_name;
128 my $k_fast_name = $self->get_name(0);
129 my $d_fast_name = $self->get_name(1);
130 my $k_slow_name = $self->get_name(2);
131 my $d_slow_name = $self->get_name(3);
132
133 return if ($indic->is_available($k_fast_name, $i) &&
134 $indic->is_available($d_fast_name, $i) &&
135 $indic->is_available($k_slow_name, $i) &&
136 $indic->is_available($d_slow_name, $i));
137  
138 my $nb_days = ($self->{'args'}->get_arg_values($calc, $i, 2) > $self->{'args'}->get_arg_values($calc, $i, 3)+$self->{'args'}->get_arg_values($calc, $i, 4)) ?
139 $self->{'args'}->get_arg_values($calc, $i, 2) : $self->{'args'}->get_arg_values($calc, $i, 3)+$self->{'args'}->get_arg_values($calc, $i, 4);
140  
141 return if (! $self->check_dependencies($calc, $i));
142  
143 # Calculate %K Fast
144 for (my $n = 0; $n < $nb_days; $n++) {
145  
146 # Return if %K Fast is available
147 next if $indic->is_available($k_fast_name, $i - $n);
148
149 # Get MIN and MAX
150 my $lowest_low = $indic->get($min_name, $i - $n);
151 my $highest_high = $indic->get($max_name, $i - $n);
152
153 # Calculate %K Fast
154 $highest_high += 0.000001 if ($highest_high == $lowest_low);
155 my $k_fast_value = 100 * (($self->{'args'}->get_arg_values($calc, $i-$n, 7) - $lowest_low) / ($highest_high - $lowest_low));
156  
157 # Return the results
158 $indic->set($k_fast_name, $i - $n, $k_fast_value);
159 }
160  
161 # Calculate %D Fast
162 $self->{'%d_fast'}->calculate($calc, $i);
163  
164 # Calculate %K Slow
165 $self->{'%k_slow'}->calculate_interval($calc, $i - $self->{'args'}->get_arg_values($calc, $i, 4) + 1, $i);
166
167 # Calculate %D Slow
168 $self->{'%d_slow'}->calculate($calc, $i);
169  
170 # Get all values
171 my $d_fast_value = $indic->get($self->{'%d_fast'}->get_name, $i);
172 my $k_slow_value = $indic->get($self->{'%k_slow'}->get_name, $i);
173 my $d_slow_value = $indic->get($self->{'%d_slow'}->get_name, $i);
174  
175 # Return the results
176 $indic->set($d_fast_name, $i, $d_fast_value);
177 $indic->set($k_slow_name, $i, $k_slow_value);
178 $indic->set($d_slow_name, $i, $d_slow_value);
179 }
180  
181 sub calculate_interval {
182 my ($self, $calc, $first, $last) = @_;
183 my $indic = $calc->indicators;
184 my $prices = $calc->prices;
185 my $min_name = $self->{'min'}->get_name;
186 my $max_name = $self->{'max'}->get_name;
187 my $k_fast_name = $self->get_name(0);
188 my $d_fast_name = $self->get_name(1);
189 my $k_slow_name = $self->get_name(2);
190 my $d_slow_name = $self->get_name(3);
191  
192 return if ($indic->is_available_interval($k_fast_name, $first, $last) &&
193 $indic->is_available_interval($d_fast_name, $first, $last) &&
194 $indic->is_available_interval($k_slow_name, $first, $last) &&
195 $indic->is_available_interval($d_slow_name, $first, $last));
196  
197 my $nb_days = ($self->{'args'}->get_arg_constant(2) > $self->{'args'}->get_arg_constant(3)+$self->{'args'}->get_arg_constant(4)) ?
198 $self->{'args'}->get_arg_constant(2) : $self->{'args'}->get_arg_constant(3)+$self->{'args'}->get_arg_constant(4);
199  
200 while (! $self->check_dependencies_interval($calc, $first, $last)) {
201 return if $first == $last;
202 $first++;
203 }
204  
205  
206 for (my $i=$first;$i<=$last;$i++) {
207 # Calculate %K Fast
208 for (my $n = 0; $n < $nb_days; $n++) {
209  
210 # Return if %K Fast is available
211 next if $indic->is_available($k_fast_name, $i - $n);
212  
213 # Get MIN and MAX
214 my $lowest_low = $indic->get($min_name, $i - $n);
215 my $highest_high = $indic->get($max_name, $i - $n);
216
217 # Calculate %K Fast
218 $highest_high += 0.000001 if ($highest_high == $lowest_low);
219 my $k_fast_value = 100 * (($self->{'args'}->get_arg_values($calc, $i-$n, 7) - $lowest_low) / ($highest_high - $lowest_low));
220  
221 # Return the results
222 $indic->set($k_fast_name, $i - $n, $k_fast_value);
223 }
224 }
225  
226 # Calculate %D Fast
227 $self->{'%d_fast'}->calculate_interval($calc, $first, $last);
228  
229 # Calculate %K Slow
230 $self->{'%k_slow'}->calculate_interval($calc, $first - $self->{'args'}->get_arg_constant(4) + 1, $last);
231
232 # Calculate %D Slow
233 $self->{'%d_slow'}->calculate_interval($calc, $first, $last);
234  
235 for (my $i=$first;$i<=$last;$i++) {
236 # Get all values
237 my $d_fast_value = $indic->get($self->{'%d_fast'}->get_name, $i);
238 my $k_slow_value = $indic->get($self->{'%k_slow'}->get_name, $i);
239 my $d_slow_value = $indic->get($self->{'%d_slow'}->get_name, $i);
240  
241 # Return the results
242 $indic->set($d_fast_name, $i, $d_fast_value);
243 $indic->set($k_slow_name, $i, $k_slow_value);
244 $indic->set($d_slow_name, $i, $d_slow_value);
245 }
246 }
247  
248 1;