App-Chart

 view release on metacpan or  search on metacpan

lib/App/Chart/Series/Derived/Stochastics.pm  view on Meta::CPAN

# Copyright 2004, 2005, 2006, 2007, 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::Stochastics;
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';
use App::Chart::Series::Derived::SMA;
use App::Chart::Series::Derived::WilliamsR;


sub longname   { __('Stochastics %K/%D') }
sub shortname  { __('%R') }
sub manual     { __p('manual-node','Stochastics') }

use constant
  { type       => 'indicator',
    units      => 'percentage',
    minimum    => 0,
    maximum    => 100,
    hlines     => [ 20, 80 ],
    parameter_info => [ { name    => __('%K Days'),
                          key     => 'stochastics_K_days',
                          type    => 'integer',
                          minimum => 1,
                          default => 14 },
                        { name    => __('%D Days'),
                          key     => 'stochastics_D_days',
                          type    => 'integer',
                          minimum => 1,
                          default => 3 },
                        { name    => __('Slow Days'),
                          key     => 'stochastics_slow_days',
                          type    => 'integer',
                          minimum => 0,
                          default => 0 }],

    line_colours => { K => App::Chart::DOWN_COLOUR(),
                      D => App::Chart::UP_COLOUR() },
  };

sub new {
  my ($class, $parent, $K_count, $D_count, $slow_count) = @_;

  $K_count    //= parameter_info()->[0]->{'default'};
  $D_count    //= parameter_info()->[1]->{'default'};
  $slow_count //= parameter_info()->[2]->{'default'};

  return $class->SUPER::new
    (parent     => $parent,
     parameters => [ $K_count, $D_count, $slow_count ],
     arrays     => { K => [],
                     D => [] },
     array_aliases => { values => 'K' });
}

sub name {
  my ($self) = @_;
  my ($K_count, $D_count, $slow_count) = @{$self->{'parameters'}};
  if ($slow_count == 0) {
    return __x('%K {K_count}, %D {D_count}, no slow',
               K_count => $K_count,
               D_count => $D_count);
  } else {
    return __x('%K {K_count}, %D {D_count}, slow {slow_count}',
               K_count => $K_count,
               D_count => $D_count,
               slow_count => $slow_count);
  }
}

sub warmup_count {
  my ($self_or_class, $K_count, $D_count, $slow_count) = @_;
  return $K_count
    + max (1, $slow_count) - 1
      + $D_count - 1;
}



( run in 0.588 second using v1.01-cache-2.11-cpan-39bf76dae61 )