| 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::Ultimate; | ||
| 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 | use App::Chart::Series::Derived::WilliamsR; | ||
| 28 | |||
| 29 | # http://www.stockcharts.com/education/IndicatorAnalysis/indic_ultimate.html | ||
| 30 | |||
| 31 | |||
| 32 | sub longname { __('Ultimate Oscillator') } | ||
| 33 | sub shortname { __('Ultimate') } | ||
| 34 | sub manual { __p('manual-node','Ultimate Oscillator') } | ||
| 35 | |||
| 36 | use constant | ||
| 37 | { type => 'indicator', | ||
| 38 | units => 'percentage', | ||
| 39 | minimum => 0, | ||
| 40 | maximum => 100, | ||
| 41 | hlines => [ 30, 50, 70 ], | ||
| 42 | parameter_info => [ { name => __('Days'), | ||
| 43 | key => 'williams_r_days', | ||
| 44 | type => 'integer', | ||
| 45 | minimum => 1, | ||
| 46 | default => 10 } ], | ||
| 47 | }; | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my ($class, $parent, $N) = @_; | ||
| 51 | |||
| 52 | $N //= parameter_info()->[0]->{'default'}; | ||
| 53 | ($N > 0) || croak "Ultimate bad N: $N"; | ||
| 54 | |||
| 55 | return $class->SUPER::new | ||
| 56 | (parent => $parent, | ||
| 57 | parameters => [ $N ], | ||
| 58 | arrays => { values => [] }, | ||
| 59 | array_aliases => { }); | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | use constant { N1 => 7, | ||
| 64 | N2 => 14, | ||
| 65 | N3 => 28 }; | ||
| 66 | use constant warmup_count => N3; | ||
| 67 | |||
| 68 | sub proc { | ||
| 69 | my ($class_or_self) = @_; | ||
| 70 | my $prev_close; | ||
| 71 | |||
| 72 | my $b1_proc = App::Chart::Series::Derived::SMA->proc (N1); | ||
| 73 | my $t1_proc = App::Chart::Series::Derived::SMA->proc (N1); | ||
| 74 | my $b2_proc = App::Chart::Series::Derived::SMA->proc (N2); | ||
| 75 | my $t2_proc = App::Chart::Series::Derived::SMA->proc (N2); | ||
| 76 | my $b3_proc = App::Chart::Series::Derived::SMA->proc (N3); | ||
| 77 | my $t3_proc = App::Chart::Series::Derived::SMA->proc (N3); | ||
| 78 | |||
| 79 | return sub { | ||
| 80 | my ($high, $low, $close) = @_; | ||
| 81 | $high //= $close; | ||
| 82 | $low //= $close; | ||
| 83 | |||
| 84 | # extend to true high/low | ||
| 85 | if (defined $prev_close) { | ||
| 86 | $high = max ($high, $prev_close); | ||
| 87 | $low = min ($low, $prev_close); | ||
| 88 | } | ||
| 89 | $prev_close = $close; | ||
| 90 | |||
| 91 | my $bp = $close - $low; # buying pressure | ||
| 92 | my $tr = $high - $low; # true range | ||
| 93 | |||
| 94 | my $b1 = $b1_proc->($bp); | ||
| 95 | my $t1 = $t1_proc->($tr); | ||
| 96 | my $b2 = $b2_proc->($bp); | ||
| 97 | my $t2 = $t2_proc->($tr); | ||
| 98 | my $b3 = $b3_proc->($bp); | ||
| 99 | my $t3 = $t3_proc->($tr); | ||
| 100 | |||
| 101 | if ($t1 == 0 || $t2 == 0 || $t3 == 0) { return undef; } | ||
| 102 | |||
| 103 | return 100 * (4*$b1/$t1 + 2*$b2/$t2 + $b3/$t3) / 7; | ||
| 104 | }; | ||
| 105 | } | ||
| 106 | *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part; | ||
| 107 | |||
| 108 | 1; | ||
| 109 | __END__ |