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

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 178 CC 17 SCAR 51.3
CC
17
Cov
0%
fill_part
306.0
longname
2.0
shortname
2.0
CRAP 168.7
1 # Copyright 2007, 2009, 2010, 2013 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::DarvasBoxes;
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://www.gerryco.com/tech/darvas.html ... gone, but at archive.org
31 # http://web.archive.org/web/20080212170344/http://www.gerryco.com/tech/darvas.html
32 # State diagram.
33 #
34 # http://www.linnsoft.com/tour/techind/darvas.htm
35 # Sample MSFT August 2002, using close-must-penetrate for breakouts.
36 #
37 # http://www.guppytraders.com/gup206.htm
38 #
39  
40  
41 # Darryl Guppy's modified Darvas (not implemented here):
42 #
43 # http://www.traders.com/Documentation/FEEDbk_docs/Archive/052005/Abstracts_new/Guppy/guppy.html
44 # TASC May 2005 intro, rest for sale.
45 #
46 # http://www.traders.com/Documentation/FEEDbk_docs/Archive/062005/TradersTips/TradersTips.html
47 # Trader's Tips June 2005.
48  
49  
50  
51 sub longname { __('Darvas Boxes') }
52 sub shortname { __('Darvas') }
53 sub manual { __p('manual-node','Darvas Boxes') }
54  
55 use constant
56 { type => 'average',
57 parameter_info => [ ],
58 default_linestyle => 'Stops',
59 };
60  
61 sub new {
62 my ($class, $parent) = @_;
63  
64 return $class->SUPER::new
65 (parent => $parent,
66 parameters => [ ],
67 arrays => { values => [] },
68 arrays => { upper => [],
69 lower => [] },
70 array_aliases => { values => 'upper' });
71 }
72  
73 sub fill_part {
74 my ($self, $lo, $hi) = @_;
75 my $parent = $self->{'parent'};
76  
77 # everything in one go
78 $lo = 0;
79 $hi = $parent->hi;
80  
81 $parent->fill ($lo, $hi);
82 my $p = $parent->values_array;
83 my $ph = $parent->array('highs') || $p;
84 my $pl = $parent->array('lows') || $p;
85  
86 my $s_upper = $self->array('upper');
87 my $s_lower = $self->array('lower');
88 $hi = min ($hi, $#$p);
89 if ($#$s_upper < $hi) { $#$s_upper = $hi; } # pre-extend
90 if ($#$s_lower < $hi) { $#$s_lower = $hi; } # pre-extend
91  
92 my $box_start; # date pos
93 my $box_high;
94 my $box_low;
95 my $state = 0;
96  
97 my $fill_box = sub {
98 my ($box_end) = @_;
99 foreach my $i ($box_start .. $box_end) {
100 $s_upper->[$i] = $box_high;
101 $s_lower->[$i] = $box_low;
102 }
103 };
104  
105 foreach my $i ($lo .. $hi) {
106 my $close = $p->[$i] // next;
107 my $high = $ph->[$i] // $close;
108 my $low = $pl->[$i] // $close;
109  
110 if ($state == 0) {
111 # initial
112 $box_high = $high;
113 $box_start = $i;
114 $state = 1;
115  
116 } elsif ($state == 1) {
117 if ($high > $box_high) {
118 # breakout, stay here
119 $box_high = $high;
120 $box_start = $i;
121 } else {
122 # held, go onwards
123 $state = 2;
124 }
125  
126 } elsif ($state == 2) {
127 if ($high > $box_high) {
128 # breakout, back to 1
129 $box_high = $high;
130 $box_start = $i;
131 $state = 1;
132 } else {
133 # held, go onwards
134 $box_low = $low;
135 $state = 3;
136 }
137  
138 } elsif ($state == 3) {
139 if ($high > $box_high) {
140 # broke high, back to 1
141 $box_high = $high;
142 $box_start = $i;
143 $state = 1;
144 } elsif ($low < $box_low) {
145 # break low, stay here
146 $box_low = $low;
147 } else {
148 # held, go onwards
149 $state = 4;
150 }
151  
152 } elsif ($state == 4) {
153 if ($high > $box_high) {
154 # broke high, back to 1
155 $box_high = $high;
156 $box_start = $i;
157 $state = 1;
158 } elsif ($low < $box_low) {
159 # break low, back to 3
160 $box_low = $low;
161 $state = 3;
162 } else {
163 # held, go onwards
164 $state = 5;
165 }
166  
167 } elsif ($state == 5) {
168 if ($high > $box_high
169 || $low < $box_low) {
170 # possible "close-must-penetrate" style
171 # ($close > $box_high || $close < $box_low)
172  
173 # break either way, stop box
174 $fill_box->($i);
175 $box_high = $high;
176 $box_start = $i;
177 $state = 1;
178 }
179 }
180 }
181  
182 if ($state == 5) {
183 $fill_box->($hi);
184 }
185 }
186  
187 1;
188 __END__