| 1 | # Copyright 2007, 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::Volume; | ||
| 18 | use 5.008; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use Locale::TextDomain ('App-Chart'); | ||
| 24 | use base 'App::Chart::Series::Indicator'; | ||
| 25 | |||
| 26 | use constant DEBUG => 0; | ||
| 27 | |||
| 28 | sub longname { __('Volume') } | ||
| 29 | *shortname = \&longname; | ||
| 30 | # FIXME | ||
| 31 | # sub manual { __p('manual-node','Volume and Open Interest') } | ||
| 32 | |||
| 33 | use constant | ||
| 34 | { type => 'indicator', | ||
| 35 | units => 'volume', | ||
| 36 | priority => 10, | ||
| 37 | minimum => 0, | ||
| 38 | decimals => 0, # no decimals normally | ||
| 39 | default_linestyle => 'Bars', | ||
| 40 | }; | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my ($class, $parent) = @_; | ||
| 44 | |||
| 45 | my $p = $parent->array('volumes') | ||
| 46 | || croak "No volumes in series '",$parent->name,"'"; | ||
| 47 | return $class->SUPER::new (parent => $parent, | ||
| 48 | arrays => { values => $p }); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub fill_part { | ||
| 52 | my ($self, $lo, $hi) = @_; | ||
| 53 | my $parent = $self->{'parent'}; | ||
| 54 | $parent->fill_part ($lo, $hi); | ||
| 55 | } | ||
| 56 | |||
| 57 | # Return (LOWER UPPER) which is a suggested initial Y-axis page range to | ||
| 58 | # show for dates LO to HI. This is for use with trading volumes and also | ||
| 59 | # open-interest. | ||
| 60 | # | ||
| 61 | # The page size is meant to keep average to high volume days visible in the | ||
| 62 | # window, but leave abnormally high days off the top of the screen so as not | ||
| 63 | # to make the average ones come out tiny. | ||
| 64 | # | ||
| 65 | # The rule is to look for the 98% fractile, meaning a level which covers 98% | ||
| 66 | # of the values in the LO to HI range, then an extra 1.5x that much. Making | ||
| 67 | # UPPER just the 0.98 fractile alone would guarantee two or three points off | ||
| 68 | # the top of the window even if they're only modestly bigger than the | ||
| 69 | # average, hence going up to 1.5x that point. | ||
| 70 | # | ||
| 71 | # | ||
| 72 | # ENHANCE-ME: If LO to HI is small then maybe the 98% should be reduced a | ||
| 73 | # bit. If the width is less than 50 (or is it 25?) dates then 98% ends up | ||
| 74 | # meaning every data point. | ||
| 75 | # | ||
| 76 | sub initial_range { | ||
| 77 | my ($self, $lo, $hi) = @_; | ||
| 78 | if (DEBUG) { print "Volume initial range\n"; } | ||
| 79 | $lo = max ($lo, 0); | ||
| 80 | $hi = max ($hi, 0); | ||
| 81 | |||
| 82 | $self->fill ($lo, $hi); | ||
| 83 | my $values = $self->values_array; | ||
| 84 | my @v = grep{defined} @{$values}[$lo..$hi]; | ||
| 85 | |||
| 86 | if (defined (my $symbol = $self->symbol)) { | ||
| 87 | my $latest = App::Chart::Latest->get($symbol); | ||
| 88 | my $timebase = $self->timebase; | ||
| 89 | if (defined $latest->{'volume'} | ||
| 90 | && defined (my $last_iso = $latest->{'last_date'})) { | ||
| 91 | my $last_t = $timebase->from_iso_floor ($last_iso); | ||
| 92 | if ($last_t >= $lo && $last_t <= $hi) { | ||
| 93 | push @v, $latest->{'volume'}; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | if (! @v) { | ||
| 99 | return; # no non-undef values at all | ||
| 100 | } | ||
| 101 | |||
| 102 | # ENHANCE-ME: the top few values can probably be found without a full sort | ||
| 103 | my $lastval = $v[-1]; | ||
| 104 | @v = sort {$a <=> $b} @v; | ||
| 105 | |||
| 106 | return (0, max (0, | ||
| 107 | $lastval, # always fit last value | ||
| 108 | $v[$#v * 0.98], # fractile | ||
| 109 | 1.5 * List::Util::sum (@v) / scalar @v)); # 1.5*mean | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; | ||
| 113 | __END__ |