| 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::NVI; | ||
| 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::OBV; | ||
| 28 | |||
| 29 | # http://www.incrediblecharts.com/indicators/negative_volume_index.php | ||
| 30 | # http://www.incrediblecharts.com/indicators/positive_volume_index.php | ||
| 31 | # | ||
| 32 | # http://www.fosback.com/ | ||
| 33 | # Norman Fosback's web site (nothing on NVI). | ||
| 34 | # | ||
| 35 | |||
| 36 | |||
| 37 | sub longname { __('NVI - Negative Volume Index') } | ||
| 38 | sub shortname { __('NVI') } | ||
| 39 | sub manual { __p('manual-node','Negative Volume Index') } | ||
| 40 | |||
| 41 | use constant | ||
| 42 | { type => 'indicator', | ||
| 43 | units => 'NVI', | ||
| 44 | }; | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my ($class, $parent) = @_; | ||
| 48 | |||
| 49 | return $class->SUPER::new | ||
| 50 | (parent => $parent, | ||
| 51 | arrays => { values => [] }, | ||
| 52 | array_aliases => { }, | ||
| 53 | accumulate => 'multiply'); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub proc { | ||
| 57 | my ($self) = @_; | ||
| 58 | my $parent = $self->parent; | ||
| 59 | my $pv = $parent->array('volumes') || []; | ||
| 60 | sub { | ||
| 61 | my ($i, $value, $prev_i, $prev_value) = @_; | ||
| 62 | if ($prev_value == 0) { return 1; } | ||
| 63 | my $volume = $pv->[$i] // 0; | ||
| 64 | my $prev_volume = $pv->[$prev_i] // 0; | ||
| 65 | if ($volume < $prev_volume) { | ||
| 66 | return $value / $prev_value; | ||
| 67 | } else { | ||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | }; | ||
| 71 | } | ||
| 72 | |||
| 73 | *fill_part = \&App::Chart::Series::Derived::OBV::fill_part; | ||
| 74 | |||
| 75 | 1; | ||
| 76 | __END__ |