| 1 | # Copyright 2006, 2007, 2009, 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::VIDYAalpha; | ||
| 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 | use App::Chart::Series::Calculation; | ||
| 27 | use App::Chart::Series::Derived::Stddev; | ||
| 28 | |||
| 29 | |||
| 30 | sub longname { __('VIDYA alpha') } | ||
| 31 | sub shortname { __('VIDYA alpha') } | ||
| 32 | sub manual { __p('manual-node','Variable Index Dynamic Average') } | ||
| 33 | |||
| 34 | use constant | ||
| 35 | { type => 'indicator', | ||
| 36 | priority => -10, | ||
| 37 | units => 'ema_alpha', | ||
| 38 | minimum => 0, | ||
| 39 | maximum => 1, | ||
| 40 | parameter_info => [ { name => __('Fast Days'), | ||
| 41 | key => 'vidya_fast_days', | ||
| 42 | type => 'integer', | ||
| 43 | minimum => 1, | ||
| 44 | default => 9 }, | ||
| 45 | { name => __('Slow Days'), | ||
| 46 | key => 'vidya_slow_days', | ||
| 47 | type => 'integer', | ||
| 48 | minimum => 1, | ||
| 49 | default => 30 }], | ||
| 50 | }; | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my ($class, $parent, $N_fast, $N_slow) = @_; | ||
| 54 | |||
| 55 | $N_fast //= parameter_info()->[0]->{'default'}; | ||
| 56 | ($N_fast > 0) || croak "VIDYA bad N_fast: $N_fast"; | ||
| 57 | |||
| 58 | $N_slow //= parameter_info()->[1]->{'default'}; | ||
| 59 | ($N_slow > 0) || croak "VIDYA bad N_slow: $N_slow"; | ||
| 60 | |||
| 61 | return $class->SUPER::new | ||
| 62 | (parent => $parent, | ||
| 63 | parameters => [ $N_fast, $N_slow ], | ||
| 64 | arrays => { values => [] }, | ||
| 65 | array_aliases => { }); | ||
| 66 | } | ||
| 67 | sub warmup_count { | ||
| 68 | my ($self_or_class, $N_fast, $N_slow) = @_; | ||
| 69 | return App::Chart::Series::Derived::Stddev->warmup_count | ||
| 70 | (max ($N_fast, $N_slow)); | ||
| 71 | } | ||
| 72 | sub proc { | ||
| 73 | my ($class, $N_fast, $N_slow) = @_; | ||
| 74 | ### VIDYAalpha proc(): $N_fast,$N_slow | ||
| 75 | |||
| 76 | my $fast_proc = App::Chart::Series::Derived::Stddev->proc ($N_fast); | ||
| 77 | my $slow_proc = App::Chart::Series::Derived::Stddev->proc ($N_slow); | ||
| 78 | |||
| 79 | # Not sure if the ratio could go above 5, to make an alpha above 1, | ||
| 80 | # probably yes with the right data and a small FAST-COUNT, so clamp to 1. | ||
| 81 | |||
| 82 | return sub { | ||
| 83 | my ($value) = @_; | ||
| 84 | my $slow = $slow_proc->($value); | ||
| 85 | my $fast = $fast_proc->($value); | ||
| 86 | return ($slow == 0 ? 0 | ||
| 87 | : min (1, 0.2 * $fast / $slow)); | ||
| 88 | }; | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | __END__ |