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

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 107 CC 6 SCAR 22.8
CC
6
Cov
0%
fill_part
30.0
minimum
6.0
longname
2.0
CRAP 9.8
1 # Copyright 2007, 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::COG;
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  
28 # http://www.mesasoftware.com/technicalpapers.htm
29 # http://www.mesasoftware.com/Papers/The%20CG%20Oscillator.pdf
30 # Paper by John Elhers.
31 #
32 # http://www.linnsoft.com/tour/techind/cog.htm
33 # Sample Intel (INTC) 2001/2.
34 #
35 # http://www.working-money.com/Documentation/FEEDbk_docs/Archive/082002/TradersTips/TradersTips.html
36 # Trader's tips August 2002, formulas from May 2002.
37 #
38  
39  
40 sub longname { __('Centre of Gravity') }
41 sub shortname { __('COG') }
42 sub manual { __p('manual-node','Centre of Gravity Oscillator') }
43  
44 use constant
45 { type => 'indicator',
46 maximum => -1,
47 parameter_info => [ { name => __('Days'),
48 key => 'cog_days',
49 type => 'integer',
50 minimum => 1,
51 default => 10 } ],
52 };
53 sub minimum {
54 my ($self) = @_;
55 return (ref $self ? - $self->{'N'} : undef);
56 }
57  
58  
59 sub new {
60 ### COG new()
61 my ($class, $parent, $N) = @_;
62 ($N > 0) || croak "COG bad N: $N";
63  
64 return $class->SUPER::new
65 (parent => $parent,
66 N => $N,
67 parameters => [ $N ],
68 arrays => { values => [] },
69 array_aliases => { });
70 }
71  
72 sub fill_part {
73 my ($self, $lo, $hi) = @_;
74 my $parent = $self->{'parent'};
75 my $N = $self->{'N'};
76  
77 my $start = $parent->find_before ($lo, $N-1);
78 $parent->fill ($start, $hi);
79 my $s = $self->values_array; # self
80 my $p = $parent->values_array; # parent
81  
82 $hi = min ($hi, $#$p);
83 if ($#$s < $hi) { $#$s = $hi; } # pre-extend
84  
85 # @a is current points accumulated, or undef in empty positions. a[$pos]
86 # is the oldest point.
87 #
88 # $den is the sum of the values in @a.
89 #
90 # $num is the sum a[$pos]*1 + a[$pos-1]*2 + ... + a[$pos-N-1]*N, with $pos
91 # wrapping around in the $N elements of @a
92 #
93 my @a;
94 $#a = $N-1; # pre-extend
95 my $num = 0;
96 my $den = 0;
97  
98 my $pos = 0;
99 foreach my $i ($start .. $lo-1) {
100 my $value = $p->[$i] // next;
101 # step existing and add new point
102 $num += $den + $value;
103 $den += $value;
104 $a[$pos++] = $value;
105 }
106 my $count = $pos;
107 ### COG warmed to: "num=$num, den=$den, count=$count"
108  
109 foreach my $i ($lo .. $hi) {
110 my $value = $p->[$i] // next;
111  
112 if ($count < $N) {
113 $count++;
114 } else {
115 # drop oldest point
116 $num -= $N * $a[$pos];
117 $den -= $a[$pos];
118 }
119  
120 # step existing num, and add new point
121 $num += $den + $value;
122 $den += $value;
123  
124 $a[$pos++] = $value;
125 if ($pos >= $N) { $pos = 0; }
126  
127 $s->[$i] = ($den == 0 ? -1 : - $num / $den);
128 }
129 }
130  
131 1;
132 __END__