| 1 | # Copyright 2006, 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::ChaikinOsc; | ||
| 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::Derived::EMA; | ||
| 27 | |||
| 28 | sub longname { __('Chaikin Oscillator') } | ||
| 29 | sub shortname { __('Chaikin Osc') } | ||
| 30 | sub manual { __p('manual-node','Chaikin Oscillator') } | ||
| 31 | |||
| 32 | use constant | ||
| 33 | { type => 'indicator', | ||
| 34 | minimum => -1, | ||
| 35 | maximum => 1, | ||
| 36 | units => 'ChaikinOsc', | ||
| 37 | hlines => [ 0 ], | ||
| 38 | parameter_info => [ { name => __('Fast Days'), | ||
| 39 | key => 'chaikin_osc_fast_days', | ||
| 40 | type => 'float', | ||
| 41 | minimum => 1, | ||
| 42 | default => 3, | ||
| 43 | decimals => 0, | ||
| 44 | step => 1 }, | ||
| 45 | { name => __('Slow Days'), | ||
| 46 | key => 'chaikin_osc_slow_days', | ||
| 47 | type => 'float', | ||
| 48 | minimum => 1, | ||
| 49 | default => 10, | ||
| 50 | decimals => 0, | ||
| 51 | step => 1 }], | ||
| 52 | }; | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my ($class, $parent, $N_fast, $N_slow) = @_; | ||
| 56 | |||
| 57 | $N_fast //= parameter_info()->[0]->{'default'}; | ||
| 58 | ($N_fast > 0) || croak "ChaikinOsc bad N_fast: $N_fast"; | ||
| 59 | |||
| 60 | $N_slow //= parameter_info()->[0]->{'default'}; | ||
| 61 | ($N_slow > 0) || croak "ChaikinOsc bad N_slow: $N_slow"; | ||
| 62 | |||
| 63 | return $class->SUPER::new | ||
| 64 | (parent => $parent, | ||
| 65 | parameters => [ $N_fast, $N_slow ], | ||
| 66 | arrays => { values => [] }, | ||
| 67 | array_aliases => { }, | ||
| 68 | accdist => $parent->AccDist); | ||
| 69 | } | ||
| 70 | sub warmup_count { | ||
| 71 | my ($self_or_class, $N_fast, $N_slow) = @_; | ||
| 72 | return App::Chart::Series::Derived::EMA->warmup_count (max($N_fast, $N_slow)); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub fill_part { | ||
| 76 | my ($self, $lo, $hi) = @_; | ||
| 77 | |||
| 78 | my ($N_fast, $N_slow) = @{$self->{'parameters'}}; | ||
| 79 | my $fast_proc = App::Chart::Series::Derived::EMA->proc ($N_fast); | ||
| 80 | my $slow_proc = App::Chart::Series::Derived::EMA->proc ($N_slow); | ||
| 81 | my $accdist = $self->{'accdist'}; | ||
| 82 | |||
| 83 | my $warmup_count = $self->warmup_count_for_position ($lo); | ||
| 84 | my $start = $accdist->find_before ($lo, $warmup_count); | ||
| 85 | $accdist->fill ($start, $hi); | ||
| 86 | my $p = $accdist->values_array; | ||
| 87 | |||
| 88 | my $s = $self->values_array; | ||
| 89 | $hi = min ($hi, $#$p); | ||
| 90 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 91 | |||
| 92 | foreach my $i ($start .. $lo-1) { | ||
| 93 | my $value = $p->[$i] // next; | ||
| 94 | $fast_proc->($value); | ||
| 95 | $slow_proc->($value); | ||
| 96 | } | ||
| 97 | foreach my $i ($lo .. $hi) { | ||
| 98 | my $value = $p->[$i] // next; | ||
| 99 | $s->[$i] = $fast_proc->($value) - $slow_proc->($value); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 | __END__ |