| 1 | # Copyright 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::Fisher; | ||
| 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 | |||
| 28 | |||
| 29 | # http://www.mesasoftware.com/technicalpapers.htm | ||
| 30 | # http://www.mesasoftware.com/Papers/USING%20THE%20FISHER%20TRANSFORM.pdf | ||
| 31 | # Paper by John Elhers. | ||
| 32 | # | ||
| 33 | # http://www.linnsoft.com/tour/techind/fish.htm | ||
| 34 | # Sample NYSE QQQ, August 2002. | ||
| 35 | |||
| 36 | sub longname { __('Fisher Transform') } | ||
| 37 | sub shortname { __('Fisher') } | ||
| 38 | sub manual { __p('manual-node','Fisher Transform') } | ||
| 39 | |||
| 40 | use constant | ||
| 41 | { type => 'indicator', | ||
| 42 | units => 'Fisher', | ||
| 43 | hlines => [ 0 ], | ||
| 44 | parameter_info => [ { name => __('Days'), | ||
| 45 | key => 'fisher_days', | ||
| 46 | type => 'integer', | ||
| 47 | minimum => 1, | ||
| 48 | default => 10 }, | ||
| 49 | ], | ||
| 50 | }; | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my ($class, $parent, $N) = @_; | ||
| 54 | |||
| 55 | $N //= parameter_info()->[0]->{'default'}; | ||
| 56 | ($N > 0) || croak "Fisher bad N: $N"; | ||
| 57 | |||
| 58 | return $class->SUPER::new | ||
| 59 | (parent => $parent, | ||
| 60 | N => $N, | ||
| 61 | parameters => [ $N ], | ||
| 62 | arrays => { values => [] }, | ||
| 63 | array_aliases => { }); | ||
| 64 | } | ||
| 65 | |||
| 66 | # val-smooth is alpha=0.33, which is 5 days | ||
| 67 | # fish-smooth is alpha=0.5, which is 3 days | ||
| 68 | # | ||
| 69 | use constant _warmup_ema => | ||
| 70 | (App::Chart::Series::Derived::EMA->warmup_count (5) | ||
| 71 | + App::Chart::Series::Derived::EMA->warmup_count (3)); | ||
| 72 | sub warmup_count { | ||
| 73 | my ($class_or_self, $N) = @_; | ||
| 74 | return $N + _warmup_ema - 1; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub fill_part { | ||
| 78 | my ($self, $lo, $hi) = @_; | ||
| 79 | my $parent = $self->{'parent'}; | ||
| 80 | my $N = $self->{'N'}; | ||
| 81 | |||
| 82 | my $start = $parent->find_before ($lo, $self->warmup_count($N)); | ||
| 83 | $parent->fill ($start, $hi); | ||
| 84 | my $p = $parent->values_array; | ||
| 85 | |||
| 86 | my $s = $self->values_array; | ||
| 87 | $hi = min ($hi, $#$p); | ||
| 88 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 89 | |||
| 90 | my $ema5_proc = App::Chart::Series::Derived::EMA->proc(5); | ||
| 91 | my $ema3_proc = App::Chart::Series::Derived::EMA->proc(3); | ||
| 92 | |||
| 93 | my @array; | ||
| 94 | foreach my $i ($start .. $hi) { | ||
| 95 | my $value = $p->[$i] // next; | ||
| 96 | |||
| 97 | unshift @array, $value; | ||
| 98 | if (@array > $N) { pop @array; } | ||
| 99 | my $l = min (@array); | ||
| 100 | my $h = max (@array); | ||
| 101 | my $f = $h - $l; | ||
| 102 | if ($f != 0) { | ||
| 103 | $f = 2 * ($value - $l) / $f - 1; # raw = -1 to +1 | ||
| 104 | $f = $ema5_proc->($f); | ||
| 105 | $f = max (-0.999, min (0.999, $f)); # avoid full +/-1 infinities | ||
| 106 | $f = log ((1 + $f) / (1 - $f)); | ||
| 107 | $f = $ema3_proc->($f); | ||
| 108 | } | ||
| 109 | if ($i >= $lo) { | ||
| 110 | $s->[$i] = $f; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | __END__ |