| 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::ParabolicSAR; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use Locale::TextDomain 1.17; # for __p() | ||
| 24 | use Locale::TextDomain ('App-Chart'); | ||
| 25 | |||
| 26 | use base 'App::Chart::Series::Indicator'; | ||
| 27 | use App::Chart::Series::Derived::MFI; | ||
| 28 | use App::Chart::Series::Derived::TrueRange; | ||
| 29 | |||
| 30 | |||
| 31 | sub longname { __('Parabolic SAR') } | ||
| 32 | sub shortname { __('SAR') } | ||
| 33 | sub manual { __p('manual-node','Parabolic SAR') } | ||
| 34 | |||
| 35 | use constant | ||
| 36 | { type => 'average', | ||
| 37 | parameter_info => [ { name => __('Initial accel'), | ||
| 38 | key => 'parabolic_sar_initial_accel', | ||
| 39 | type => 'float', | ||
| 40 | decimals => 2, | ||
| 41 | minimum => 0, | ||
| 42 | maximum => 1, | ||
| 43 | default => 0.02, | ||
| 44 | step => 0.01 }, | ||
| 45 | { name => __('Max accel'), | ||
| 46 | key => 'parabolic_sar_max_accel', | ||
| 47 | type => 'float', | ||
| 48 | decimals => 2, | ||
| 49 | minimum => 0, | ||
| 50 | maximum => 1, | ||
| 51 | default => 0.2, | ||
| 52 | step => 0.1 } ], | ||
| 53 | default_linestyle => 'Stops', | ||
| 54 | }; | ||
| 55 | |||
| 56 | sub new { | ||
| 57 | my ($class, $parent, $initial_accel, $max_accel) = @_; | ||
| 58 | |||
| 59 | $initial_accel //= parameter_info()->[0]->{'default'}; | ||
| 60 | # ($initial_accel >= 0) || croak "ParabolicSAR bad initial accel: $initial_accel"; | ||
| 61 | |||
| 62 | $max_accel //= parameter_info()->[0]->{'default'}; | ||
| 63 | # ($max_accel >= 0) || croak "ParabolicSAR bad max accel: $max_accel"; | ||
| 64 | |||
| 65 | return $class->SUPER::new | ||
| 66 | (parent => $parent, | ||
| 67 | parameters => [ $initial_accel, $max_accel ], | ||
| 68 | arrays => { values => [] }, | ||
| 69 | hi => $parent->hi + 1, | ||
| 70 | array_aliases => { values => 'high' }); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub fill_part { | ||
| 74 | my ($self, $lo, $hi) = @_; | ||
| 75 | my ($initial_accel, $max_accel) = @{$self->{'parameters'}}; | ||
| 76 | |||
| 77 | my $parent = $self->{'parent'}; | ||
| 78 | $lo = 0; | ||
| 79 | $hi = $parent->hi; | ||
| 80 | |||
| 81 | $parent->fill ($lo, $hi); | ||
| 82 | my $p = $parent->values_array; | ||
| 83 | my $ph = $parent->array('highs') || $p; | ||
| 84 | my $pl = $parent->array('lows') || $p; | ||
| 85 | |||
| 86 | my $s = $self->values_array; | ||
| 87 | $hi = min ($hi, $#$p); | ||
| 88 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 89 | |||
| 90 | my ($extreme, $sar, $accel); | ||
| 91 | my ($rising, $falling, $proc); | ||
| 92 | my ($high, $low); | ||
| 93 | |||
| 94 | $rising = sub { | ||
| 95 | if ($high > $extreme) { | ||
| 96 | $extreme = $high; | ||
| 97 | $accel = min ($max_accel, $accel + $initial_accel); | ||
| 98 | } | ||
| 99 | |||
| 100 | $sar += $accel * ($extreme - $sar); | ||
| 101 | |||
| 102 | # if SAR penetrated, reverse | ||
| 103 | if ($low <= $sar) { | ||
| 104 | $sar = $extreme; | ||
| 105 | $extreme = $low; | ||
| 106 | $accel = $initial_accel; | ||
| 107 | $proc = $falling; | ||
| 108 | } | ||
| 109 | }; | ||
| 110 | |||
| 111 | $falling = sub { | ||
| 112 | if ($low < $extreme) { | ||
| 113 | $extreme = $low; | ||
| 114 | $accel = min ($max_accel, $accel + $initial_accel); | ||
| 115 | } | ||
| 116 | |||
| 117 | $sar += $accel * ($extreme - $sar); | ||
| 118 | |||
| 119 | # if SAR penetrated, reverse | ||
| 120 | if ($high >= $sar) { | ||
| 121 | $sar = $extreme; | ||
| 122 | $extreme = $high; | ||
| 123 | $accel = $initial_accel; | ||
| 124 | $proc = $rising; | ||
| 125 | } | ||
| 126 | }; | ||
| 127 | |||
| 128 | $proc = sub { | ||
| 129 | $extreme = $high; | ||
| 130 | $accel = $initial_accel; | ||
| 131 | $sar = $low; | ||
| 132 | $proc = $rising; | ||
| 133 | }; | ||
| 134 | |||
| 135 | foreach my $i ($lo .. $hi) { | ||
| 136 | my $value = $p->[$i] // next; | ||
| 137 | $high = $ph->[$i] // $value; | ||
| 138 | $low = $pl->[$i] // $value; | ||
| 139 | $proc->($ph->[$i], $pl->[$i], $value); | ||
| 140 | $s->[$i] = $sar; | ||
| 141 | } | ||
| 142 | |||
| 143 | # prospective stop one beyond parent series | ||
| 144 | $sar += $accel * ($extreme - $sar); | ||
| 145 | $s->[$hi+1] = $sar; | ||
| 146 | } | ||
| 147 | |||
| 148 | 1; | ||
| 149 | __END__ |