| 1 | # New Zealand Stock Exchange setups. | ||
| 2 | |||
| 3 | # Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2017, 2018 Kevin Ryde | ||
| 4 | |||
| 5 | # This file is part of Chart. | ||
| 6 | # | ||
| 7 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 8 | # terms of the GNU General Public License as published by the Free Software | ||
| 9 | # Foundation; either version 3, or (at your option) any later version. | ||
| 10 | # | ||
| 11 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 12 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 13 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 14 | # details. | ||
| 15 | # | ||
| 16 | # You should have received a copy of the GNU General Public License along | ||
| 17 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | package App::Chart::Suffix::NZ; | ||
| 20 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 1 | ▶ | use URI::Escape; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 1 | ▶ | use Locale::TextDomain 'App-Chart'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | |||
| 26 | 1 | ▶ | use App::Chart; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 27 | use App::Chart::Database; | ||
| 28 | use App::Chart::Google; | ||
| 29 | use App::Chart::Download; | ||
| 30 | use App::Chart::DownloadHandler; | ||
| 31 | use App::Chart::DownloadHandler::DividendsPage; | ||
| 32 | use App::Chart::Sympred; | ||
| 33 | use App::Chart::TZ; | ||
| 34 | use App::Chart::Weblink; | ||
| 35 | use App::Chart::Yahoo; | ||
| 36 | |||
| 37 | # uncomment this to run the ### lines | ||
| 38 | # use Smart::Comments; | ||
| 39 | |||
| 40 | |||
| 41 | our $timezone_newzealand = App::Chart::TZ->new | ||
| 42 | (name => __('New Zealand'), | ||
| 43 | choose => [ 'Pacific/Auckland' ], | ||
| 44 | fallback => 'NST-12'); | ||
| 45 | |||
| 46 | # http://au.finance.yahoo.com/nzindices | ||
| 47 | # ^NZ50 | ||
| 48 | # ^NZC50 | ||
| 49 | # ^NZ10G | ||
| 50 | # ^NZMGC | ||
| 51 | # ^NZGI | ||
| 52 | # ^NZSCG | ||
| 53 | # ^NZ50G | ||
| 54 | # ^NZ30G | ||
| 55 | my $pred_shares = App::Chart::Sympred::Suffix->new ('.NZ'); | ||
| 56 | my $pred_any = App::Chart::Sympred::Regexp->new (qr/^\^NZ|\.NZ$/); | ||
| 57 | $timezone_newzealand->setup_for_symbol ($pred_any); | ||
| 58 | |||
| 59 | App::Chart::setup_source_help | ||
| 60 | ($pred_any, __p('manual-node','New Zealand Stock Exchange')); | ||
| 61 | |||
| 62 | # See http://www.nzx.com/markets/key-dates/trading-hours | ||
| 63 | # pre-open 9am-10am, trading 10am-5pm, adjust 5:30pm, then enquiry | ||
| 64 | # NZAX shares are only 10am to 4:30pm, but don't worry about that | ||
| 65 | # (yahoo-quote-lock! newzealand-symbol? | ||
| 66 | # #,(hms->seconds 9 0 0) #,(hms->seconds 17 30 0)) | ||
| 67 | # Yahoo index values based on last trades, so they should only update | ||
| 68 | # during trading 10am to 5pm. | ||
| 69 | # (yahoo-quote-lock! yahoo-index-symbol-newzealand? | ||
| 70 | # #,(hms->seconds 10 0 0) #,(hms->seconds 17 0 0)) | ||
| 71 | |||
| 72 | $App::Chart::Google::google_web_pred->add ($pred_shares); | ||
| 73 | |||
| 74 | |||
| 75 | #------------------------------------------------------------------------------ | ||
| 76 | # weblink - NZX company info | ||
| 77 | # | ||
| 78 | # Eg. https://www.nzx.com/markets/NZSX/securities/FBU | ||
| 79 | # | ||
| 80 | # cf top by value traded, | ||
| 81 | # https://www.nzx.com/markets/NZSX/securities/values | ||
| 82 | |||
| 83 | App::Chart::Weblink->new | ||
| 84 | (pred => $pred_shares, | ||
| 85 | name => __('NZX _Company Information'), | ||
| 86 | desc => __('Open web browser at the New Zealand Stock Exchange page for this stock'), | ||
| 87 | proc => sub { | ||
| 88 | my ($symbol) = @_; | ||
| 89 | return 'https://www.nzx.com/markets/NZSX/securities/' | ||
| 90 | . URI::Escape::uri_escape (App::Chart::symbol_sans_suffix ($symbol)); | ||
| 91 | }); | ||
| 92 | |||
| 93 | |||
| 94 | #------------------------------------------------------------------------------ | ||
| 95 | # dividends | ||
| 96 | # | ||
| 97 | # This uses the dividend page at | ||
| 98 | # | ||
| 99 | use constant DIVIDENDS_URL => | ||
| 100 | 'https://www.nzx.com/markets/NZSX/dividends'; | ||
| 101 | |||
| 102 | App::Chart::DownloadHandler::DividendsPage->new | ||
| 103 | (name => __('NZX dividends'), | ||
| 104 | pred => $pred_shares, | ||
| 105 | url => DIVIDENDS_URL, | ||
| 106 | parse => \÷nds_parse, | ||
| 107 | key => 'NZ-dividends', | ||
| 108 | # low priority so prices fetched first | ||
| 109 | priority => -10); | ||
| 110 | |||
| 111 | sub dividends_parse { | ||
| 112 | my ($resp) = @_; | ||
| 113 | ### NZ dividends_parse() ... | ||
| 114 | |||
| 115 | # note: want wide-chars for HTML::TableExtract parse | ||
| 116 | my $body = $resp->decoded_content (raise_error => 1); | ||
| 117 | ### $body | ||
| 118 | |||
| 119 | my @dividends = (); | ||
| 120 | my $h = { source => __PACKAGE__, | ||
| 121 | resp => $resp, | ||
| 122 | dividends => \@dividends, | ||
| 123 | copyright_key => 'NZ-dividends-copyright', | ||
| 124 | copyright => 'http://www.nzx.com/terms', | ||
| 125 | date_format => 'dmy', # dates like "27 Sep 2018" | ||
| 126 | prefer_decimals => 2, | ||
| 127 | }; | ||
| 128 | |||
| 129 | # Column "Dividend Period" for "interim" or "final" not very interesting. | ||
| 130 | # FIXME: what's the "Supp." column? | ||
| 131 | require HTML::TableExtract; | ||
| 132 | my $te = HTML::TableExtract->new | ||
| 133 | (headers => ['Code', | ||
| 134 | 'Ex', | ||
| 135 | 'Payable', | ||
| 136 | 'Amount', | ||
| 137 | 'Currency', | ||
| 138 | 'Imputation' ]); | ||
| 139 | |||
| 140 | $te->parse($body); | ||
| 141 | if (! $te->tables) { | ||
| 142 | die "NZX dividend table not matched"; | ||
| 143 | } | ||
| 144 | |||
| 145 | foreach my $ts ($te->tables) { | ||
| 146 | foreach my $row ($ts->rows) { | ||
| 147 | my ($symbol, $ex_date, $pay_date, $amount, $currency, $imput) | ||
| 148 | = @$row; | ||
| 149 | push @dividends, | ||
| 150 | dividend_parse ($symbol, $ex_date,$pay_date,$amount, $currency,$imput); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | my $count = scalar(@dividends); | ||
| 154 | App::Chart::Download::verbose_message ("NZX dividends total $count found"); | ||
| 155 | return $h; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub dividend_parse { | ||
| 159 | my ($symbol, $ex_date,$pay_date,$amount, $currency,$imput) = @_; | ||
| 160 | ### NZ dividend_parse(): @_ | ||
| 161 | |||
| 162 | # leading and trailing whitespace | ||
| 163 | $symbol = App::Chart::collapse_whitespace($symbol); | ||
| 164 | $amount = App::Chart::collapse_whitespace ($amount); | ||
| 165 | $imput = App::Chart::collapse_whitespace ($imput); | ||
| 166 | $currency = App::Chart::collapse_whitespace ($currency); | ||
| 167 | |||
| 168 | foreach ($amount, $imput) { | ||
| 169 | # eg. "3.900c" with c for cents, or latin-1 0xA2 cents symbol | ||
| 170 | if (s/ *[c\xA2]$//) { | ||
| 171 | $_ = App::Chart::Download::cents_to_dollars ($_); | ||
| 172 | } | ||
| 173 | # discard trailing zeros, when a whole number of cents | ||
| 174 | $_ = App::Chart::Download::trim_decimals ($_, 2); | ||
| 175 | } | ||
| 176 | |||
| 177 | # foreign like AUD or GBP turned into merely a note | ||
| 178 | my $note; | ||
| 179 | if ($currency ne 'NZD') { | ||
| 180 | if ($imput ne '' && $imput != 0) { | ||
| 181 | $note = "$amount + $imput $currency"; | ||
| 182 | } else { | ||
| 183 | $note = "$amount $currency"; | ||
| 184 | } | ||
| 185 | $note = App::Chart::collapse_whitespace ($note); | ||
| 186 | $amount = undef; | ||
| 187 | $imput = undef; | ||
| 188 | } | ||
| 189 | |||
| 190 | return { symbol => "$symbol.NZ", | ||
| 191 | ex_date => $ex_date, | ||
| 192 | pay_date => $pay_date, | ||
| 193 | amount => $amount, | ||
| 194 | imputation => $imput, | ||
| 195 | note => $note }; | ||
| 196 | } | ||
| 197 | |||
| 198 | 1; | ||
| 199 | __END__ | ||