| 1 | # Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 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::AccDist; | ||
| 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 | |||
| 27 | use constant DEBUG => 0; | ||
| 28 | |||
| 29 | sub longname { __('Accumulation/Distribution Index') } | ||
| 30 | sub shortname { __("Acc/Dist") } | ||
| 31 | sub manual { __p('manual-node','Accumulation/Distribution') } | ||
| 32 | |||
| 33 | use constant | ||
| 34 | { type => 'indicator', | ||
| 35 | units => 'AccDist', | ||
| 36 | }; | ||
| 37 | |||
| 38 | sub new { | ||
| 39 | my ($class, $parent) = @_; | ||
| 40 | |||
| 41 | return $class->SUPER::new | ||
| 42 | (parent => $parent, | ||
| 43 | arrays => { values => [] }, | ||
| 44 | array_aliases => { }); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub proc { | ||
| 48 | my ($self) = @_; | ||
| 49 | my $parent = $self->parent; | ||
| 50 | my $p = $parent->values_array; | ||
| 51 | my $ph = $parent->array('highs') || $p; | ||
| 52 | my $pl = $parent->array('lows') || $p; | ||
| 53 | my $pv = $parent->array('volumes') || []; | ||
| 54 | sub { | ||
| 55 | my ($i, $value) = @_; | ||
| 56 | |||
| 57 | my $high = $ph->[$i] // $value; | ||
| 58 | my $low = $pl->[$i] // $value; | ||
| 59 | my $volume = $pv->[$i] // 0; | ||
| 60 | my $range = $high - $low; | ||
| 61 | |||
| 62 | return ($range == 0 ? 0 | ||
| 63 | : $volume * (-1 + 2 * ($value - $low) / $range)); | ||
| 64 | }; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub fill_part { | ||
| 68 | my ($self, $lo, $hi) = @_; | ||
| 69 | my $parent = $self->{'parent'}; | ||
| 70 | |||
| 71 | $parent->fill ($lo, $hi); | ||
| 72 | my $p = $parent->values_array; | ||
| 73 | |||
| 74 | my $s = $self->values_array; | ||
| 75 | $hi = min ($hi, $#$p); | ||
| 76 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 77 | |||
| 78 | my $lo_done = ($self->{'lo_done'} //= do { | ||
| 79 | my $i; | ||
| 80 | if ((($i = $parent->find_after($lo,1)) && defined $p->[$i]) | ||
| 81 | || (($i = $parent->find_before($lo,1)) && defined $p->[$i])) { | ||
| 82 | $s->[$i] = 0; | ||
| 83 | $i; | ||
| 84 | } else { | ||
| 85 | # no values at all in $parent | ||
| 86 | $self->{'hi_done'} = $parent->hi; | ||
| 87 | 0 | ||
| 88 | } | ||
| 89 | }); | ||
| 90 | my $hi_done = ($self->{'hi_done'} //= $lo_done); | ||
| 91 | my $proc = $self->proc (@{$self->{'parameters'}}); | ||
| 92 | |||
| 93 | # extend $hi_done to $hi | ||
| 94 | if ($hi > $hi_done) { | ||
| 95 | if (DEBUG) { print " extend forward from $hi_done to $hi\n"; } | ||
| 96 | my $i = $parent->find_before ($hi_done, 1); | ||
| 97 | my $acc = $s->[$i]; | ||
| 98 | |||
| 99 | for ( ; $i <= $hi; $i++) { | ||
| 100 | my $value = $p->[$i] // next; | ||
| 101 | $s->[$i] = ($acc += $proc->($i,$value)); | ||
| 102 | } | ||
| 103 | $self->{'hi_done'} = $hi; | ||
| 104 | } | ||
| 105 | |||
| 106 | # extend $lo_done to $lo | ||
| 107 | if ($lo < $lo_done) { | ||
| 108 | if (DEBUG) { print " extend backward from $lo_done to $lo\n"; } | ||
| 109 | my $i = $parent->find_after ($lo_done, 1); | ||
| 110 | my $acc = $s->[$i]; | ||
| 111 | |||
| 112 | for ( ; $i >= $lo; $i--) { | ||
| 113 | my $value = $p->[$i] // next; | ||
| 114 | $s->[$i] = ($acc -= $proc->($i,$value)); | ||
| 115 | } | ||
| 116 | $self->{'lo_done'} = $hi; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; | ||
| 121 | __END__ |