| 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::Collapse; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | # use Locale::TextDomain ('App-Chart'); | ||
| 23 | |||
| 24 | use App::Chart; | ||
| 25 | use base 'App::Chart::Series'; | ||
| 26 | |||
| 27 | # uncomment this to run the ### lines | ||
| 28 | #use Smart::Comments; | ||
| 29 | |||
| 30 | use constant DEBUG => 0; | ||
| 31 | |||
| 32 | |||
| 33 | use constant { longname => 'Timebase Collapse', | ||
| 34 | shortname => 'Collapse', | ||
| 35 | manual => undef, | ||
| 36 | type => 'special', | ||
| 37 | }; | ||
| 38 | |||
| 39 | |||
| 40 | sub new { | ||
| 41 | my ($class, $parent, $timebase_class) = @_; | ||
| 42 | |||
| 43 | if ($timebase_class !~ /::/) { | ||
| 44 | $timebase_class = "App::Chart::Timebase::\u$timebase_class"; | ||
| 45 | } | ||
| 46 | require Module::Load; | ||
| 47 | Module::Load::load ($timebase_class); | ||
| 48 | |||
| 49 | my $parent_timebase = $parent->timebase; | ||
| 50 | my $new_timebase = $timebase_class->new_from_timebase ($parent_timebase); | ||
| 51 | my $self = $class->SUPER::new (parent => $parent, | ||
| 52 | timebase => $new_timebase, | ||
| 53 | arrays => { map {; ($_ => []) } | ||
| 54 | $parent->array_names }, | ||
| 55 | array_aliases => $parent->{'array_aliases'}); | ||
| 56 | return $self; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub name { | ||
| 60 | my ($self) = @_; | ||
| 61 | if (exists $self->{'name'}) { | ||
| 62 | return $self->{'name'} | ||
| 63 | } else { | ||
| 64 | my $timebase = $self->timebase; | ||
| 65 | my $parent = $self->{'parent'}; | ||
| 66 | return ($self->{'name'} | ||
| 67 | = join (' - ', $parent->name||'', $timebase->adjective)); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub hi { | ||
| 72 | my ($self) = @_; | ||
| 73 | my $self_timebase = $self->{'timebase'}; | ||
| 74 | my $parent = $self->{'parent'}; | ||
| 75 | my $parent_timebase = $parent->timebase; | ||
| 76 | return $self_timebase->convert_from_ceil ($parent_timebase, $parent->hi); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub range_default_names { | ||
| 80 | my ($self) = @_; | ||
| 81 | return $self->{'parent'}->range_default_names; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub fill_part { | ||
| 85 | my ($self, $lo, $hi) = @_; | ||
| 86 | ### Collapse fill_part: "$lo $hi" | ||
| 87 | |||
| 88 | my $timebase = $self->timebase; | ||
| 89 | my $parent = $self->{'parent'}; | ||
| 90 | my $parent_timebase = $parent->timebase; | ||
| 91 | |||
| 92 | my $p_lo = $parent_timebase->convert_from_floor ($timebase, $lo); | ||
| 93 | my $p_hi = $parent_timebase->convert_from_ceil ($timebase, $hi); | ||
| 94 | ### parent has: "lo=$p_lo hi=$p_hi" | ||
| 95 | |||
| 96 | $parent->fill ($p_lo, $p_hi); | ||
| 97 | my $p_opens = $parent->array('opens') || []; | ||
| 98 | my $p_highs = $parent->array('highs'); | ||
| 99 | my $p_lows = $parent->array('lows'); | ||
| 100 | my $p_values = $parent->values_array; | ||
| 101 | my $p_volumes = $parent->array('volumes'); | ||
| 102 | my $p_openints = $parent->array('openints'); | ||
| 103 | |||
| 104 | my $s_opens = $self->array('opens'); | ||
| 105 | my $s_highs = $self->array('highs'); | ||
| 106 | my $s_lows = $self->array('lows'); | ||
| 107 | my $s_values = $self->values_array; | ||
| 108 | my $s_volumes = $self->array('volumes'); | ||
| 109 | my $s_openints = $self->array('openints'); | ||
| 110 | |||
| 111 | my $p_t = $p_lo; | ||
| 112 | for (my $t = $lo; $t <= $hi; $t++) { | ||
| 113 | my $p_t_next = $parent_timebase->convert_from_floor ($timebase, $t + 1); | ||
| 114 | my $p_end = $p_t_next - 1; | ||
| 115 | if (DEBUG) { print " at t=$t p_t=$p_t p_end=$p_end\n"; } | ||
| 116 | |||
| 117 | if ($s_opens) { | ||
| 118 | # Open is first day with an open or a close. Look at the close in case | ||
| 119 | # a closes-only series as happens for various indexes and sources. | ||
| 120 | |||
| 121 | if (DEBUG) { local $,=' '; | ||
| 122 | print " p_opens", @{$p_opens}[$p_t..$p_end], "\n"; } | ||
| 123 | foreach my $i ($p_t .. $p_end) { | ||
| 124 | if (defined $p_opens->[$i]) { | ||
| 125 | $s_opens->[$t] = $p_opens->[$i]; | ||
| 126 | last; | ||
| 127 | } | ||
| 128 | if (defined $p_values->[$i]) { | ||
| 129 | $s_values->[$t] = $p_values->[$i]; | ||
| 130 | last; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | # some data sources only give closes, so include them in the high/low | ||
| 136 | # calc; likewise maybe some data sources might only offer opens+closes, | ||
| 137 | # so include the opens | ||
| 138 | # | ||
| 139 | if ($s_highs) { | ||
| 140 | $s_highs->[$t] = App::Chart::max_maybe (@$p_highs [$p_t .. $p_end], | ||
| 141 | @$p_opens [$p_t .. $p_end], | ||
| 142 | @$p_values[$p_t .. $p_end]); | ||
| 143 | } | ||
| 144 | if ($s_lows) { | ||
| 145 | $s_lows->[$t] = App::Chart::min_maybe (@$p_lows [$p_t .. $p_end], | ||
| 146 | @$p_opens [$p_t .. $p_end], | ||
| 147 | @$p_values[$p_t .. $p_end]); | ||
| 148 | } | ||
| 149 | |||
| 150 | # FIXME: treat other named arrays likewise | ||
| 151 | # | ||
| 152 | # close is the last close in the period | ||
| 153 | for (my $i = $p_end; $i >= $p_t; $i--) { | ||
| 154 | if (defined $p_values->[$i]) { | ||
| 155 | $s_values->[$t] = $p_values->[$i]; | ||
| 156 | last; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | # volume is the total in the period, or undef if in period are undef | ||
| 161 | # note List::Util::sum() unhelpfully returns empty string for no args | ||
| 162 | if ($s_volumes) { | ||
| 163 | my @volumes = grep {defined} @$p_volumes[$p_t .. $p_end]; | ||
| 164 | if (@volumes) { | ||
| 165 | $s_volumes->[$t] = List::Util::sum (@volumes); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | # openint i the last openint in the period | ||
| 170 | # this might be on a different day than the final close ... | ||
| 171 | if ($s_openints) { | ||
| 172 | for (my $i = $p_end; $i >= $p_t; $i--) { | ||
| 173 | if (defined $p_openints->[$i]) { | ||
| 174 | $s_openints->[$t] = $p_openints->[$i]; | ||
| 175 | last; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | $p_t = $p_t_next; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | 1; | ||
| 186 | __END__ |