| 1 | # gone away ? | ||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | # Ghana Stock Exchange setups. | ||
| 6 | |||
| 7 | # Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014, 2016 Kevin Ryde | ||
| 8 | |||
| 9 | # This file is part of Chart. | ||
| 10 | # | ||
| 11 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 12 | # terms of the GNU General Public License as published by the Free Software | ||
| 13 | # Foundation; either version 3, or (at your option) any later version. | ||
| 14 | # | ||
| 15 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 16 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 17 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 18 | # details. | ||
| 19 | # | ||
| 20 | # You should have received a copy of the GNU General Public License along | ||
| 21 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 22 | |||
| 23 | |||
| 24 | # company profile | ||
| 25 | # http://www.gse.com.gh/listedcomps.php?cmd=cmppro&scd=CAL | ||
| 26 | # | ||
| 27 | |||
| 28 | package App::Chart::Suffix::GHA; | ||
| 29 | use 5.010; | ||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | use List::Util qw(min max); | ||
| 33 | use URI::Escape; | ||
| 34 | use Locale::TextDomain ('App-Chart'); | ||
| 35 | |||
| 36 | use App::Chart; | ||
| 37 | use App::Chart::Database; | ||
| 38 | use App::Chart::Download; | ||
| 39 | use App::Chart::DownloadHandler; | ||
| 40 | use App::Chart::DownloadHandler::IndivInfo; | ||
| 41 | use App::Chart::Latest; | ||
| 42 | use App::Chart::Sympred; | ||
| 43 | use App::Chart::TZ; | ||
| 44 | use App::Chart::Weblink; | ||
| 45 | |||
| 46 | |||
| 47 | # FIXME: Accra is GMT with no daylight savings, so Time::TZ->tz_known() | ||
| 48 | # can't tell if it's ok | ||
| 49 | my $timezone_accra = App::Chart::TZ->new | ||
| 50 | (name => __('Accra'), | ||
| 51 | tz => 'Africa/Accra'); | ||
| 52 | |||
| 53 | my $pred = App::Chart::Sympred::Suffix->new ('.GHA'); | ||
| 54 | $timezone_accra->setup_for_symbol ($pred); | ||
| 55 | |||
| 56 | # App::Chart::setup_source_help | ||
| 57 | # ($pred, __p('manual-node','Ghana Stock Exchange')); | ||
| 58 | |||
| 59 | |||
| 60 | #------------------------------------------------------------------------------ | ||
| 61 | # weblink - company info | ||
| 62 | # | ||
| 63 | # Eg. http://www.gse.com.gh/index1.php?linkid=46&scd=CAL | ||
| 64 | |||
| 65 | App::Chart::Weblink->new | ||
| 66 | (pred => $pred, | ||
| 67 | name => __('GSE _Company Information'), | ||
| 68 | desc => __('Open web browser at the Ghana Stock Exchange information page for this company'), | ||
| 69 | proc => sub { | ||
| 70 | my ($symbol) = @_; | ||
| 71 | return 'http://www.gse.com.gh/index1.php?linkid=46&scd=' | ||
| 72 | . URI::Escape::uri_escape (App::Chart::symbol_sans_suffix ($symbol)); | ||
| 73 | }); | ||
| 74 | |||
| 75 | |||
| 76 | #----------------------------------------------------------------------------- | ||
| 77 | # daily data page | ||
| 78 | # | ||
| 79 | # This uses the daily data page, | ||
| 80 | # | ||
| 81 | use constant GSE_DAILY_URL => | ||
| 82 | 'http://www.gse.com.gh/index1.php?linkid=5&sublinkid=12'; | ||
| 83 | |||
| 84 | App::Chart::DownloadHandler->new | ||
| 85 | (name => __('GSE'), | ||
| 86 | pred => $pred, | ||
| 87 | proc => \&daily_download, | ||
| 88 | available_date_time => \&daily_available_date_time); | ||
| 89 | |||
| 90 | # Return date and time strings for the available daily report. | ||
| 91 | # | ||
| 92 | # Trading is from 10am to about midday, not sure when the new day's data | ||
| 93 | # will appear, have seen it still not available after midnight on the | ||
| 94 | # trading day, so let's try from 9am the following day. | ||
| 95 | # | ||
| 96 | sub daily_available_date_time { | ||
| 97 | return (App::Chart::Download::weekday_date_after_time | ||
| 98 | (9,0, $timezone_accra, -1), | ||
| 99 | '09:00:00'); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub daily_download { | ||
| 103 | my ($symbol_list) = @_; | ||
| 104 | App::Chart::Download::status (__('GSE latest daily data')); | ||
| 105 | my $newest_resp = App::Chart::Download->get (GSE_DAILY_URL); | ||
| 106 | my $newest_h = daily_parse ($newest_resp); | ||
| 107 | App::Chart::Download::write_latest_group ($newest_h); | ||
| 108 | |||
| 109 | my $start_tdate = App::Chart::Download::start_tdate_for_update(@$symbol_list); | ||
| 110 | my $newest_tdate = App::Chart::Download::iso_to_tdate_floor | ||
| 111 | ($newest_h->{'data'}->[0]->{'date'}); | ||
| 112 | if ($newest_tdate < $start_tdate) { return; } | ||
| 113 | |||
| 114 | foreach my $session (daily_sessions ($newest_resp)) { | ||
| 115 | my ($code, $tdate) = @$session; | ||
| 116 | if ($tdate < $start_tdate) { next; } | ||
| 117 | |||
| 118 | App::Chart::Download::status | ||
| 119 | (__x('GSE data {date}', | ||
| 120 | date => App::Chart::Download::tdate_range_string ($tdate))); | ||
| 121 | my $resp = App::Chart::Download->get (GSE_DAILY_URL . "?session=$code"); | ||
| 122 | |||
| 123 | my $h = daily_parse ($resp); | ||
| 124 | App::Chart::Download::write_daily_group ($h); | ||
| 125 | } | ||
| 126 | App::Chart::Download::write_daily_group ($newest_h); | ||
| 127 | } | ||
| 128 | |||
| 129 | # return values ([$code,$tdate],...) which are the available sessions, by | ||
| 130 | # session code and tdate, sorted from oldest to newest tdate | ||
| 131 | sub daily_sessions { | ||
| 132 | my ($resp) = @_; | ||
| 133 | my $content = $resp->decoded_content(raise_error=>1); | ||
| 134 | my @ret = (); | ||
| 135 | # Eg. <option value="2425"> 18/01/2007 </option> | ||
| 136 | while ($content =~ m{<option +value=\"([0-9]+)\"> *([0-9]+/[0-9]+/[0-9][0-9][0-9][0-9]) *</option>}g) { | ||
| 137 | my $code = $1; | ||
| 138 | my $tdate = App::Chart::ymd_to_tdate_floor | ||
| 139 | (Date::Calc::Decode_Date_EU ($2)); # d/m/y | ||
| 140 | push @ret, [$code, $tdate]; | ||
| 141 | } | ||
| 142 | return sort {$a->[1] <=> $b->[1]} @ret; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub daily_parse { | ||
| 146 | my ($resp) = @_; | ||
| 147 | my $content = $resp->decoded_content(raise_error=>1); | ||
| 148 | |||
| 149 | my @data = (); | ||
| 150 | my $h = { source => __PACKAGE__, | ||
| 151 | url => GSE_DAILY_URL, | ||
| 152 | resp => $resp, | ||
| 153 | currency => 'GHC', | ||
| 154 | date_format => 'dmy', | ||
| 155 | data => \@data }; | ||
| 156 | |||
| 157 | # Eg. "24/04/2006\n\t\t</font> Trading Results" | ||
| 158 | $content =~ m{([0-9]+/[0-9]+/[0-9][0-9][0-9][0-9])([ \t\n]*(<[^>]*>| ))*[ \t\n]*Trading Results} | ||
| 159 | or die "GSE: daily page date not found\n"; | ||
| 160 | my $date = $1; | ||
| 161 | |||
| 162 | require HTML::TableExtract; | ||
| 163 | my $te = HTML::TableExtract->new | ||
| 164 | (headers => ['', # no heading above symbols | ||
| 165 | 'ISIN', 'Bid', 'Offer', 'Opening', 'Closing', 'Change', | ||
| 166 | 'Traded' ]); | ||
| 167 | $te->parse($content); | ||
| 168 | if (! $te->tables) { die "GSE: daily page price table not found\n"; } | ||
| 169 | |||
| 170 | foreach my $row ($te->rows) { | ||
| 171 | my ($symbol, $isin, $bid, $offer, $open, $close, $change, $volume) = @$row; | ||
| 172 | $symbol =~ s/^ +//; # lose leading space | ||
| 173 | |||
| 174 | # sub-heading | ||
| 175 | if ($symbol =~ /First List/) { next; } | ||
| 176 | |||
| 177 | # 18jan07 had an empty row, delete that based on empty symbol field | ||
| 178 | if ($symbol eq '') { next; } | ||
| 179 | |||
| 180 | push @data, { symbol => $symbol . '.GHA', | ||
| 181 | isin => $isin, | ||
| 182 | date => $date, | ||
| 183 | bid => $bid, | ||
| 184 | offer => $offer, | ||
| 185 | open => $open, | ||
| 186 | close => $close, | ||
| 187 | change => $change, | ||
| 188 | volume => $volume, | ||
| 189 | }; | ||
| 190 | } | ||
| 191 | return $h; | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | #----------------------------------------------------------------------------- | ||
| 196 | # latest | ||
| 197 | # | ||
| 198 | # This uses the daily data page above. | ||
| 199 | |||
| 200 | App::Chart::LatestHandler->new | ||
| 201 | (pred => $pred, | ||
| 202 | proc => \&latest_download, | ||
| 203 | available_date_time => \&daily_available_date_time); | ||
| 204 | |||
| 205 | sub latest_download { | ||
| 206 | my ($symbol_list) = @_; | ||
| 207 | |||
| 208 | App::Chart::Download::status (__('GSE latest daily data')); | ||
| 209 | my $resp = App::Chart::Download->get (GSE_DAILY_URL); | ||
| 210 | App::Chart::Download::write_latest_group (daily_parse ($resp)); | ||
| 211 | |||
| 212 | # and update database at the same time | ||
| 213 | # (ghana-process-download '() body))) | ||
| 214 | |||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | # #----------------------------------------------------------------------------- | ||
| 219 | # # names | ||
| 220 | # # | ||
| 221 | # # This uses the info pages like | ||
| 222 | # # | ||
| 223 | # # http://www.gse.com.gh/marketstat/officiallist_details.asp?Symbol=ABL | ||
| 224 | # # | ||
| 225 | # # though they aren't available for all shares. | ||
| 226 | # # | ||
| 227 | # | ||
| 228 | # App::Chart::DownloadHandler::IndivInfo->new | ||
| 229 | # (name => __('GSE names'), | ||
| 230 | # key => 'GHA-names', | ||
| 231 | # pred => $pred, | ||
| 232 | # url_func => \&name_url, | ||
| 233 | # parse => \&name_parse, | ||
| 234 | # recheck_days => 10); | ||
| 235 | # | ||
| 236 | # sub name_url { | ||
| 237 | # my ($symbol) = @_; | ||
| 238 | # return 'http://www.gse.com.gh/marketstat/officiallist_details.asp?Symbol=' | ||
| 239 | # . URI::Escape::uri_escape (App::Chart::symbol_sans_suffix ($symbol)); | ||
| 240 | # } | ||
| 241 | # | ||
| 242 | # sub name_parse { | ||
| 243 | # my ($symbol, $resp) = @_; | ||
| 244 | # | ||
| 245 | # # eg. " <td><strong> <span class="just">2241 <strong> Trading Session </strong></span>for:<font color="#D98713"> Accra Brewery Company Ltd.</font></strong></td>" | ||
| 246 | # | ||
| 247 | # my $body = $resp->decoded_content (raise_error => 1); | ||
| 248 | # $body =~ s/<[^>]*>//g; # no html | ||
| 249 | # $body =~ /Trading Session.*for:([^\n]*)/ | ||
| 250 | # or die "GSE: company name not found for '$symbol'"; | ||
| 251 | # my $name = App::Chart::collapse_whitespace ($1); | ||
| 252 | # return { name => $name }; | ||
| 253 | # } | ||
| 254 | |||
| 255 | 1; | ||
| 256 | __END__ |