| 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::ZigZag; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use Locale::TextDomain 1.17; # for __p() | ||
| 23 | use Locale::TextDomain ('App-Chart'); | ||
| 24 | |||
| 25 | use base 'App::Chart::Series::Indicator'; | ||
| 26 | |||
| 27 | # http://stockcharts.com/education/IndicatorAnalysis/indic_ZigZag.html | ||
| 28 | # Sample HPQ chart 1999/2000. | ||
| 29 | |||
| 30 | sub longname { __('Zig Zag') } | ||
| 31 | *shortname = \&longname; | ||
| 32 | sub manual { __p('manual-node','Zig Zag Indicator') } | ||
| 33 | |||
| 34 | use constant | ||
| 35 | { type => 'average', | ||
| 36 | parameter_info => [ { name => __('% Change'), | ||
| 37 | key => 'zigzag_percent', | ||
| 38 | type => 'float', | ||
| 39 | type_hint => 'percent', | ||
| 40 | minimum => 0, | ||
| 41 | default => 5, | ||
| 42 | step => 1 }, | ||
| 43 | { name => __('Closes'), | ||
| 44 | key => 'zigzag_closes', | ||
| 45 | type => 'boolean', | ||
| 46 | default => 0 }, | ||
| 47 | ], | ||
| 48 | # default_linestyle => 'ZigZag', | ||
| 49 | line_colours => { values => 'solid' }, | ||
| 50 | }; | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my ($class, $parent, $percent, $closes_flag) = @_; | ||
| 54 | |||
| 55 | return $class->SUPER::new | ||
| 56 | (parent => $parent, | ||
| 57 | parameters => [ $percent, $closes_flag ], | ||
| 58 | arrays => { values => [] }, | ||
| 59 | array_aliases => { }); | ||
| 60 | } | ||
| 61 | |||
| 62 | # This does the whole series from start to end. It might be possible to | ||
| 63 | # work back looking for a PERCENT move which would establish the direction | ||
| 64 | # and hence a starting point in the middle of the data. | ||
| 65 | # | ||
| 66 | sub fill { | ||
| 67 | my ($self, $lo, $hi) = @_; | ||
| 68 | if ($self->{'filled'}) { return; } | ||
| 69 | $self->{'filled'} = 1; | ||
| 70 | |||
| 71 | my $parent = $self->{'parent'}; | ||
| 72 | my ($percent, $closes_flag) = @{$self->{'parameters'}}; | ||
| 73 | |||
| 74 | $hi = $self->hi; | ||
| 75 | $parent->fill (0, $hi); | ||
| 76 | my $p = $parent->values_array; | ||
| 77 | my $ph = $closes_flag ? $p : $parent->array('highs'); | ||
| 78 | my $pl = $closes_flag ? $p : $parent->array('lows'); | ||
| 79 | |||
| 80 | my $s = $self->values_array; | ||
| 81 | |||
| 82 | my $factor_increase = 1 + $percent / 100; | ||
| 83 | my $factor_decrease = 1 / $factor_increase; | ||
| 84 | my $direction = sub {}; | ||
| 85 | my $extreme; | ||
| 86 | my $target; | ||
| 87 | my $extreme_pos; | ||
| 88 | |||
| 89 | my ($rising, $falling); | ||
| 90 | $rising = sub { | ||
| 91 | my ($pos, $high, $low) = @_; | ||
| 92 | if (! defined $extreme || $high > $extreme) { | ||
| 93 | $extreme = $high; | ||
| 94 | $extreme_pos = $pos; | ||
| 95 | $target = $extreme * $factor_decrease; | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | if ($low <= $target) { | ||
| 99 | my $ret_pos = $extreme_pos; | ||
| 100 | my $ret_val = $extreme; | ||
| 101 | $direction = $falling; | ||
| 102 | $extreme = $low; | ||
| 103 | $extreme_pos = $pos; | ||
| 104 | $target = $extreme * $factor_increase; | ||
| 105 | return $ret_pos, $ret_val; | ||
| 106 | } | ||
| 107 | return; | ||
| 108 | }; | ||
| 109 | $falling = sub { | ||
| 110 | my ($pos, $high, $low) = @_; | ||
| 111 | if (! defined $extreme || $low < $extreme) { | ||
| 112 | $extreme = $low; | ||
| 113 | $extreme_pos = $pos; | ||
| 114 | $target = $extreme * $factor_increase; | ||
| 115 | return; | ||
| 116 | } | ||
| 117 | if ($low >= $target) { | ||
| 118 | my $ret_pos = $extreme_pos; | ||
| 119 | my $ret_val = $extreme; | ||
| 120 | $direction = $rising; | ||
| 121 | $extreme = $high; | ||
| 122 | $extreme_pos = $pos; | ||
| 123 | $target = $extreme * $factor_decrease; | ||
| 124 | return $ret_pos, $ret_val; | ||
| 125 | } | ||
| 126 | return; | ||
| 127 | }; | ||
| 128 | |||
| 129 | # decide initial direction rising or falling | ||
| 130 | { | ||
| 131 | my $high; | ||
| 132 | my $high_pos; | ||
| 133 | my $low; | ||
| 134 | my $low_pos; | ||
| 135 | |||
| 136 | foreach my $i (0 .. $hi) { | ||
| 137 | my $value = $p->[$i] // next; | ||
| 138 | my $this_high = $ph->[$i] // $value; | ||
| 139 | my $this_low = $pl->[$i] // $value; | ||
| 140 | |||
| 141 | if (! defined $high || $this_high > $high) { | ||
| 142 | $high = $this_high; | ||
| 143 | $high_pos = $i; | ||
| 144 | } | ||
| 145 | if (! defined $low || $this_low < $low) { | ||
| 146 | $low = $this_low; | ||
| 147 | $low_pos = $i; | ||
| 148 | } | ||
| 149 | |||
| 150 | if ($high >= $low * $factor_increase) { | ||
| 151 | if ($high_pos > $low_pos) { | ||
| 152 | $direction = $rising; | ||
| 153 | $s->[0] = $s->[$low_pos] = $low; | ||
| 154 | last; | ||
| 155 | } | ||
| 156 | if ($low_pos >= $high_pos) { | ||
| 157 | $direction = $falling; | ||
| 158 | $s->[0] = $s->[$high_pos] = $high; | ||
| 159 | last; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | foreach my $i ($lo .. $hi) { | ||
| 166 | my $value = $p->[$i] // next; | ||
| 167 | |||
| 168 | my ($pos, $val) = $direction->($i, | ||
| 169 | $ph->[$i] // $value, | ||
| 170 | $pl->[$i] // $value); | ||
| 171 | if (defined $pos) { | ||
| 172 | $s->[$pos] = $val; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | if ($extreme_pos) { | ||
| 176 | $s->[$extreme_pos] = $s->[$hi] = $extreme; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | 1; | ||
| 182 | __END__ |