| 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::WilliamsR; | ||
| 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::SMA; | ||
| 27 | |||
| 28 | sub longname { __('Williams %R') } | ||
| 29 | sub shortname { __('%R') } | ||
| 30 | sub manual { __p('manual-node','Williams %R') } | ||
| 31 | |||
| 32 | use constant | ||
| 33 | { type => 'indicator', | ||
| 34 | units => 'negative_percentage', | ||
| 35 | minimum => -100, | ||
| 36 | maximum => 0, | ||
| 37 | hlines => [-20, -80], | ||
| 38 | parameter_info => [ { name => __('Days'), | ||
| 39 | key => 'williams_r_days', | ||
| 40 | type => 'integer', | ||
| 41 | minimum => 1, | ||
| 42 | default => 10 } ], | ||
| 43 | }; | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my ($class, $parent, $N) = @_; | ||
| 47 | |||
| 48 | $N //= parameter_info()->[0]->{'default'}; | ||
| 49 | ($N > 0) || croak "WilliamsR bad N: $N"; | ||
| 50 | |||
| 51 | return $class->SUPER::new | ||
| 52 | (parent => $parent, | ||
| 53 | parameters => [ $N ], | ||
| 54 | arrays => { values => [] }, | ||
| 55 | array_aliases => { }); | ||
| 56 | } | ||
| 57 | *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1 | ||
| 58 | |||
| 59 | sub proc { | ||
| 60 | my ($class_or_self, $N) = @_; | ||
| 61 | my @h; | ||
| 62 | my @l; | ||
| 63 | return sub { | ||
| 64 | my ($high, $low, $close) = @_; | ||
| 65 | |||
| 66 | unshift @h, $high // $close; | ||
| 67 | unshift @l, $low // $close; | ||
| 68 | if (@h > $N) { | ||
| 69 | pop @h; | ||
| 70 | pop @l; | ||
| 71 | } | ||
| 72 | |||
| 73 | my $highhigh = List::Util::max (@h); | ||
| 74 | my $lowlow = List::Util::min (@l); | ||
| 75 | my $range = $highhigh - $lowlow; | ||
| 76 | return ($range == 0 ? -50 : 100 * ($close - $highhigh) / $range); | ||
| 77 | }; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub fill_part { | ||
| 81 | my ($self, $lo, $hi) = @_; | ||
| 82 | my $parent = $self->{'parent'}; | ||
| 83 | |||
| 84 | my $warmup_count = $self->warmup_count_for_position ($lo); | ||
| 85 | my $start = $parent->find_before ($lo, $warmup_count); | ||
| 86 | $parent->fill ($start, $hi); | ||
| 87 | my $p = $parent->values_array; | ||
| 88 | my $ph = $parent->array('highs') || $p; | ||
| 89 | my $pl = $parent->array('lows') || $p; | ||
| 90 | |||
| 91 | my $s = $self->values_array; | ||
| 92 | $hi = min ($hi, $#$p); | ||
| 93 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 94 | |||
| 95 | my $proc = $self->proc(@{$self->{'parameters'}}); | ||
| 96 | |||
| 97 | foreach my $i ($start .. $lo-1) { | ||
| 98 | my $close = $p->[$i] // next; | ||
| 99 | $proc->($ph->[$i], $pl->[$i], $close); | ||
| 100 | } | ||
| 101 | foreach my $i ($lo .. $hi) { | ||
| 102 | my $close = $p->[$i] // next; | ||
| 103 | $s->[$i] = $proc->($ph->[$i], $pl->[$i], $close); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | 1; | ||
| 109 | __END__ |