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

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 159 CC 6 SCAR 17.6
CC
6
Cov
0%
swing_index_calc
20.0
proc
6.0
fill_part
6.0
CRAP 5.8
1 # Copyright 2006, 2007, 2009 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::SwingIndex;
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::RSI;
27 use App::Chart::Series::Derived::TypicalPrice;
28  
29  
30 # http://www.ensignsoftware.com/espl/espl71.htm
31 # Code for swing index, ASI part confusing though.
32 #
33 # http://www.investopedia.com/articles/technical/02/100702.asp
34 # Sample chart of Apple (AAPL).
35 #
36  
37  
38 sub longname { __('Swing Index') }
39 sub shortname { __('Swing') }
40 sub manual { __p('manual-node','Accumulative Swing Index') }
41  
42 use constant
43 { priority => -10,
44 type => 'indicator',
45 units => 'swing_index',
46 hlines => [ 0 ],
47 parameter_info => [ ],
48 };
49  
50 sub new {
51 my ($class, $parent) = @_;
52  
53 return $class->SUPER::new
54 (parent => $parent,
55 parameters => [ ],
56 arrays => { values => [] },
57 array_aliases => { });
58 }
59 use constant warmup_count => 1;
60  
61 sub swing_index_calc {
62 my ($prev_open, $prev_close, $open, $high, $low, $close) = @_;
63  
64 $prev_open //= $prev_close;
65 $open //= $close;
66 $high //= $close;
67 $low //= $close;
68  
69 my $r1 = abs ($high - $prev_close);
70 my $r2 = abs ($low - $prev_close);
71 my $r3 = $high - $low;
72 my $r4 = abs ($prev_close - $prev_open);
73 my $k = max ($r1, $r2);
74 my $r;
75 if ($r1 >= max($r2,$r3)) {
76 $r = $r1 - 0.5*$r2 + 0.25*$r4;
77 } elsif ($r2 >= max($r1,$r3)) {
78 $r = $r2 - 0.5*$r1 + 0.25*$r4;
79 } else {
80 $r = $r3 + 0.25*$r4;
81 }
82 if ($r == 0) { return 0; }
83  
84 return ($k
85 * (($close - $prev_close)
86 + 0.5 * ($close - $open)
87 + 0.25 * ($prev_close - $prev_open))
88 / $r);
89 }
90  
91 sub proc {
92 my ($class_or_self) = @_;
93 my ($prev_open, $prev_close);
94  
95 return sub {
96 my ($open, $high, $low, $close) = @_;
97  
98 my $swing;
99 if (defined $prev_close) {
100 $swing = swing_index_calc ($prev_open, $prev_close,
101 $open, $high, $low, $close);
102 }
103 $prev_open = $open;
104 $prev_close = $close;
105 return $swing;
106 };
107 }
108  
109 sub fill_part {
110 my ($self, $lo, $hi) = @_;
111 my $parent = $self->{'parent'};
112  
113 my $warmup_count = $self->warmup_count_for_position ($lo);
114 my $start = $parent->find_before ($lo, $warmup_count);
115 $parent->fill ($lo, $hi);
116 my $p = $parent->values_array;
117 my $po = $parent->array('opens') || [];
118 my $ph = $parent->array('highs') || [];
119 my $pl = $parent->array('lows') || [];
120  
121 my $s = $self->values_array;
122 $hi = min ($hi, $#$p);
123 if ($#$s < $hi) { $#$s = $hi; } # pre-extend
124  
125 my $proc = $self->proc(@{$self->{'parameters'}});
126  
127 foreach my $i ($start .. $lo-1) {
128 my $close = $p->[$i] // next;
129 $proc->($po->[$i], $ph->[$i], $pl->[$i], $close);
130 }
131 foreach my $i ($lo .. $hi) {
132 my $close = $p->[$i] // next;
133 $s->[$i] = $proc->($po->[$i], $ph->[$i], $pl->[$i], $close);
134 }
135 }
136  
137  
138 1;
139 __END__