| 1 | # Copyright 2008, 2009, 2011 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::WeightedClose; | ||
| 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 | |||
| 31 | sub longname { __('Weighted Close (H+L+2*C)/4') } | ||
| 32 | sub shortname { __('WCL') } | ||
| 33 | sub manual { __p('manual-node','Weighted Close') } | ||
| 34 | |||
| 35 | use constant | ||
| 36 | { type => 'selector', | ||
| 37 | }; | ||
| 38 | |||
| 39 | sub new { | ||
| 40 | my ($class, $parent) = @_; | ||
| 41 | if (! $parent->array('highs') && ! $parent->array('lows')) { | ||
| 42 | return $parent; | ||
| 43 | } | ||
| 44 | return $class->SUPER::new | ||
| 45 | (parent => $parent, | ||
| 46 | arrays => { values => [] }, | ||
| 47 | array_aliases => { }); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub fill_part { | ||
| 51 | my ($self, $lo, $hi) = @_; | ||
| 52 | my $parent = $self->{'parent'}; | ||
| 53 | |||
| 54 | $parent->fill ($lo, $hi); | ||
| 55 | my $closes = $parent->values_array; | ||
| 56 | my $highs = $parent->array('highs') || $closes; | ||
| 57 | my $lows = $parent->array('lows') || $closes; | ||
| 58 | my $s = $self->values_array; | ||
| 59 | |||
| 60 | $hi = min ($hi, $#$closes); | ||
| 61 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 62 | |||
| 63 | foreach my $i ($lo .. $hi) { | ||
| 64 | my $close = $closes->[$i] // next; | ||
| 65 | |||
| 66 | # mean (H+L+2*C)/4 | ||
| 67 | $s->[$i] = (($lows->[$i] // $close) | ||
| 68 | + ($highs->[$i] // $close) | ||
| 69 | + 2 * $close) / 4; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | __END__ |