GeniusTrader/Indicators/SAR.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 132 CC 14 SCAR 49.5
CC
14
Cov
0%
calculate
210.0
initialize
2.0
BEGIN
2.0
CRAP 140.7
1 package Finance::GeniusTrader::Indicators::SAR;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
4 # This file is distributed under the terms of the General Public License
5 # version 2 or (at your option) any later version.
6  
7 # Standards-Version: 1.0
8  
9 use strict;
10 use vars qw(@ISA @NAMES @DEFAULT_ARGS);
11  
12 use Finance::GeniusTrader::Indicators;
13 use Finance::GeniusTrader::Indicators::Generic::MinInPeriod;
14 use Finance::GeniusTrader::Indicators::Generic::MaxInPeriod;
15 use Finance::GeniusTrader::Prices;
16  
17 @ISA = qw(Finance::GeniusTrader::Indicators);
18 @NAMES = ("SAR[#*]");
19 @DEFAULT_ARGS = (0.02, 0.02, 0.2, "{I:Prices HIGH}", "{I:Prices LOW}", "{I:Prices CLOSE}");
20  
21 =head1 Finance::GeniusTrader::Indicators::SAR
22  
23 =head2 Overview
24  
25 The Parabolic SAR, developed by Welles Wilder, is used to set trailing
26 price stops. SAR refers to "Stop-And-Reversal". It is designed to create
27 exit points for both long and short positions in such a way that it allows
28 for reactions or fluctuations at the beginning of the position, but
29 accelerates upward (for long positions) or downward (for short positions)
30 as the movement tops out.
31  
32 =head2 Calculation
33  
34 If Long :
35 SAR(i) = SAR(i-1) + Acceleration Factor *
36 (Extreme Point of the current position - SAR(i-1))
37  
38 Wilder's acceleration factor (AF) is 0.02 for the initial calculation.
39 Thereafter the AF is increased 0.02 every period there is a New High made.
40 If a new high is not made then the AF is not increased from the last SAR.
41 This continues until the AF reaches 0.2. Once the AF reaches 0.2 it
42 stays at that value for all future SAR calculations until the trade is
43 stopped out.
44  
45  
46 If Short :
47 SAR(i) = SAR(i-1) - Acceleration Factor *
48 (Extreme Point of the current position - SAR(i-1))
49  
50 The AF is initially 0.02 and changes by 0.02 intervals until it is 0.2 but
51 the change in the AF is made only after each New Low of a period is made.
52 The AF is never increased above 0.2.
53  
54 =head2 Parameters
55  
56 Most softwar packages only allow the user to vary the acceleration factor
57 increment and the acceleration factor maximum, fixing the starting
58 acceleration factor at 0.02. This restriction hampers the trend-following
59 abilities of the parabolic, so don't be surprised if GeniusTrader is going
60 a little step further and let you set up your own initial acceleration
61 factor.
62  
63 =head2 Links
64  
65 http://www.stockcharts.com/education/Resources/Glossary/parabolicSAR.html
66 http://www.equis.com/free/taaz/parabolicsar.html
67 http://www.linnsoft.com/tour/techind/sar.htm
68  
69 =cut
70  
71 sub initialize {
72 my $self = shift;
73  
74 $self->{'min'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ 3, $self->{'args'}->get_arg_names(6) ]);
75 $self->{'max'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ 3, $self->{'args'}->get_arg_names(6) ]);
76
77 $self->add_indicator_dependency($self->{'min'}, 1);
78 $self->add_indicator_dependency($self->{'max'}, 1);
79 $self->add_arg_dependency(4, 5);
80 $self->add_arg_dependency(5, 5);
81 $self->add_arg_dependency(6, 5);
82 }
83  
84 =head2 Finance::GeniusTrader::Indicators::SAR::calculate($calc, $day)
85  
86 =cut
87  
88 sub calculate {
89 my ($self, $calc, $i) = @_;
90 my $prices = $calc->prices;
91 my $indic = $calc->indicators;
92 my $sar_name = $self->get_name;
93 my $initial_acceleration_factor = $self->{'args'}->get_arg_values($calc, $i, 1);
94 my $incremental_acceleration_factor = $self->{'args'}->get_arg_values($calc, $i, 2);
95 my $maximum_acceleration_factor = $self->{'args'}->get_arg_values($calc, $i, 3);
96 my $min_name = $self->{'min'}->get_name;
97 my $max_name = $self->{'max'}->get_name;
98 my $acceleration_factor = $initial_acceleration_factor;
99 my $position = 0;
100 my $new_position = 0;
101 my $extreme_price_period;
102 my $previous_sar_value = 0;
103 my $sar_value = 0;
104 my ($min, $max);
105  
106 # Return if results are already available or dependencies missing
107 return if ($indic->is_available($sar_name, $i));
108 return if (! $self->check_dependencies($calc, $i));
109
110 # Let us start with a long position based on the observation that
111 # this high extended beyond the two previous highs during a minor
112 # trading range.
113  
114 if (($self->{'args'}->get_arg_values($calc, $i, 4) > $self->{'args'}->get_arg_values($calc, $i-1, 4)) and
115 ($self->{'args'}->get_arg_values($calc, $i, 4) > $self->{'args'}->get_arg_values($calc, $i-1, 4))) {
116 $position = 1;
117 }
118  
119 # Initialize $min and $max
120 $min = $indic->get($min_name, $i);
121 $max = $indic->get($max_name, $i);
122
123 # The first SAR value is calculated by taking the difference between
124 # the high price and the low price and multiplying the difference by
125 # the initial acceleration factor.
126
127 if ($position == 1) {
128 $previous_sar_value = $min + $acceleration_factor * ($max - $min);
129 $extreme_price_period = $min;
130 } else {
131 $previous_sar_value = $max - $acceleration_factor * ($max - $min);
132 $extreme_price_period = $max;
133 }
134  
135 for (my $j = $i; $j < $prices->count(); $j++) {
136  
137 # Stay aware of each SAR reversal !
138 if ($position == 1) {
139 if ($self->{'args'}->get_arg_values($calc, $j, 6) <= $previous_sar_value) {
140  
141 # Update $position and $new_position values
142 $new_position = 1;
143 $position = 0;
144  
145 # Initialize the first SAR value with the extreme price
146 # of the last period
147 $sar_value = $extreme_price_period;
148
149 } else {
150 $new_position = 0;
151 }
152 } else {
153 if ($self->{'args'}->get_arg_values($calc, $j, 6) >= $previous_sar_value) {
154  
155 # Update $position and $new_position values
156 $new_position = 1;
157 $position = 1;
158  
159 # Initialize the first SAR value with the extreme price
160 # of the last period
161 $sar_value = $extreme_price_period;
162
163 } else {
164 $new_position = 0;
165 }
166 }
167  
168 if ($new_position == 1) {
169  
170 # Initialize the acceleration factor to it's initial value
171 # each time we switch from a long to a short position or vice
172 # versa.
173
174 $acceleration_factor = $initial_acceleration_factor;
175
176 } else {
177  
178 # Increment the acceleration factor each time a new high or a new
179 # low appears. Update extreme price period value.
180
181 if ($position == 1) {
182 if ($self->{'args'}->get_arg_values($calc, $j, 4) > $extreme_price_period ) {
183 $acceleration_factor += $incremental_acceleration_factor;
184 $extreme_price_period = $self->{'args'}->get_arg_values($calc, $j, 4);
185 }
186 } else {
187 if ($self->{'args'}->get_arg_values($calc, $j, 5) < $extreme_price_period ) {
188 $acceleration_factor += $incremental_acceleration_factor;
189 $extreme_price_period = $self->{'args'}->get_arg_values($calc, $j, 5);
190 }
191 }
192  
193 # Be sure the acceleration remains below the maximum level
194 if ($acceleration_factor > $maximum_acceleration_factor) {
195 $acceleration_factor = $maximum_acceleration_factor;
196 }
197  
198 # Calculate SAR value if it's not the first value of a new trend
199 if ($new_position == 0) {
200 $sar_value = $previous_sar_value + $acceleration_factor *
201 ($extreme_price_period - $previous_sar_value);
202 }
203 }
204  
205 # Update $previous_sar_value with $sar_value
206 $previous_sar_value = $sar_value;
207
208 # Store every new SAR value
209 $calc->indicators->set($sar_name, $j, $sar_value);
210 }
211 }
212  
213 1;