| 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::Ease; | ||
| 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.incrediblecharts.com/technical/emv_construction.htm | ||
| 32 | # | ||
| 33 | # http://www.paritech.com.au/education/technical/indicators/strength/ease.asp | ||
| 34 | # | ||
| 35 | # http://www.linnsoft.com/tour/techind/arms.htm | ||
| 36 | # Sample Apple (AAPL) from April 2000, but using SMA instead of EMA. | ||
| 37 | # | ||
| 38 | # http://kb.worden.com/default.asp?id=49&Lang=1&SID= | ||
| 39 | # Sample Amazon (AMZN) from 2006, 14-day, maybe SMA instead of EMA. | ||
| 40 | # | ||
| 41 | |||
| 42 | sub longname { __('Ease of Movement') } | ||
| 43 | sub shortname { __('Ease') } | ||
| 44 | sub manual { __p('manual-node','Ease of Movement') } | ||
| 45 | |||
| 46 | use constant | ||
| 47 | { hlines => [ 0 ], | ||
| 48 | type => 'indicator', | ||
| 49 | units => 'Ease', | ||
| 50 | parameter_info => [ { name => __('Days'), | ||
| 51 | key => 'ease_days', | ||
| 52 | type => 'float', | ||
| 53 | minimum => 1, | ||
| 54 | default => 14, | ||
| 55 | decimals => 0, | ||
| 56 | step => 1 }], | ||
| 57 | }; | ||
| 58 | |||
| 59 | sub new { | ||
| 60 | my ($class, $parent, $N) = @_; | ||
| 61 | |||
| 62 | $N //= parameter_info()->[0]->{'default'}; | ||
| 63 | ($N > 0) || croak "Ease bad N: $N"; | ||
| 64 | |||
| 65 | return $class->SUPER::new | ||
| 66 | (parent => $parent, | ||
| 67 | parameters => [ $N ], | ||
| 68 | arrays => { values => [] }, | ||
| 69 | array_aliases => { }); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub warmup_count { | ||
| 73 | my ($class_or_self, $N) = @_; | ||
| 74 | return 1 + App::Chart::Series::Derived::EMA->warmup_count($N); | ||
| 75 | } | ||
| 76 | |||
| 77 | # Return a procedure which calculates a relative volatility index, using | ||
| 78 | # Dorsey's original 1993 definition, over an accumulated window. | ||
| 79 | # | ||
| 80 | sub proc { | ||
| 81 | my ($class_or_self, $N) = @_; | ||
| 82 | |||
| 83 | my $ema_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 84 | my $prev_mid; | ||
| 85 | |||
| 86 | return sub { | ||
| 87 | my ($high, $low, $close, $volume) = @_; | ||
| 88 | $high //= $close; | ||
| 89 | $low //= $close; | ||
| 90 | my $mid = ($high + $low) / 2; | ||
| 91 | |||
| 92 | my $ease; | ||
| 93 | if (defined $prev_mid) { | ||
| 94 | $volume //= 0; | ||
| 95 | $ease = ($mid - $prev_mid) * ($high - $low) * 1e6 / $volume; | ||
| 96 | $ease = $ema_proc->($ease); | ||
| 97 | } | ||
| 98 | $prev_mid = $mid; | ||
| 99 | return $ease; | ||
| 100 | }; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub fill_part { | ||
| 104 | my ($self, $lo, $hi) = @_; | ||
| 105 | my $parent = $self->{'parent'}; | ||
| 106 | |||
| 107 | my $warmup_count = $self->warmup_count_for_position ($lo); | ||
| 108 | my $start = $parent->find_before ($lo, $warmup_count); | ||
| 109 | $parent->fill ($lo, $hi); | ||
| 110 | my $p = $parent->values_array; | ||
| 111 | my $ph = $parent->array('highs') || $p; | ||
| 112 | my $pl = $parent->array('lows') || $p; | ||
| 113 | my $pv = $parent->array('volumes') || []; | ||
| 114 | |||
| 115 | my $s = $self->values_array; | ||
| 116 | $hi = min ($hi, $#$p); | ||
| 117 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 118 | |||
| 119 | my $proc = $self->proc(@{$self->{'parameters'}}); | ||
| 120 | |||
| 121 | foreach my $i ($start .. $lo-1) { | ||
| 122 | my $close = $p->[$i] // next; | ||
| 123 | $proc->($ph->[$i], $pl->[$i], $close, $pv->[$i]); | ||
| 124 | } | ||
| 125 | foreach my $i ($lo .. $hi) { | ||
| 126 | my $close = $p->[$i] // next; | ||
| 127 | $s->[$i] = $proc->($ph->[$i], $pl->[$i], $close, $pv->[$i]); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | 1; | ||
| 131 | __END__ |