File Coverage

blib/lib/App/Chart/Series/Derived/GuppyMMA.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 27 27 100.0


line stmt bran cond sub pod time code
1             # Copyright 2007, 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::GuppyMMA;
18 1     1   31818 use 5.010;
  1         3  
19 1     1   4 use strict;
  1         6  
  1         19  
20 1     1   5 use warnings;
  1         2  
  1         23  
21 1     1   5 use Carp;
  1         1  
  1         47  
22 1     1   5 use List::Util qw(min max);
  1         2  
  1         60  
23 1     1   6 use Locale::TextDomain ('App-Chart');
  1         2  
  1         7  
24              
25 1     1   172 use base 'App::Chart::Series::Indicator';
  1         2  
  1         314  
26             use App::Chart::Series::Derived::EMA;
27              
28             # http://www.guppytraders.com/gup329.shtml
29             # Summary.
30             #
31             # http://www.chartfilter.com/articles/technicalanalysis/movingaverage.htm
32             # 2004 article by Darryl Guppy.
33             #
34             # http://www.incrediblecharts.com/indicators/multiple_moving_averages.php
35             # Sample of ASX.AX from 1999.
36             #
37              
38             sub longname { __('Guppy Multiple MA') }
39             sub shortname { __('GMMA') }
40             sub manual { __p('manual-node','Guppy Multiple Moving Average') }
41              
42             use constant
43             { type => 'average',
44             parameter_info =>
45             [ map {; 0 || { name => ($_ == 3 ? __('Days')
46             : $_ == 20 ? __p('long-moving-average','Long')
47             : ''),
48             key => "guppy_mma_days_$_",
49             type => 'integer',
50             min => 0,
51             default => $_ } }
52             3, 5, 8, 10, 12, 15,
53             30, 35, 40, 45, 50, 60, ],
54             };
55              
56             sub new {
57             my ($class, $parent, @parameters) = @_;
58             if (@parameters) {
59             @parameters = grep {defined && $_ != 0} @parameters;
60             } else {
61             @parameters = map {$_->{'default'}} @{$class->parameters};
62             }
63             foreach my $N (@parameters) {
64             ($N > 0) or croak "Guppy MMA bad N: $N";
65             }
66             my @Ns_arrays;
67             my %arrays;
68             foreach my $i (0 .. $#parameters) {
69             $arrays{"ma_$i"} = $Ns_arrays[$i] = [];
70             }
71             return $class->SUPER::new
72             (parent => $parent,
73             parameters => \@parameters,
74             Ns_arrays => \@Ns_arrays,
75             arrays => \%arrays,
76             array_aliases => { values => 'ma_0' });
77             }
78              
79             sub warmup_count {
80             my ($self) = @_;
81             return App::Chart::Series::Derived::EMA->warmup_count
82             (max (@{$self->{'parameters'}}));
83             }
84              
85             sub fill_part {
86             my ($self, $lo, $hi) = @_;
87             my $parent = $self->{'parent'};
88             my $Ns = $self->{'parameters'};
89              
90             my $warmup_count = $self->warmup_count_for_position ($lo);
91             my $start = $parent->find_before ($lo, $warmup_count);
92             $parent->fill ($start, $hi);
93             my $p = $parent->values_array;
94              
95             $hi = min ($hi, $#$p);
96             my @procs = map {App::Chart::Series::Derived::EMA->proc($_)} @$Ns;
97              
98             foreach my $i ($start .. $lo-1) {
99             my $value = $p->[$i] // next;
100             foreach (@procs) {
101             $_->($value);
102             }
103             }
104              
105             my $arrays = $self->{'Ns_arrays'};
106             foreach my $i ($lo .. $hi) {
107             my $value = $p->[$i] // next;
108              
109             foreach my $j (0 .. $#procs) {
110             $arrays->[$j]->[$i] = $procs[$j]->($value);
111             }
112             }
113             }
114              
115             1;
116             __END__
117              
118             # =head1 NAME
119             #
120             # App::Chart::Series::Derived::GuppyMMA -- Guppy multiple moving averages
121             #
122             # =head1 SYNOPSIS
123             #
124             # my $series = $parent->GuppyMMA;
125             # my $series = $parent->GuppyMMA (3, 5, 8, 10, ...);
126             #
127             # =head1 DESCRIPTION
128             #
129             # ...
130             #
131             # =head1 SEE ALSO
132             #
133             # L<App::Chart::Series>, L<App::Chart::Series::Derived::EMA>
134             #
135             # =cut