| 1 | # Copyright 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011 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::RSI; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use Locale::TextDomain 1.17; # for __p() | ||
| 24 | use Locale::TextDomain ('App-Chart'); | ||
| 25 | |||
| 26 | use base 'App::Chart::Series::Indicator'; | ||
| 27 | use App::Chart::Series::Derived::EMA; | ||
| 28 | |||
| 29 | |||
| 30 | # http://www.incrediblecharts.com/technical/relative_strength_index.php | ||
| 31 | # Wilder's EMA reckoning, simple average for initial. | ||
| 32 | # | ||
| 33 | # http://www.stockcharts.com/education/IndicatorAnalysis/indic_RSI.html | ||
| 34 | # Numerical examples, graph of DELL. | ||
| 35 | # | ||
| 36 | |||
| 37 | sub longname { __('RSI - Relative Strength Index') } | ||
| 38 | sub shortname { __('RSI') } | ||
| 39 | sub manual { __p('manual-node','Relative Strength Index') } | ||
| 40 | |||
| 41 | use constant | ||
| 42 | { hlines => [ 30, 50, 70 ], | ||
| 43 | type => 'indicator', | ||
| 44 | units => 'percentage', | ||
| 45 | minimum => 0, | ||
| 46 | maximum => 100, | ||
| 47 | parameter_info => [ { name => __('Days'), | ||
| 48 | key => 'rsi_days', | ||
| 49 | type => 'float', | ||
| 50 | minimum => 1, | ||
| 51 | default => 14, | ||
| 52 | decimals => 0, | ||
| 53 | step => 1 } ], | ||
| 54 | }; | ||
| 55 | |||
| 56 | sub new { | ||
| 57 | my ($class, $parent, $N) = @_; | ||
| 58 | |||
| 59 | $N //= parameter_info()->[0]->{'default'}; | ||
| 60 | ($N > 0) || croak "RSI bad N: $N"; | ||
| 61 | |||
| 62 | return $class->SUPER::new | ||
| 63 | (parent => $parent, | ||
| 64 | parameters => [ $N ], | ||
| 65 | arrays => { values => [] }, | ||
| 66 | array_aliases => { }); | ||
| 67 | } | ||
| 68 | sub proc { | ||
| 69 | my ($class_or_self, $N) = @_; | ||
| 70 | $N = App::Chart::Series::Derived::EMA::N_from_Wilder_N($N); | ||
| 71 | my $up_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 72 | my $down_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 73 | my $prev; | ||
| 74 | return sub { | ||
| 75 | my ($value) = @_; | ||
| 76 | my $ret; | ||
| 77 | if (defined $prev) { | ||
| 78 | my $up = $up_proc->(max (0, $value - $prev)); | ||
| 79 | my $down = $down_proc->(max (0, $prev - $value)); | ||
| 80 | my $total = $up + $down; | ||
| 81 | $ret = ($total == 0 ? 50 : 100 * $up / $total); | ||
| 82 | } | ||
| 83 | $prev = $value; | ||
| 84 | return $ret; | ||
| 85 | }; | ||
| 86 | } | ||
| 87 | sub warmup_count { | ||
| 88 | my ($class_or_self, $N) = @_; | ||
| 89 | $N = App::Chart::Series::Derived::EMA::N_from_Wilder_N($N); | ||
| 90 | return 1 + App::Chart::Series::Derived::EMA->warmup_count($N); | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; | ||
| 94 | __END__ |