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

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 147 CC 7 SCAR 25.5
CC
7
Cov
0%
fill_part
42.0
new
6.0
longname
2.0
CRAP 12.8
1 # Copyright 2008, 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::VWMA;
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 use Math::Trig ();
25  
26 use base 'App::Chart::Series::Indicator';
27  
28 use constant DEBUG => 0;
29  
30 sub longname { __('VWMA - Volume Weighted') }
31 sub shortname { __('VWMA') }
32 sub manual { __p('manual-node','Volume Weighted Moving Average') }
33  
34 use constant
35 { type => 'average',
36 parameter_info => [ { name => __('Days'),
37 key => 'vwma_days',
38 type => 'integer',
39 minimum => 1,
40 default => 20 } ],
41 };
42  
43 sub new {
44 if (DEBUG >= 2) { require Data::Dumper;
45 print "VWMA->new ",Data::Dumper::Dumper(\@_); }
46 my ($class, $parent, $N) = @_;
47  
48 $parent->array('volumes')
49 or croak "No volumes in series '",$parent->name,"'";
50  
51 $N //= parameter_info()->[0]->{'default'};
52 ($N > 0) || croak "VWMA bad N: $N";
53  
54 return $class->SUPER::new
55 (parent => $parent,
56 N => $N,
57 parameters => [ $N ],
58 arrays => { values => [] },
59 array_aliases => { });
60 }
61  
62 sub fill_part {
63 my ($self, $lo, $hi) = @_;
64 my $parent = $self->{'parent'};
65 my $N = $self->{'N'};
66  
67 my $start = $parent->find_before ($lo, $N-1);
68 $parent->fill ($start, $hi);
69 my $s = $self->values_array;
70 my $pp = $parent->values_array;
71 my $pv = $parent->array('volumes');
72  
73 $hi = min ($hi, $#$pp);
74 if ($#$s < $hi) { $#$s = $hi; } # pre-extend
75  
76 # $a[0] is the newest point, $a[1] the prev, through to $a[$N-1]
77 my @a;
78 my @v;
79  
80 my $total_volume = 0;
81 my $total_prod = 0;
82 my $pos = 0;
83 foreach my $i ($start .. $lo-1) {
84 my $value = $pp->[$i] // next;
85 my $volume = $pv->[$i] // 0;
86 $total_volume += ($v[$pos] = $volume);
87 $total_prod += ($a[$pos++] = $value * $volume);
88 }
89 my $count = $pos;
90 if (DEBUG) { print " warmed to total_volume=$total_volume count=$count\n";
91 require Data::Dumper;
92 print Data::Dumper->Dump([\@v,\@a],['v','a']); }
93  
94 foreach my $i ($lo .. $hi) {
95 my $value = $pp->[$i] // next;
96 my $volume = $pv->[$i] // 0;
97  
98 if ($count < $N) {
99 $count++;
100 } else {
101 # drop old
102 $total_volume -= $v[$pos];
103 $total_prod -= $a[$pos];
104 }
105 # add new
106 $total_volume += ($v[$pos] = $volume);
107 $total_prod += ($a[$pos++] = $value * $volume);
108 if ($pos >= $N) { $pos = 0; }
109  
110 if ($total_volume != 0) {
111 $s->[$i] = $total_prod / $total_volume;
112 }
113 }
114 }
115  
116 1;
117 __END__