| 1 | # Copyright 2008, 2009, 2010, 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 App::Chart::Series::GT; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use base 'App::Chart::Series'; | ||
| 24 | |||
| 25 | use GT::Prices; | ||
| 26 | use GT::Conf; | ||
| 27 | use GT::Eval; | ||
| 28 | use GT::DateTime; | ||
| 29 | use GT::Tools; | ||
| 30 | |||
| 31 | use constant DEBUG => 0; | ||
| 32 | |||
| 33 | |||
| 34 | my %class_to_timeframe = ('App::Chart::Timebase::Days' => $DAY, | ||
| 35 | 'App::Chart::Timebase::Weeks' => $WEEK, | ||
| 36 | 'App::Chart::Timebase::Months' => $MONTH, | ||
| 37 | 'App::Chart::Timebase::Years' => $YEAR); | ||
| 38 | |||
| 39 | sub new { | ||
| 40 | my ($class, $type, $parent, @args) = @_; | ||
| 41 | if (DEBUG) { local $,=' '; print "GT->new type '$type' args", @args, "\n"; } | ||
| 42 | my $indicator = GT::Eval::create_standard_object ($type, @args); | ||
| 43 | my $nb_values = $indicator->get_nb_values; | ||
| 44 | my $arrays = {}; | ||
| 45 | foreach my $i (0 .. $nb_values-1) { | ||
| 46 | my $name = $indicator->get_name ($i); | ||
| 47 | $arrays->{$name} = []; | ||
| 48 | } | ||
| 49 | my $array_aliases = { values => $indicator->get_name(0) }; | ||
| 50 | if (DEBUG) { require Data::Dumper; | ||
| 51 | print " ",Data::Dumper->Dump([$arrays],['arrays']); | ||
| 52 | print " ",Data::Dumper->Dump([$array_aliases], | ||
| 53 | ['array_aliases']); } | ||
| 54 | |||
| 55 | return $class->SUPER::new (parent => $parent, | ||
| 56 | indicator => $indicator, | ||
| 57 | arrays => $arrays, | ||
| 58 | array_aliases => $array_aliases); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub name { | ||
| 62 | my ($self) = @_; | ||
| 63 | my $indicator = $self->{'indicator'}; | ||
| 64 | return $indicator->get_name; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub fill_part { | ||
| 68 | my ($self, $lo, $hi) = @_; | ||
| 69 | if (DEBUG) { print "GT fill_part $lo $hi\n"; } | ||
| 70 | |||
| 71 | my $parent = $self->{'parent'}; | ||
| 72 | my $timebase = $self->timebase; | ||
| 73 | my $indicator = $self->{'indicator'}; | ||
| 74 | |||
| 75 | require GT::DB::Chart; | ||
| 76 | my $db = GT::DB::Chart->new ($parent, $hi); | ||
| 77 | my $code = $parent->symbol || 'DUMMY-STOCK-CODE'; | ||
| 78 | my $timebase_class = ref $timebase; | ||
| 79 | # ENHANCE-ME: can probably fallback to $DAYS or something for unknown | ||
| 80 | # $timebase_class, not much in the GT indicators depends on the timebase | ||
| 81 | my $timeframe = $class_to_timeframe{$timebase_class} | ||
| 82 | || croak "Timebase class $timebase_class not known to GT"; | ||
| 83 | my $full = 0; | ||
| 84 | my $start = $timebase->to_iso ($lo); | ||
| 85 | my $end = $timebase->to_iso ($hi); | ||
| 86 | my $nb_item = 0; | ||
| 87 | my $max_loaded_items = $hi - $lo + 1 + $indicator->days_required; | ||
| 88 | |||
| 89 | if (DEBUG) { print " find_calculator start=$start end=$end code=$code timeframe=$timeframe max_loaded_items=$max_loaded_items\n"; } | ||
| 90 | my ($calc, $first, $last); | ||
| 91 | |||
| 92 | if (! eval { | ||
| 93 | ($calc, $first, $last) = GT::Tools::find_calculator | ||
| 94 | ($db, $code, $timeframe, $full, $start,$end,$nb_item,$max_loaded_items); | ||
| 95 | 1 | ||
| 96 | }) { | ||
| 97 | if ($@ =~ /No data available/i) { return; } | ||
| 98 | die $@; | ||
| 99 | } | ||
| 100 | if (DEBUG) { print " gives first=$first last=$last\n"; } | ||
| 101 | |||
| 102 | $indicator->calculate_interval ($calc, $first, $last); | ||
| 103 | |||
| 104 | my $nb_values = $indicator->get_nb_values; | ||
| 105 | my $indic_cache = $calc->indicators; # GT::Cache of indicator results | ||
| 106 | my $prices = $calc->prices; # GT::Prices inputs | ||
| 107 | if (DEBUG >= 2) { | ||
| 108 | require Data::Dumper; | ||
| 109 | print " ",Data::Dumper->Dump([$indicator],['indicator']); | ||
| 110 | print " ",Data::Dumper->Dump([$prices],['prices']); | ||
| 111 | print " ",Data::Dumper->Dump([$indic_cache],['indic_cache']); | ||
| 112 | } | ||
| 113 | |||
| 114 | for (my $n = 0; $n < $nb_values; $n++) { | ||
| 115 | my $name = $indicator->get_name($n); | ||
| 116 | my $array = $self->{'arrays'}->{$name}; | ||
| 117 | |||
| 118 | # reverse to pre-extend $array | ||
| 119 | for (my $i = $last; $i >= $first; $i--) { | ||
| 120 | my $value = $indic_cache->get($name, $i) // next; | ||
| 121 | my $t = $prices->at($i)->[$GT::DB::Chart::DATE_T]; | ||
| 122 | $array->[$t] = $value; | ||
| 123 | if (DEBUG >= 2) { print " i=$i t=$t value=$value\n"; } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | if (DEBUG >= 2) { print "\n"; } | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; | ||
| 130 | __END__ |