| 1 | # Copyright 2002, 2003, 2004, 2005, 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::Ichimoku; | ||
| 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://www.fmlabs.com/reference/IchimokuKinkoHyo.htm | ||
| 28 | # Formulas, some description. | ||
| 29 | |||
| 30 | |||
| 31 | sub longname { __('Ichimoku Kinko Hyo') } | ||
| 32 | sub shortname { __('Ichimoku') } | ||
| 33 | sub manual { __p('manual-node','Ichimoku Kinko Hyo') } | ||
| 34 | |||
| 35 | use constant | ||
| 36 | { hlines => [ 0 ], | ||
| 37 | type => 'average', | ||
| 38 | minimum => -10, | ||
| 39 | maximum => 10, | ||
| 40 | parameter_info => [ { name => __('Tenkan Days'), | ||
| 41 | key => 'tenkan_N', | ||
| 42 | type => 'integer', | ||
| 43 | minimum => 1, | ||
| 44 | default => 9 }, | ||
| 45 | { name => __('Kijun Days'), | ||
| 46 | key => 'kijun_N', | ||
| 47 | type => 'integer', | ||
| 48 | minimum => 1, | ||
| 49 | default => 26 }, | ||
| 50 | ], | ||
| 51 | line_colours => { tenkan => 'orange', | ||
| 52 | kijun => 'dark red', | ||
| 53 | chikou => 'blue', | ||
| 54 | senkou_A => 'dark green', | ||
| 55 | senkou_B => 'dark green' }, | ||
| 56 | }; | ||
| 57 | |||
| 58 | sub new { | ||
| 59 | my ($class, $parent, $tenkan_N, $kijun_N) = @_; | ||
| 60 | ### Ichimoku new(): "@_" | ||
| 61 | |||
| 62 | $tenkan_N //= parameter_info()->[0]->{'default'}; | ||
| 63 | ($tenkan_N > 0) || croak "Ichimoku bad tenkan_N: $tenkan_N"; | ||
| 64 | |||
| 65 | $kijun_N //= parameter_info()->[1]->{'default'}; | ||
| 66 | ($kijun_N > 0) || croak "Ichimoku bad kijun_N: $kijun_N"; | ||
| 67 | |||
| 68 | return $class->SUPER::new | ||
| 69 | (parent => $parent, | ||
| 70 | parameters => [ $tenkan_N, $kijun_N ], | ||
| 71 | tenkan_N => $tenkan_N, | ||
| 72 | kijun_N => $kijun_N, | ||
| 73 | hi => $parent->hi + $kijun_N, | ||
| 74 | arrays => { tenkan => [], | ||
| 75 | kijun => [], | ||
| 76 | senkou_A => [], | ||
| 77 | senkou_B => [], | ||
| 78 | chikou => [], | ||
| 79 | }, | ||
| 80 | array_aliases => { values => 'tenkan' }); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub fill_part { | ||
| 84 | my ($self, $lo, $hi) = @_; | ||
| 85 | my $parent = $self->{'parent'}; | ||
| 86 | my $kijun_N = $self->{'kijun_N'}; | ||
| 87 | my $tenkan_N = $self->{'tenkan_N'}; | ||
| 88 | |||
| 89 | my $start = $parent->find_before ($lo, 3 * $kijun_N - 1); | ||
| 90 | $parent->fill ($start, $hi); | ||
| 91 | my $p = $parent->values_array; | ||
| 92 | my $ph = $parent->array('highs') || $p; | ||
| 93 | my $pl = $parent->array('lows') || $p; | ||
| 94 | |||
| 95 | my $chikou_array = $self->array('chikou'); | ||
| 96 | my $tenkan_array = $self->array('tenkan'); | ||
| 97 | my $kijun_array = $self->array('kijun'); | ||
| 98 | my $senkou_A_array = $self->array('senkou_A'); | ||
| 99 | my $senkou_B_array = $self->array('senkou_B'); | ||
| 100 | |||
| 101 | # $hi = min ($hi, $#$p); | ||
| 102 | # if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 103 | |||
| 104 | # chikou copy of input closes but $kijun_N days behind | ||
| 105 | for (my $i = $lo; $i < $hi && $i + $kijun_N <= $#$p; $i++) { | ||
| 106 | $chikou_array->[$i] = $p->[$i + $kijun_N]; | ||
| 107 | } | ||
| 108 | |||
| 109 | my $array_N = 2 * $kijun_N; | ||
| 110 | my @h; | ||
| 111 | my @l; | ||
| 112 | foreach my $i ($start .. $hi) { | ||
| 113 | my $value = $p->[$i] // next; | ||
| 114 | unshift @h, $ph->[$i] // $value; | ||
| 115 | unshift @l, $pl->[$i] // $value; | ||
| 116 | if (@h > $array_N) { | ||
| 117 | pop @h; | ||
| 118 | pop @l; | ||
| 119 | } | ||
| 120 | |||
| 121 | if ($i >= $lo) { | ||
| 122 | my $h = max (@h[0 .. min ($#h, $tenkan_N - 1)]); | ||
| 123 | my $l = min (@l[0 .. min ($#l, $tenkan_N - 1)]); | ||
| 124 | my $tenkan = $tenkan_array->[$i] = ($h + $l) * 0.5; | ||
| 125 | |||
| 126 | $h = max (@h[0 .. min ($#h, $kijun_N - 1)]); | ||
| 127 | $l = min (@l[0 .. min ($#l, $kijun_N - 1)]); | ||
| 128 | my $kijun = $kijun_array->[$i] = ($h + $l) * 0.5; | ||
| 129 | |||
| 130 | my $s_i = $i + $kijun_N; | ||
| 131 | if ($s_i >= $lo && $s_i <= $hi) { | ||
| 132 | $senkou_A_array->[$s_i] = ($tenkan + $kijun) * 0.5; | ||
| 133 | |||
| 134 | $h = max (@h); | ||
| 135 | $l = min (@l); | ||
| 136 | # $h = max ($h, @h[$kijun_N .. $#h]); | ||
| 137 | # $l = min ($l, @l[$kijun_N .. $#l]); | ||
| 138 | $senkou_B_array->[$s_i] = ($h + $l) * 0.5; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | 1; | ||
| 145 | __END__ |