| 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::GAPO; | ||
| 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 | |||
| 27 | # http://trader.online.pl/MSZ/e-w-Gopalakrishnan_Range_Index_GAPO.html | ||
| 28 | # Formula, sample chart for WIG something. | ||
| 29 | # | ||
| 30 | # http://www.tradingsolutions.com/download/tip0105.zip | ||
| 31 | # Formula. | ||
| 32 | # | ||
| 33 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/012001/TradersTips/TradersTips.html | ||
| 34 | # Traders tips formulas for several packages. Small sample chart of | ||
| 35 | # ^IXIC nasdaq composite for 2000. | ||
| 36 | # | ||
| 37 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/012001/Abstracts_new/Jayanthi/Jayanthi.html | ||
| 38 | # First few paras of article. | ||
| 39 | # | ||
| 40 | # http://store.traders.com/v19114gopran.html | ||
| 41 | # Article for sale, first para only. | ||
| 42 | |||
| 43 | |||
| 44 | sub longname { __('GAPO') } | ||
| 45 | *shortname = \&longname; | ||
| 46 | sub manual { __p('manual-node','Gopalakrishnan Range Index') } | ||
| 47 | |||
| 48 | use constant | ||
| 49 | { type => 'indicator', | ||
| 50 | priority => -10, | ||
| 51 | minimum => 0, | ||
| 52 | hlines => [ 0 ], | ||
| 53 | parameter_info => [ { name => __('Days'), | ||
| 54 | key => 'gapo_days', | ||
| 55 | type => 'integer', | ||
| 56 | minimum => 1, | ||
| 57 | default => 5 } ], | ||
| 58 | }; | ||
| 59 | sub maximum { | ||
| 60 | my ($self) = @_; | ||
| 61 | if (defined $self) { | ||
| 62 | return log ($self->{'N'}); | ||
| 63 | } else { | ||
| 64 | return undef; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub new { | ||
| 69 | my ($class, $parent, $N) = @_; | ||
| 70 | |||
| 71 | $N //= parameter_info()->[0]->{'default'}; | ||
| 72 | ($N > 0) || croak "GAPO bad N: $N"; | ||
| 73 | |||
| 74 | return $class->SUPER::new | ||
| 75 | (parent => $parent, | ||
| 76 | N => $N, | ||
| 77 | parameters => [ $N ], | ||
| 78 | arrays => { values => [] }, | ||
| 79 | array_aliases => { }); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub fill_part { | ||
| 83 | my ($self, $lo, $hi) = @_; | ||
| 84 | my $parent = $self->{'parent'}; | ||
| 85 | my $N = $self->{'N'}; | ||
| 86 | |||
| 87 | my $start = $parent->find_before ($lo, $N-1); | ||
| 88 | $parent->fill ($lo, $hi); | ||
| 89 | my $pp = $parent->values_array; | ||
| 90 | my $ph = $parent->array('highs') || $pp; | ||
| 91 | my $pl = $parent->array('lows') || $pp; | ||
| 92 | |||
| 93 | my $s = $self->values_array; | ||
| 94 | $hi = min ($hi, $#$pp); | ||
| 95 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 96 | |||
| 97 | my @h; | ||
| 98 | my @l; | ||
| 99 | |||
| 100 | foreach my $i ($start .. $lo-1) { | ||
| 101 | my $value = $pp->[$i] // next; | ||
| 102 | unshift @h, $ph->[$i] // $value; | ||
| 103 | unshift @l, $pl->[$i] // $value; | ||
| 104 | } | ||
| 105 | my $scale = 1 / log(scalar @h); | ||
| 106 | |||
| 107 | foreach my $i ($lo .. $hi) { | ||
| 108 | my $value = $pp->[$i] // next; | ||
| 109 | |||
| 110 | if (@h >= $N) { | ||
| 111 | # drop old | ||
| 112 | pop @h; | ||
| 113 | pop @l; | ||
| 114 | } else { | ||
| 115 | # gain new point, update scale | ||
| 116 | $scale = 1 / log(scalar @h); | ||
| 117 | } | ||
| 118 | unshift @h, $ph->[$i] // $value; | ||
| 119 | unshift @l, $pl->[$i] // $value; | ||
| 120 | |||
| 121 | my $highhigh = max (@h); | ||
| 122 | my $lowlow = min (@l); | ||
| 123 | my $range = $highhigh - $lowlow; | ||
| 124 | $s->[$i] = log ($range) * $scale; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | 1; | ||
| 130 | __END__ |