App-Chart
view release on metacpan or search on metacpan
lib/App/Chart/Series/Derived/COG.pm view on Meta::CPAN
# Copyright 2007, 2008, 2009, 2010 Kevin Ryde
# This file is part of Chart.
#
# Chart is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3, or (at your option) any later version.
#
# Chart is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Chart. If not, see <http://www.gnu.org/licenses/>.
package App::Chart::Series::Derived::COG;
use 5.010;
use strict;
use warnings;
use Carp;
use List::Util qw(min max);
use Locale::TextDomain ('App-Chart');
use base 'App::Chart::Series::Indicator';
# http://www.mesasoftware.com/technicalpapers.htm
# http://www.mesasoftware.com/Papers/The%20CG%20Oscillator.pdf
# Paper by John Elhers.
#
# http://www.linnsoft.com/tour/techind/cog.htm
# Sample Intel (INTC) 2001/2.
#
# http://www.working-money.com/Documentation/FEEDbk_docs/Archive/082002/TradersTips/TradersTips.html
# Trader's tips August 2002, formulas from May 2002.
#
sub longname { __('Centre of Gravity') }
sub shortname { __('COG') }
sub manual { __p('manual-node','Centre of Gravity Oscillator') }
use constant
{ type => 'indicator',
maximum => -1,
parameter_info => [ { name => __('Days'),
key => 'cog_days',
type => 'integer',
minimum => 1,
default => 10 } ],
};
sub minimum {
my ($self) = @_;
return (ref $self ? - $self->{'N'} : undef);
}
sub new {
### COG new()
my ($class, $parent, $N) = @_;
($N > 0) || croak "COG bad N: $N";
return $class->SUPER::new
(parent => $parent,
N => $N,
parameters => [ $N ],
arrays => { values => [] },
array_aliases => { });
}
sub fill_part {
my ($self, $lo, $hi) = @_;
my $parent = $self->{'parent'};
my $N = $self->{'N'};
my $start = $parent->find_before ($lo, $N-1);
$parent->fill ($start, $hi);
my $s = $self->values_array; # self
my $p = $parent->values_array; # parent
$hi = min ($hi, $#$p);
if ($#$s < $hi) { $#$s = $hi; } # pre-extend
# @a is current points accumulated, or undef in empty positions. a[$pos]
# is the oldest point.
#
# $den is the sum of the values in @a.
#
# $num is the sum a[$pos]*1 + a[$pos-1]*2 + ... + a[$pos-N-1]*N, with $pos
# wrapping around in the $N elements of @a
#
my @a;
$#a = $N-1; # pre-extend
my $num = 0;
my $den = 0;
( run in 0.835 second using v1.01-cache-2.11-cpan-39bf76dae61 )