| 1 | # Copyright 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::ForceIndex; | ||
| 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::EMA; | ||
| 27 | use App::Chart::Series::Derived::Stddev; | ||
| 28 | use App::Chart::Series::Derived::WilliamsR; | ||
| 29 | |||
| 30 | |||
| 31 | # http://www.linnsoft.com/tour/techind/efi.htm | ||
| 32 | # Formula, but sample only an intraday. | ||
| 33 | # | ||
| 34 | |||
| 35 | sub longname { __('Force Index') } | ||
| 36 | sub shortname { __('Force') } | ||
| 37 | sub manual { __p('manual-node','Force Index') } | ||
| 38 | |||
| 39 | use constant | ||
| 40 | { hlines => [ 0 ], | ||
| 41 | type => 'indicator', | ||
| 42 | units => 'ForceIndex', | ||
| 43 | parameter_info => [ { name => __('Smooth Days'), | ||
| 44 | key => 'force_index_days', | ||
| 45 | type => 'integer', | ||
| 46 | minimum => 1, | ||
| 47 | default => 2, | ||
| 48 | decimals => 0, | ||
| 49 | step => 1 }], | ||
| 50 | }; | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my ($class, $parent, $N) = @_; | ||
| 54 | |||
| 55 | $N //= parameter_info()->[0]->{'default'}; | ||
| 56 | ($N > 0) || croak "ForceIndex bad N: $N"; | ||
| 57 | |||
| 58 | return $class->SUPER::new | ||
| 59 | (parent => $parent, | ||
| 60 | parameters => [ $N ], | ||
| 61 | arrays => { values => [] }, | ||
| 62 | array_aliases => { }); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub warmup_count { | ||
| 66 | my ($class_or_self, $N) = @_; | ||
| 67 | return 1 + App::Chart::Series::Derived::EMA->warmup_count($N); | ||
| 68 | } | ||
| 69 | |||
| 70 | # Return a procedure which calculates a relative volatility index, using | ||
| 71 | # Dorsey's original 1993 definition, over an accumulated window. | ||
| 72 | # | ||
| 73 | sub proc { | ||
| 74 | my ($class_or_self, $N) = @_; | ||
| 75 | |||
| 76 | my $ema_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 77 | my $prev; | ||
| 78 | |||
| 79 | return sub { | ||
| 80 | my ($close, $volume) = @_; | ||
| 81 | my $force; | ||
| 82 | if (defined $prev) { | ||
| 83 | $force = $ema_proc->(($close - $prev) * ($volume // 0)); | ||
| 84 | } | ||
| 85 | $prev = $close; | ||
| 86 | return $force; | ||
| 87 | }; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub fill_part { | ||
| 91 | my ($self, $lo, $hi) = @_; | ||
| 92 | my $parent = $self->{'parent'}; | ||
| 93 | |||
| 94 | my $warmup_count = $self->warmup_count_for_position ($lo); | ||
| 95 | my $start = $parent->find_before ($lo, $warmup_count); | ||
| 96 | $parent->fill ($lo, $hi); | ||
| 97 | my $p = $parent->values_array; | ||
| 98 | my $pv = $parent->array('volumes') || []; | ||
| 99 | |||
| 100 | my $s = $self->values_array; | ||
| 101 | $hi = min ($hi, $#$p); | ||
| 102 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 103 | |||
| 104 | my $proc = $self->proc(@{$self->{'parameters'}}); | ||
| 105 | |||
| 106 | foreach my $i ($start .. $lo-1) { | ||
| 107 | my $close = $p->[$i] // next; | ||
| 108 | $proc->($close, $pv->[$i]); | ||
| 109 | } | ||
| 110 | foreach my $i ($lo .. $hi) { | ||
| 111 | my $close = $p->[$i] // next; | ||
| 112 | $s->[$i] = $proc->($close, $pv->[$i]); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | 1; | ||
| 116 | __END__ |