| 1 | # Copyright 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::Aroon; | ||
| 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 { __('Aroon') } | ||
| 30 | sub shortname { __('Aroon') } | ||
| 31 | sub manual { __p('manual-node','Aroon') } | ||
| 32 | |||
| 33 | use constant | ||
| 34 | { type => 'indicator', | ||
| 35 | minimum => -100, | ||
| 36 | maximum => 100, | ||
| 37 | hlines => [ 0 ], | ||
| 38 | parameter_info => [ { name => __('Days'), | ||
| 39 | key => 'aroon_days', | ||
| 40 | type => 'integer', | ||
| 41 | minimum => 1, | ||
| 42 | default => 25 } ], | ||
| 43 | decimals => 0, # none needed normally | ||
| 44 | |||
| 45 | line_colours => { up => App::Chart::UP_COLOUR(), | ||
| 46 | down => App::Chart::DOWN_COLOUR() }, | ||
| 47 | }; | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my ($class, $parent, $N) = @_; | ||
| 51 | |||
| 52 | $N //= parameter_info()->[0]->{'default'}; | ||
| 53 | |||
| 54 | return $class->SUPER::new | ||
| 55 | (parent => $parent, | ||
| 56 | N => $N, | ||
| 57 | parameters => [ $N ], | ||
| 58 | arrays => { up => [], | ||
| 59 | down => [], | ||
| 60 | oscillator => [] }, | ||
| 61 | array_aliases => { values => 'oscillator' }); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub fill_part { | ||
| 65 | my ($self, $lo, $hi) = @_; | ||
| 66 | my $parent = $self->{'parent'}; | ||
| 67 | my $N = $self->{'N'}; | ||
| 68 | |||
| 69 | my $start = $parent->find_before ($lo, $N-1); | ||
| 70 | $parent->fill ($start, $hi); | ||
| 71 | |||
| 72 | my $p = $parent->array('values'); | ||
| 73 | $hi = min ($hi, $#$p); | ||
| 74 | |||
| 75 | my $up = $self->array('up'); | ||
| 76 | my $down = $self->array('down'); | ||
| 77 | my $osc = $self->array('oscillator'); | ||
| 78 | if ($#$up < $hi) { $#$up = $hi; } # pre-extend | ||
| 79 | if ($#$down < $hi) { $#$down = $hi; } | ||
| 80 | if ($#$osc < $hi) { $#$osc = $hi; } | ||
| 81 | |||
| 82 | my @a; | ||
| 83 | foreach my $i ($start .. $lo-1) { | ||
| 84 | my $value = $p->[$i] // next; | ||
| 85 | unshift @a, $value; | ||
| 86 | } | ||
| 87 | |||
| 88 | foreach my $i ($lo .. $hi) { | ||
| 89 | my $value = $p->[$i] // next; | ||
| 90 | |||
| 91 | # keep last $N + 1 many points | ||
| 92 | if (@a > $N) { | ||
| 93 | pop @a; # drop old | ||
| 94 | } | ||
| 95 | unshift @a, $value; # add new | ||
| 96 | |||
| 97 | # for equal highs or equal lows, the oldest is taken, on the | ||
| 98 | # theory that a new high/low has not been established but rather | ||
| 99 | # the old one is still in force | ||
| 100 | # | ||
| 101 | if (DEBUG) { require Data::Dumper; | ||
| 102 | print "Aroon on ",Data::Dumper->Dump([\@a],['a']); } | ||
| 103 | my $high_val = my $low_val = $a[0]; | ||
| 104 | my $high_pos = my $low_pos = 0; | ||
| 105 | foreach my $pos (1 .. $#a) { | ||
| 106 | $value = $a[$pos]; | ||
| 107 | if ($value >= $high_val) { $high_val = $value; $high_pos = $pos; } | ||
| 108 | if ($value <= $low_val) { $low_val = $value; $low_pos = $pos; } | ||
| 109 | } | ||
| 110 | |||
| 111 | if (DEBUG) { print " high_val=$high_val high_pos=$high_pos", | ||
| 112 | " low_val=$low_val low_pos=$low_pos in N=$N\n"; } | ||
| 113 | $up->[$i] = $high_pos = 100 - 100 * $high_pos / $N; | ||
| 114 | $down->[$i] = $low_pos = 100 - 100 * $low_pos / $N; | ||
| 115 | $osc->[$i] = $high_pos - $low_pos; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | 1; | ||
| 121 | __END__ |