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

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 185 CC 11 SCAR 39.7
CC
11
Cov
0%
fill_part
132.0
longname
2.0
shortname
2.0
CRAP 53.1
1 # Copyright 2003, 2004, 2005, 2006, 2007, 2008, 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::OBV;
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  
27 use constant DEBUG => 0;
28  
29 sub longname { __('OBV - On-balance Volume') }
30 sub shortname { __('OBV') }
31 sub manual { __p('manual-node','On-Balance Volume') }
32  
33 use constant
34 { type => 'indicator',
35 units => 'OBV',
36 };
37  
38 sub new {
39 my ($class, $parent) = @_;
40  
41 return $class->SUPER::new
42 (parent => $parent,
43 arrays => { values => [] },
44 array_aliases => { });
45 }
46  
47 sub proc {
48 my ($self) = @_;
49 my $parent = $self->parent;
50 my $pv = $parent->array('volumes') || [];
51 sub {
52 my ($i, $value, $i_prev, $value_prev) = @_;
53 my $volume = $pv->[$i] // 0;
54 return ($value <=> $value_prev) * $volume;
55 };
56 }
57  
58 sub fill_part {
59 my ($self, $lo, $hi) = @_;
60 my $parent = $self->{'parent'};
61  
62 $parent->fill ($lo-1, $hi+1);
63 my $p = $parent->values_array;
64  
65 my $s = $self->values_array;
66 $hi = min ($hi, $#$p);
67 if ($#$s < $hi) { $#$s = $hi; } # pre-extend
68  
69 my $multiply = (($self->{'accumulate'}||'') eq 'multiply');
70 my $acc_forward = \&List::Util::sum;
71 my $acc_backward = \&_sub;
72 if ($multiply) {
73 $acc_forward = \&_mul;
74 $acc_backward = \&_div;
75 }
76  
77 my $lo_done = ($self->{'lo_done'} //= do {
78 my $i;
79 if (do { $i = $parent->find_after($lo-1,1);
80 defined $p->[$i] }
81 || do { $i = $parent->find_before($lo+1,1);
82 defined $p->[$i]
83 }) {
84 if (DEBUG) { print " start at $i with acc==0\n"; }
85 $s->[$i] = ($multiply ? 100 : 0);
86 $i;
87 } else {
88 if (DEBUG) { print " no values at all in parent\n"; }
89 $self->{'hi_done'} = $parent->hi;
90 0
91 }
92 });
93 my $hi_done = ($self->{'hi_done'} //= $lo_done);
94 my $proc = $self->proc (@{$self->{'parameters'}});
95  
96 # extend $hi_done to $hi
97 if ($hi > $hi_done) {
98 if (DEBUG) { print " extend forward from $hi_done to $hi\n"; }
99 my $i_prev = $parent->find_before ($hi_done+1, 1);
100 my $acc = $s->[$i_prev];
101 my $value_prev = $p->[$i_prev];
102  
103 for (my $i = $i_prev + 1; $i <= $hi; $i++) {
104 my $value = $p->[$i] // next;
105  
106 $s->[$i] = ($acc = $acc_forward->
107 ($acc, $proc->($i, $value, $i_prev, $value_prev)));
108 $value_prev = $value;
109 $i_prev = $i;
110 }
111 $self->{'hi_done'} = $hi;
112 }
113  
114 # extend $lo_done to $lo
115 if ($lo < $lo_done) {
116 if (DEBUG) { print " extend backward from $lo_done to $lo\n"; }
117 my $i = $parent->find_after ($lo_done-1, 1);
118 my $acc = $s->[$i];
119 my $value = $p->[$i];
120  
121 for (my $i_prev = $i - 1; $i_prev >= $lo; $i_prev--) {
122 my $value_prev = $p->[$i_prev] // next;
123  
124 $s->[$i_prev] = ($acc = $acc_backward->
125 ($acc, $proc->($i, $value, $i_prev, $value_prev)));
126 $i = $i_prev;
127 $value = $value_prev;
128 }
129 $self->{'lo_done'} = $hi;
130 }
131 }
132 sub _sub { $_[0] - $_[1] }
133 sub _mul { $_[0] * $_[1] }
134 sub _div { $_[0] / ($_[1]||1) }
135  
136 1;
137 __END__