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

filter: Statementstmt 0.0% 0 / 50 Branchbran n/a Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 77 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, 2011 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::TMA;
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 use constant DEBUG => 0;
31  
32 # http://www.linnsoft.com/tour/techind/movAvg.htm
33 # Formula, and sample TMA N=20 on Nasdaq 100 (symbol QQQ, yahoo now in
34 # ^IXIC) from 2001.
35 #
36  
37  
38  
39 sub longname { __('TMA - Triangular MA') }
40 sub shortname { __('TMA') }
41 sub manual { __p('manual-node','Triangular Moving Average') }
42  
43 use constant
44 { priority => -10,
45 type => 'average',
46 parameter_info => [ { name => __('Days'),
47 key => 'tma_days',
48 type => 'integer',
49 minimum => 1,
50 default => 20 } ],
51 };
52  
53 sub new {
54 my ($class, $parent, $N) = @_;
55 ### TMA new(): @_
56  
57 $N //= $class->parameter_info->[0]->{'default'};
58 ($N > 0) || croak "TMA bad N: $N";
59  
60 return $class->SUPER::new
61 (parent => $parent,
62 parameters => [ $N ],
63 arrays => { values => [] },
64 array_aliases => { });
65 }
66 *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1
67  
68 sub proc {
69 my ($self_or_class, $N) = @_;
70 # eg. N=5 gives 1, 2, 3, 2, 1
71 # eg. N=6 gives 1, 2, 3, 3, 2, 1
72 return App::Chart::Series::Calculation::ma_proc_by_weights
73 (1 .. int($N/2), (reverse 1 .. int(($N+1)/2)));
74 }
75  
76 1;
77 __END__
78  
79 # =head1 NAME
80 #
81 # App::Chart::Series::Derived::TMA -- triangular moving average
82 #
83 # =head1 SYNOPSIS
84 #
85 # my $series = $parent->TMA($N);
86 #
87 # =head1 DESCRIPTION
88 #
89 # ...
90 #
91 # =head1 SEE ALSO
92 #
93 # L<App::Chart::Series>, L<App::Chart::Series::Derived::SMA>,
94 # L<App::Chart::Series::Derived::SineMA>
95 #
96 # =cut
97  
98  
99  
100 # Old stuff done with a adjusting totals instead of vector product each time
101 # ...
102 #
103 # (define-public (tma-calc-proc count)
104 #
105 # ;; FACTORS is the weight factors, like '(1 2 3 2 1), stepped along to
106 # ;; build the divisor while the window isn't yet full
107 # ;;
108 # ;; DATA is a circular list of the accumulated window
109 # ;;
110 # ;; TOTAL is the sum of the DATA values with weighting factors applied
111 # ;;
112 # ;; STEP is how much to add to TOTAL to adjust for moving the DATA one
113 # ;; place along
114 # ;;
115 # ;; F-DATA and C-DATA are positions in DATA where the value stops adding
116 # ;; and starts subtracting, as it reaches its peak weighting then
117 # ;; starts decreasing
118 # ;;
119 # (let* ((c-count (quotient (1+ count) 2)) ;; ceil(N/2)
120 # (factors (tma-factors count))
121 # (div-min (ceiling-exact (* indicator-minimum-fraction
122 # (apply + factors))))
123 # (div 0)
124 # (total 0)
125 # (step 0)
126 # (data (make-circular-list count #f))
127 # (f-data (list-cdr-ref data c-count)) ;; measured from other end
128 # (c-data (if (odd? count)
129 # f-data
130 # (cdr f-data))))
131 #
132 # (lambda (x)
133 # ;; shift up past points, and add in new point
134 # (set! total (+ total step x))
135 # (set! step (+ step x))
136 #
137 # (if (car data)
138 # ;; window full, drop oldest point out of stepping
139 # (set! step (+ step (car data)))
140 #
141 # ;; window not yet full, increase divisor for new point
142 # (begin
143 # (set! div (+ div (first factors)))
144 # (set! factors (cdr factors))))
145 #
146 # ;; replace oldest point with new point
147 # (set-car! data x)
148 # (set! data (cdr data))
149 #
150 # ;; at floor(N/2) point stops adding
151 # (if (car f-data)
152 # (set! step (- step (car f-data))))
153 # (set! f-data (cdr f-data))
154 #
155 # ;; at ceil(N/2) point starts subtracting
156 # (if (car c-data)
157 # (set! step (- step (car c-data))))
158 # (set! c-data (cdr c-data))
159 #
160 # (and (>= div div-min)
161 # (exact->inexact (/ total div))))))