| 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::VOLR; | ||
| 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::MFI; | ||
| 27 | use App::Chart::Series::Derived::WilliamsR; | ||
| 28 | |||
| 29 | # http://www.incrediblecharts.com/indicators/volatility_ratio_schwager.php | ||
| 30 | |||
| 31 | sub longname { __('Volatility Ratio') } | ||
| 32 | sub shortname { __('VOLR') } | ||
| 33 | sub manual { __p('manual-node','Volatility Ratio') } | ||
| 34 | |||
| 35 | use constant | ||
| 36 | { type => 'indicator', | ||
| 37 | units => 'zero_to_one', | ||
| 38 | minimum => 0, | ||
| 39 | maximum => 1, | ||
| 40 | hlines => [ 0.5 ], | ||
| 41 | parameter_info => [ { name => __('Days'), | ||
| 42 | key => 'volr_days', | ||
| 43 | type => 'integer', | ||
| 44 | minimum => 1, | ||
| 45 | default => 10 }], | ||
| 46 | }; | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my ($class, $parent, $N) = @_; | ||
| 50 | |||
| 51 | $N //= parameter_info()->[0]->{'default'}; | ||
| 52 | ($N > 0) || croak "VOLR bad N: $N"; | ||
| 53 | |||
| 54 | return $class->SUPER::new | ||
| 55 | (parent => $parent, | ||
| 56 | parameters => [ $N ], | ||
| 57 | arrays => { values => [] }, | ||
| 58 | array_aliases => { }); | ||
| 59 | } | ||
| 60 | *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part; | ||
| 61 | *warmup_count = \&App::Chart::Series::Derived::MFI::warmup_count; # $N | ||
| 62 | |||
| 63 | sub proc { | ||
| 64 | my ($class_or_self, $N) = @_; | ||
| 65 | my @highs; | ||
| 66 | my @lows; | ||
| 67 | my $prev_close; | ||
| 68 | |||
| 69 | return sub { | ||
| 70 | my ($high, $low, $close) = @_; | ||
| 71 | |||
| 72 | $high //= $close; | ||
| 73 | $low //= $close; | ||
| 74 | |||
| 75 | if (defined $prev_close) { | ||
| 76 | $high = max ($high, $prev_close); # true high, true low | ||
| 77 | $low = min ($low, $prev_close); | ||
| 78 | } | ||
| 79 | $prev_close = $close; | ||
| 80 | |||
| 81 | unshift @highs, $high; | ||
| 82 | unshift @lows, $low; | ||
| 83 | if (@highs > $N) { | ||
| 84 | pop @highs; | ||
| 85 | pop @lows; | ||
| 86 | } | ||
| 87 | |||
| 88 | my $period_high = max (@highs); | ||
| 89 | my $period_low = min (@lows); | ||
| 90 | my $period_range = $period_high - $period_low; | ||
| 91 | return ($period_range == 0 ? 0.5 : ($high-$low)/$period_range); | ||
| 92 | }; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 | __END__ |