| 1 | # Copyright 2008, 2009, 2010, 2011, 2012, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2023, 2024 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 GT::DB::Chart; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util; | ||
| 23 | |||
| 24 | # don't "use base" since GT::DB as of 2010 doesn't have a $VERSION and | ||
| 25 | # base.pm will warn and assign $GT::DB::VERSION=-1 | ||
| 26 | use GT::DB; | ||
| 27 | our @ISA = ('GT::DB'); | ||
| 28 | |||
| 29 | use GT::Prices; | ||
| 30 | use GT::Conf; | ||
| 31 | use GT::DateTime; | ||
| 32 | |||
| 33 | # uncomment this to run the ### lines | ||
| 34 | #use Smart::Comments; | ||
| 35 | |||
| 36 | our $VERSION = 275; | ||
| 37 | |||
| 38 | # extra appended to GT::Prices elements giving the tdate etc corresponding | ||
| 39 | # to the $DATE element | ||
| 40 | our $DATE_T = List::Util::max ($OPEN, $HIGH, $LOW, $CLOSE, $VOLUME, $DATE) + 1; | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my ($class, $series, $hi) = @_; | ||
| 44 | return bless { series => $series, | ||
| 45 | hi => $hi, | ||
| 46 | }, $class; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub disconnect { | ||
| 50 | my ($self) = @_; | ||
| 51 | if (App::Chart::DBI->can('disconnect')) { # if loaded | ||
| 52 | App::Chart::DBI->disconnect; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | sub has_code { | ||
| 57 | my ($self, $symbol) = @_; | ||
| 58 | ### GT-DB-Chart has_code(): $symbol | ||
| 59 | require App::Chart::Database; | ||
| 60 | return App::Chart::Database->symbol_exists ($symbol); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub get_db_name { | ||
| 64 | my ($self, $symbol) = @_; | ||
| 65 | ### GT-DB-Chart get_db_name(): $symbol | ||
| 66 | require App::Chart::Database; | ||
| 67 | return App::Chart::Database->symbol_name ($symbol); | ||
| 68 | } | ||
| 69 | |||
| 70 | my %timeframe_to_class = ($WEEK => 'Weeks', | ||
| 71 | $MONTH => 'Months', | ||
| 72 | $YEAR => 'Years'); | ||
| 73 | |||
| 74 | sub get_prices { | ||
| 75 | my ($self, $symbol, $timeframe) = @_; | ||
| 76 | return $self->get_last_prices ($symbol, -1, $timeframe); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub get_last_prices { | ||
| 80 | my ($self, $symbol, $limit, $timeframe) = @_; | ||
| 81 | ### GT-DB-Chart get_last_prices(): $symbol | ||
| 82 | ### $limit | ||
| 83 | ### $timeframe | ||
| 84 | |||
| 85 | $timeframe ||= $DAY; | ||
| 86 | my $prices = GT::Prices->new; | ||
| 87 | $prices->set_timeframe ($timeframe); | ||
| 88 | |||
| 89 | if ($timeframe < $DAY) { | ||
| 90 | ### no intraday data | ||
| 91 | return $prices; | ||
| 92 | } | ||
| 93 | if ($limit == 0) { | ||
| 94 | return $prices; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $series = $self->{'series'} || do { | ||
| 98 | require App::Chart::Series::Database; | ||
| 99 | App::Chart::Series::Database->new ($symbol); | ||
| 100 | }; | ||
| 101 | if ($timeframe != $DAY) { | ||
| 102 | # can leave this to GT::Tools too, maybe | ||
| 103 | my $class = $timeframe_to_class{$timeframe} | ||
| 104 | or croak __PACKAGE__.": unrecognised timeframe $timeframe"; | ||
| 105 | $series = $series->collapse ($class); | ||
| 106 | } | ||
| 107 | |||
| 108 | my $hi = $self->{'hi'} // $series->hi; | ||
| 109 | my $lo; | ||
| 110 | if ($limit == -1) { | ||
| 111 | $lo = 0; # all data | ||
| 112 | } else { | ||
| 113 | # the newest $limit many values | ||
| 114 | $lo = $series->find_before ($hi, $limit-1); | ||
| 115 | } | ||
| 116 | |||
| 117 | $series->fill ($lo, $hi); | ||
| 118 | my $opens = $series->array('opens') || []; | ||
| 119 | my $highs = $series->array('highs') || []; | ||
| 120 | my $lows = $series->array('lows') || []; | ||
| 121 | my $closes = $series->array('closes') || $series->values_array; | ||
| 122 | my $volumes = $series->array('volumes') || []; | ||
| 123 | my $timebase = $series->timebase; | ||
| 124 | |||
| 125 | foreach my $t ($lo .. $hi) { | ||
| 126 | $closes->[$t] // next; | ||
| 127 | my @elem; | ||
| 128 | $elem[$DATE_T] = $t; | ||
| 129 | $elem[$OPEN] = $opens->[$t] || $closes->[$t]; | ||
| 130 | $elem[$HIGH] = $highs->[$t] || $closes->[$t]; | ||
| 131 | $elem[$LOW] = $lows->[$t] || $closes->[$t]; | ||
| 132 | $elem[$CLOSE] = $closes->[$t]; | ||
| 133 | $elem[$VOLUME] = $volumes->[$t] || 0; | ||
| 134 | $elem[$DATE] = $timebase->to_iso($t); | ||
| 135 | $prices->add_prices (\@elem); # added in ascending date order | ||
| 136 | } | ||
| 137 | return $prices; | ||
| 138 | } | ||
| 139 | |||
| 140 | 1; | ||
| 141 | __END__ |