| 1 | # Copyright 2008, 2009, 2010, 2011, 2012, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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 = 269; | ||
| 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__ | ||
| 142 | |||
| 143 | =for stopwords GeniusTrader Ryde | ||
| 144 | |||
| 145 | =head1 NAME | ||
| 146 | |||
| 147 | GT::DB::Chart - GeniusTrader access to data from Chart | ||
| 148 | |||
| 149 | =head1 SYNOPSIS | ||
| 150 | |||
| 151 | use GT::DB::Chart; | ||
| 152 | my $db = GT::DB::Chart->new; | ||
| 153 | my $prices = $db->get_prices ('BHP.AX', $GT::Prices::DAYS); | ||
| 154 | |||
| 155 | =head1 DESCRIPTION | ||
| 156 | |||
| 157 | This is a C<GT::DB> module giving access to the Chart database from | ||
| 158 | GeniusTrader scripts and calculations. | ||
| 159 | |||
| 160 | =head1 FUNCTIONS | ||
| 161 | |||
| 162 | =over 4 | ||
| 163 | |||
| 164 | =item C<< $db = GT::DB::Chart->new() >> | ||
| 165 | |||
| 166 | Create and return a new C<GT::DB::Chart> object to retrieve data from | ||
| 167 | the Chart database (F<~/Chart/database.sqdb>). | ||
| 168 | |||
| 169 | =item C<< $db->disconnect() >> | ||
| 170 | |||
| 171 | Disconnect from the Chart database. | ||
| 172 | |||
| 173 | =item C<< $prices = $db->get_prices ($symbol, $timeframe) >> | ||
| 174 | |||
| 175 | =item C<< $prices = $db->get_last_prices ($symbol, $limit, $timeframe) >> | ||
| 176 | |||
| 177 | Create and return a C<GT::Prices> object with the data for C<$symbol> in the | ||
| 178 | given C<$timeframe> increments. C<$timeframe> can be C<$DAYS>, C<$WEEKS>, | ||
| 179 | C<$MONTHS> or C<$YEARS>. | ||
| 180 | |||
| 181 | C<get_prices()> returns all available data for C<$symbol>, | ||
| 182 | C<get_last_prices()> returns only the most recent C<$limit> many values (or | ||
| 183 | as many as available). For example to get the last 250 trading days, | ||
| 184 | |||
| 185 | my $prices = $db->get_last_prices ('GM', 250, | ||
| 186 | $GT::Prices::DAYS); | ||
| 187 | |||
| 188 | =item C<< $str = $db->get_db_name ($symbol) >> | ||
| 189 | |||
| 190 | Return the company name for the stock C<$symbol>, or C<undef> if unknown. | ||
| 191 | |||
| 192 | For most applications use C<< $db->get_name() >> instead (see C<GT::DB>), | ||
| 193 | since it tries your F<~/.gt/sharenames> file if nothing from | ||
| 194 | C<get_db_name()>. | ||
| 195 | |||
| 196 | =back | ||
| 197 | |||
| 198 | =head1 SEE ALSO | ||
| 199 | |||
| 200 | L<GT::DB>, L<GT::Prices> | ||
| 201 | |||
| 202 | L<App::Chart::Series::Database> | ||
| 203 | |||
| 204 | =head1 HOME PAGE | ||
| 205 | |||
| 206 | L<http://user42.tuxfamily.org/chart/index.html> | ||
| 207 | |||
| 208 | =head1 LICENCE | ||
| 209 | |||
| 210 | Copyright 2008, 2009, 2010, 2011, 2012, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Kevin Ryde | ||
| 211 | |||
| 212 | Chart is free software; you can redistribute it and/or modify it under the | ||
| 213 | terms of the GNU General Public License as published by the Free Software | ||
| 214 | Foundation; either version 3, or (at your option) any later version. | ||
| 215 | |||
| 216 | Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 217 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 218 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 219 | details. | ||
| 220 | |||
| 221 | You should have received a copy of the GNU General Public License along with | ||
| 222 | Chart; see the file F<COPYING>. Failing that, see | ||
| 223 | L<http://www.gnu.org/licenses/>. | ||
| 224 | |||
| 225 | =cut |