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

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 159 CC 6 SCAR 19.3
CC
6
Cov
0%
proc
20.0
fill_part
12.0
longname
2.0
CRAP 6.9
1 # Copyright 2006, 2007, 2009, 2010 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::RWI;
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::Derived::MFI;
27 use App::Chart::Series::Derived::TrueRange;
28  
29  
30 # http://store.traders.com/-v10-c01-sideran-pdf.html
31 # Abstract of sidebar calculation, excess of channel height (scaled by
32 # one-day move) over square root.
33 # http://store.traders.com/-v11-c11-sideran-pdf.html
34 # Similar.
35 # "Are There Persistent Cycles", E. Michael Poulos, September 1992
36 # TASC.
37 #
38 # http://www.prophet.net/analyze/popglossary.jsp?studyid=RWI
39 # Sample of McDonalds, year not shown but looks like May 2001.
40 #
41 # http://www.paritech.com/paritech-site/education/technical/indicators/momentum/random.asp
42 # Description, no formulas, sample of DJS.AX, year not shown but is
43 # 1999 (RWI period not shown either).
44 #
45 # http://trader.online.pl/MSZ/e-w-Random_Walk_Index.html
46 # Formula.
47 #
48 # http://trader.online.pl/MSZ/e-w-Random_Walk_Index_II.html
49 # Long and short term high and low forms. Chart of Telecom Polska
50 # (TPSA.WA, London international order book TPSD.IL).
51 #
52 # http://www.linnsoft.com/tour/techind/ranWalk.htm
53 # Formula.
54 #
55 # http://www.equis.com/customer/resources/formulas/formula.aspx?Id=48
56 # Metastock formula, but only the RWI High part
57 #
58 # http://xeatrade.com/trading/2/R/1160.html
59 # Metastock formula.
60 #
61 # http://www.tradecision.com/support/indicators/random_walk_index.htm
62 # Tradedecsion formula for 9-day, cumulative sum of 1-day ATRs.
63 #
64 # http://technical.traders.com/tradersonline/display.asp?art=606
65 # Dennis Peterson on a random walk trading system. Sample Nasdaq
66 # composite (QQQ, yahoo now ^IXIC) from 1999/2000.
67 #
68 # http://www.sirtrade.com/serv03.htm
69 # Sample code of programming service by Pierre Orphelin, having
70 # struggled a bit with the correct definition. (Rejected incorrect
71 # code using the highest high in the past N days, maybe.)
72 #
73 # http://www.biz-analyst.com/library/stocks_commodities_trend.pdf
74 # Copies of some TASC articles, including "Futures According to Trend
75 # Tendency" V.10:1 by Michael Poulos, which includes a sidebar on his
76 # RWI. (Doesn't specify SMA or EMA for average.)
77 #
78 # http://ta-glossary.netfirms.com/taglossary-R.htm
79 # Description, sample Bradley Pharma (NYSE symbol BDY) from 2004.
80 #
81 # http://www.wallstreettape.com/tutorials/ta/c37.asp
82 # Chartfilter description.
83 #
84 #
85  
86 sub longname { __('RWI - Random Walk Index') }
87 sub shortname { __('RWI') }
88 sub manual { __p('manual-node','Random Walk Index') }
89  
90 use constant
91 { type => 'indicator',
92 units => 'rwi',
93 minimum => 0,
94 hlines => [ 1 ],
95 parameter_info => [ { name => __('Days'),
96 key => 'rwi_days',
97 type => 'integer',
98 minimum => 1,
99 default => 9 } ],
100 line_colours => { high => App::Chart::UP_COLOUR(),
101 low => App::Chart::DOWN_COLOUR() },
102 };
103  
104 sub new {
105 my ($class, $parent, $N) = @_;
106  
107 $N //= parameter_info()->[0]->{'default'};
108 ($N >= 1) || croak "RWI bad N: $N";
109  
110 return $class->SUPER::new
111 (parent => $parent,
112 parameters => [ $N ],
113 arrays => { high => [],
114 low => [] },
115 array_aliases => { values => 'high' });
116 }
117 *warmup_count = \&App::Chart::Series::Derived::MFI::warmup_count; # $N
118  
119 sub proc {
120 my ($class_or_self, $N) = @_;
121 my $tr_proc = App::Chart::Series::Derived::TrueRange->proc;
122 my (@H, @L, @TR, $prev_tr);
123  
124 return sub {
125 my ($high, $low, $close) = @_;
126 $high //= $close;
127 $low //= $close;
128  
129 my ($rwi_high, $rwi_low);
130 if (defined $prev_tr) {
131 my (@rwi_highs, @rwi_lows);
132 my $tr_sum = $prev_tr;
133  
134 foreach my $i (0 .. $#H) {
135 $tr_sum += $TR[$i];
136 if ($tr_sum == 0) { next; }
137 my $factor = sqrt($i+2);
138 push @rwi_highs, $factor * ($high - $L[$i]) / $tr_sum;
139 push @rwi_lows, $factor * ($H[$i] - $low) / $tr_sum;
140 }
141 $rwi_high = max(@rwi_highs);
142 $rwi_low = max(@rwi_lows);
143  
144 unshift @H, $high;
145 unshift @L, $low;
146 unshift @TR, $prev_tr;
147 if (@H > $N) {
148 pop @H;
149 pop @L;
150 pop @TR;
151 }
152 }
153 $prev_tr = $tr_proc->($high, $low, $close);
154  
155 return ($rwi_high, $rwi_low);
156 };
157 }
158  
159 # FIXME: share with DMI.pm
160 sub fill_part {
161 my ($self, $lo, $hi) = @_;
162 my $parent = $self->{'parent'};
163  
164 my $warmup_count = $self->warmup_count_for_position ($lo);
165 my $start = $parent->find_before ($lo, $warmup_count);
166 $parent->fill ($lo, $hi);
167 my $p = $parent->values_array;
168 my $ph = $parent->array('highs') || $p;
169 my $pl = $parent->array('lows') || $p;
170  
171 my $s_plus = $self->array('high');
172 my $s_minus = $self->array('low');
173 $hi = min ($hi, $#$p);
174 if ($#$s_plus < $hi) { $#$s_plus = $hi; } # pre-extend
175 if ($#$s_minus < $hi) { $#$s_minus = $hi; } # pre-extend
176  
177 my $proc = $self->proc(@{$self->{'parameters'}});
178  
179 foreach my $i ($start .. $lo-1) {
180 my $value = $p->[$i] // next;
181 $proc->($ph->[$i], $pl->[$i], $value);
182 }
183 foreach my $i ($lo .. $hi) {
184 my $value = $p->[$i] // next;
185 ($s_plus->[$i], $s_minus->[$i]) = $proc->($ph->[$i], $pl->[$i], $value);
186 }
187 }
188  
189 1;
190 __END__