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

filter: Statementstmt 0.0% 0 / 46 Branchbran n/a Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 72 CC 1 SCAR 6.9
CC
1
Cov
0%
longname
2.0
shortname
2.0
manual
2.0
CRAP 2.0
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::WMA;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use Locale::TextDomain 1.17; # for __p()
23 use Locale::TextDomain ('App-Chart');
24 use Math::Trig ();
25  
26 use base 'App::Chart::Series::Indicator';
27 use App::Chart::Series::Calculation;
28 use App::Chart::Series::Derived::SMA;
29  
30 # http://www.linnsoft.com/tour/techind/movAvg.htm
31 # Formula, and sample WMA N=20 on Nasdaq 100 (symbol QQQ, yahoo now in
32 # ^IXIC) from 2001.
33 #
34  
35 sub longname { __('WMA - Weighted MA') }
36 sub shortname { __('WMA') }
37 sub manual { __p('manual-node','Weighted Moving Average') }
38  
39 use constant
40 { priority => 11,
41 type => 'average',
42 parameter_info => [ { name => __('Days'),
43 key => 'wma_days',
44 type => 'integer',
45 minimum => 1,
46 default => 20 } ],
47 };
48  
49 sub new {
50 my ($class, $parent, $N) = @_;
51  
52 $N //= parameter_info()->[0]->{'default'};
53 ($N > 0) || croak "WMA bad N: $N";
54  
55 return $class->SUPER::new
56 (parent => $parent,
57 parameters => [ $N ],
58 arrays => { values => [] },
59 array_aliases => { });
60 }
61 *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1
62  
63 sub proc {
64 my ($class_or_self, $N) = @_;
65 return App::Chart::Series::Calculation::ma_proc_by_weights (reverse 1 .. $N);
66 }
67  
68 1;
69 __END__
70  
71 # =head1 NAME
72 #
73 # App::Chart::Series::Derived::WMA -- weighted moving average
74 #
75 # =head1 SYNOPSIS
76 #
77 # my $series = $parent->WMA($N);
78 #
79 # =head1 DESCRIPTION
80 #
81 # ...
82 #
83 # =head1 SEE ALSO
84 #
85 # L<App::Chart::Series>, L<App::Chart::Series::Derived::SMA>,
86 # L<App::Chart::Series::Derived::SineMA>
87 #
88 # =cut
89  
90  
91 # with accumulated sums ...
92 #
93 # (define*-public (wma-calc-proc count #:optional (data '()))
94 # (set! data (take-right-maybe data count))
95 #
96 # ;; SUM is plain sum of DATA
97 # ;; WSUM is weighted sum of DATA
98 # ;; POINTS is number of non-#f in DATA
99 # ;; DIV is divisor for weighting in DATA
100 # ;; DIV-MIN is minimum DIV for valid result
101 #
102 # (let* ((points (length data))
103 # (sum (apply + data))
104 # (wsum (dot-product data (iota points (1+ (- count points)))))
105 # (div (exact->inexact ;; avoid exact fraction in guile 1.8
106 # (- (triangular count) (triangular (- count points)))))
107 # (div-min (ceiling-exact (* indicator-minimum-fraction
108 # (triangular count)))))
109 # (set! data (indicator-circular count data))
110 #
111 # (lambda (x)
112 #
113 # ;; drop old point, including one less of each in DATA
114 # (set! wsum (- wsum sum))
115 # (if (car data)
116 # (set! sum (- sum (car data)))
117 # (begin
118 # (set! div (+ div (- count points)))
119 # (set! points (1+ points))))
120 #
121 # ;; hold this point
122 # (set-car! data x)
123 # (set! data (cdr data))
124 #
125 # ;; add this point
126 # (set! sum (+ sum x))
127 # (set! wsum (+ wsum (* x count)))
128 #
129 # (and (>= div div-min)
130 # (/ wsum div)))))