| 1 | # Copyright 2008, 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::Midpoint; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use Scalar::Util; | ||
| 24 | use Locale::TextDomain ('App-Chart'); | ||
| 25 | |||
| 26 | use App::Chart::Database; | ||
| 27 | use App::Chart::TZ; | ||
| 28 | use base 'App::Chart::Series::Indicator'; | ||
| 29 | |||
| 30 | sub longname { __('Midpoint (H+L)/2') } | ||
| 31 | sub shortname { __('Mid') } | ||
| 32 | sub manual { __p('manual-node','Midpoint') } | ||
| 33 | |||
| 34 | use constant | ||
| 35 | { type => 'selector', | ||
| 36 | }; | ||
| 37 | |||
| 38 | sub new { | ||
| 39 | my ($class, $parent) = @_; | ||
| 40 | if (! $parent->array('highs') && ! $parent->array('lows')) { | ||
| 41 | return $parent; | ||
| 42 | } | ||
| 43 | return $class->SUPER::new | ||
| 44 | (parent => $parent, | ||
| 45 | arrays => { values => [] }, | ||
| 46 | array_aliases => { }); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub fill_part { | ||
| 50 | my ($self, $lo, $hi) = @_; | ||
| 51 | my $parent = $self->{'parent'}; | ||
| 52 | |||
| 53 | $parent->fill ($lo, $hi); | ||
| 54 | my $closes = $parent->values_array; | ||
| 55 | my $highs = $parent->array('highs') // $closes; | ||
| 56 | my $lows = $parent->array('lows') // $closes; | ||
| 57 | my $s = $self->values_array; | ||
| 58 | |||
| 59 | $hi = min ($hi, $#$closes); | ||
| 60 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 61 | |||
| 62 | foreach my $i ($lo .. $hi) { | ||
| 63 | my $close = $closes->[$i]; | ||
| 64 | if (defined $close) { | ||
| 65 | my $low = $lows->[$i] // $close; | ||
| 66 | my $high = $highs->[$i] // $close; | ||
| 67 | $s->[$i] = ($high + $low) / 2; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | __END__ |