| 1 | # New Zealand Stock Exchange setups. | ||
| 2 | |||
| 3 | # Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2017, 2018, 2024 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 JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 1 | ▶ | use URI::Escape; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 1 | ▶ | use Locale::TextDomain 'App-Chart'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | |||
| 27 | 0 | ▶ | use App::Chart; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 28 | use App::Chart::Database; | ||
| 29 | use App::Chart::Google; | ||
| 30 | use App::Chart::Download; | ||
| 31 | use App::Chart::DownloadHandler; | ||
| 32 | use App::Chart::DownloadHandler::DividendsPage; | ||
| 33 | use App::Chart::Sympred; | ||
| 34 | use App::Chart::TZ; | ||
| 35 | use App::Chart::Weblink; | ||
| 36 | use App::Chart::Yahoo; | ||
| 37 | |||
| 38 | # uncomment this to run the ### lines | ||
| 39 | # use Smart::Comments; | ||
| 40 | |||
| 41 | |||
| 42 | our $timezone_newzealand = App::Chart::TZ->new | ||
| 43 | (name => __('New Zealand'), | ||
| 44 | choose => [ 'Pacific/Auckland' ], | ||
| 45 | fallback => 'NST-12'); | ||
| 46 | |||
| 47 | # http://au.finance.yahoo.com/nzindices | ||
| 48 | # ^NZ50 | ||
| 49 | # ^NZC50 | ||
| 50 | # ^NZ10G | ||
| 51 | # ^NZMGC | ||
| 52 | # ^NZGI | ||
| 53 | # ^NZSCG | ||
| 54 | # ^NZ50G | ||
| 55 | # ^NZ30G | ||
| 56 | my $pred_shares = App::Chart::Sympred::Suffix->new ('.NZ'); | ||
| 57 | my $pred_any = App::Chart::Sympred::Regexp->new (qr/^\^NZ|\.NZ$/); | ||
| 58 | $timezone_newzealand->setup_for_symbol ($pred_any); | ||
| 59 | |||
| 60 | App::Chart::setup_source_help | ||
| 61 | ($pred_any, __p('manual-node','New Zealand Stock Exchange')); | ||
| 62 | |||
| 63 | # See http://www.nzx.com/markets/key-dates/trading-hours | ||
| 64 | # pre-open 9am-10am, trading 10am-5pm, adjust 5:30pm, then enquiry | ||
| 65 | # NZAX shares are only 10am to 4:30pm, but don't worry about that | ||
| 66 | # (yahoo-quote-lock! newzealand-symbol? | ||
| 67 | # #,(hms->seconds 9 0 0) #,(hms->seconds 17 30 0)) | ||
| 68 | # Yahoo index values based on last trades, so they should only update | ||
| 69 | # during trading 10am to 5pm. | ||
| 70 | # (yahoo-quote-lock! yahoo-index-symbol-newzealand? | ||
| 71 | # #,(hms->seconds 10 0 0) #,(hms->seconds 17 0 0)) | ||
| 72 | |||
| 73 | $App::Chart::Google::google_web_pred->add ($pred_shares); | ||
| 74 | |||
| 75 | |||
| 76 | #------------------------------------------------------------------------------ | ||
| 77 | # weblink - NZX company info | ||
| 78 | # | ||
| 79 | # Eg. https://www.nzx.com/markets/NZSX/securities/FBU | ||
| 80 | # | ||
| 81 | # cf top by value traded, | ||
| 82 | # https://www.nzx.com/markets/NZSX/securities/values | ||
| 83 | |||
| 84 | App::Chart::Weblink->new | ||
| 85 | (pred => $pred_shares, | ||
| 86 | name => __('NZX _Company Information'), | ||
| 87 | desc => __('Open web browser at the New Zealand Stock Exchange page for this stock'), | ||
| 88 | proc => sub { | ||
| 89 | my ($symbol) = @_; | ||
| 90 | return 'https://www.nzx.com/markets/NZSX/securities/' | ||
| 91 | . URI::Escape::uri_escape (App::Chart::symbol_sans_suffix ($symbol)); | ||
| 92 | }); | ||
| 93 | |||
| 94 | |||
| 95 | #------------------------------------------------------------------------------ | ||
| 96 | # Dividends | ||
| 97 | # | ||
| 98 | # This uses the dividend page at | ||
| 99 | # | ||
| 100 | # https://www.nzx.com/markets/NZSX/dividends | ||
| 101 | # | ||
| 102 | # The dividend table is buried in a JSON data structure in <script> | ||
| 103 | # so as minimise the number browsers which will display it. | ||
| 104 | # As of Sept 2024 the page was about 100 kbytes with 50 dividends | ||
| 105 | # (declared or to be paid). | ||
| 106 | # So set to weekly downloads. | ||
| 107 | # | ||
| 108 | use constant DIVIDENDS_URL => | ||
| 109 | 'https://www.nzx.com/markets/NZSX/dividends'; | ||
| 110 | |||
| 111 | App::Chart::DownloadHandler::DividendsPage->new | ||
| 112 | (name => __('NZX dividends'), | ||
| 113 | pred => $pred_shares, | ||
| 114 | url => DIVIDENDS_URL, | ||
| 115 | parse => \÷nds_parse, | ||
| 116 | key => 'NZ-dividends', | ||
| 117 | recheck_days => 7, | ||
| 118 | # low priority so prices fetched first | ||
| 119 | priority => -10); | ||
| 120 | |||
| 121 | sub dividends_parse { | ||
| 122 | my ($resp) = @_; | ||
| 123 | ### NZ dividends_parse() ... | ||
| 124 | |||
| 125 | my @dividends = (); | ||
| 126 | my $h = { source => __PACKAGE__, | ||
| 127 | resp => $resp, | ||
| 128 | dividends => \@dividends, | ||
| 129 | copyright_key => 'NZ-dividends-copyright', | ||
| 130 | copyright => 'https://www.nzx.com/meta-pages/terms-of-use', | ||
| 131 | date_format => 'ymd', # ISO 2024-09-09 | ||
| 132 | prefer_decimals => 2, | ||
| 133 | }; | ||
| 134 | |||
| 135 | # note: want wide-chars for HTML::TableExtract parse | ||
| 136 | my $content = $resp->decoded_content (raise_error => 1); | ||
| 137 | $content =~ /<script id="__NEXT_DATA__".*?>(.*?)<\/script>/i | ||
| 138 | or die "NZX dividends cannot find JSON table"; | ||
| 139 | my $str = $1; | ||
| 140 | my $json = JSON::from_json($1) // {}; | ||
| 141 | |||
| 142 | my $props = $json->{'props'} // {}; | ||
| 143 | my $pageProps = $props->{'pageProps'} // {}; | ||
| 144 | my $data = $pageProps->{'data'} // {}; | ||
| 145 | |||
| 146 | # marketInstruments array of hashrefs like | ||
| 147 | # "code" : "AFI", | ||
| 148 | # "isin" : "AU000000AFI5", | ||
| 149 | # "name" : "Australian Foundation Investment Company Limited Ord Shares", | ||
| 150 | # "currencyCode" : "NZD", | ||
| 151 | # Maps "isin" to exchange "code". | ||
| 152 | # "name" is longer than want to display, so stay with Yahoo "shortname". | ||
| 153 | # | ||
| 154 | my $marketInstruments = $data->{'marketInstruments'} // []; | ||
| 155 | my %isin_to_symbol; | ||
| 156 | foreach my $href (@$marketInstruments) { | ||
| 157 | my $isin = $href->{'isin'} // next; | ||
| 158 | my $code = $href->{'code'} // next; | ||
| 159 | $isin_to_symbol{$isin} = "$code.NZ"; | ||
| 160 | } | ||
| 161 | # print JSON->new->pretty->encode($marketInstruments), "\n"; exit 0; | ||
| 162 | |||
| 163 | my $marketDividends = $data->{'marketDividends'} | ||
| 164 | // die "NZX dividends parse failed"; | ||
| 165 | # print JSON->new->pretty->encode($marketDividends), "\n"; exit 0; | ||
| 166 | |||
| 167 | # eg. "amount" : "23.000000000", | ||
| 168 | # "currencyCode" : "NZD", | ||
| 169 | # "imputationCreditAmount" : "0.08166667", | ||
| 170 | # "baseQuantity" : "100", | ||
| 171 | # "expectedDate" : 1724673600 | ||
| 172 | # "payableDate" : 1727352000, | ||
| 173 | # "supplementaryAmount" : "0.03705882", | ||
| 174 | # Dates are Unix seconds since 1970 GMT and in NZ timezone is | ||
| 175 | # midnight on the relevant date. | ||
| 176 | # | ||
| 177 | foreach my $href (@$marketDividends) { | ||
| 178 | my $isin = $href->{'isin'}; | ||
| 179 | my $symbol = $isin_to_symbol{$href->{'isin'}} | ||
| 180 | // die "NZX dividends, no symbol for ISIN $isin"; | ||
| 181 | |||
| 182 | my $amount = App::Chart::Download::trim_decimals | ||
| 183 | (App::Chart::Download::cents_to_dollars($href->{'amount'}), | ||
| 184 | 2); | ||
| 185 | my $imput = $href->{'imputationCreditAmount'}; | ||
| 186 | my $ex_date = $timezone_newzealand->iso_date($href->{'expectedDate'}); | ||
| 187 | my $pay_date = $timezone_newzealand->iso_date($href->{'payableDate'}); | ||
| 188 | |||
| 189 | # Dividends not in NZD shown just as note. | ||
| 190 | # Can have AUD for dual-listed Aust/NZ shares, eg. DOW.AX = DOW.NZ | ||
| 191 | # In the past had some GBP. | ||
| 192 | my $note; | ||
| 193 | my $currency = $href->{'currencyCode'} // ''; | ||
| 194 | if ($currency ne 'NZD') { | ||
| 195 | if ($imput ne '' && $imput != 0) { | ||
| 196 | $note = "$amount + $imput $currency"; | ||
| 197 | } else { | ||
| 198 | $note = "$amount $currency"; | ||
| 199 | } | ||
| 200 | $note = App::Chart::collapse_whitespace ($note); | ||
| 201 | $amount = undef; | ||
| 202 | $imput = undef; | ||
| 203 | } | ||
| 204 | |||
| 205 | push @dividends, { symbol => $symbol, | ||
| 206 | ex_date => $ex_date, | ||
| 207 | pay_date => $pay_date, | ||
| 208 | amount => $amount, | ||
| 209 | imputation => $imput, | ||
| 210 | note => $note, | ||
| 211 | }; | ||
| 212 | } | ||
| 213 | my $count = scalar(@dividends); | ||
| 214 | App::Chart::Download::verbose_message ("NZX dividends total $count found"); | ||
| 215 | return $h; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub dividend_parse { | ||
| 219 | my ($symbol, $ex_date,$pay_date,$amount, $currency,$imput) = @_; | ||
| 220 | ### NZ dividend_parse(): @_ | ||
| 221 | |||
| 222 | # leading and trailing whitespace | ||
| 223 | |||
| 224 | foreach ($amount, $imput) { | ||
| 225 | # eg. "3.900c" with c for cents, or latin-1 0xA2 cents symbol | ||
| 226 | if (s/ *[c\xA2]$//) { | ||
| 227 | $_ = ($_); | ||
| 228 | } | ||
| 229 | # discard trailing zeros, when a whole number of cents | ||
| 230 | $_ = App::Chart::Download::trim_decimals ($_, 2); | ||
| 231 | } | ||
| 232 | |||
| 233 | # foreign like AUD or GBP turned into merely a note | ||
| 234 | |||
| 235 | return ; | ||
| 236 | } | ||
| 237 | |||
| 238 | #------------------------------------------------------------------------------ | ||
| 239 | |||
| 240 | # (String quoting for parsing of <script> in HTML to get crumb.) | ||
| 241 | # undo javascript string backslash quoting in STR, per | ||
| 242 | # | ||
| 243 | # https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals#String_Literals | ||
| 244 | # | ||
| 245 | # Encode::JavaScript::UCS does \u, but not the rest | ||
| 246 | # | ||
| 247 | # cf Java as such not quite the same: | ||
| 248 | # unicode: http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#100850 | ||
| 249 | # strings: http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#101089 | ||
| 250 | # | ||
| 251 | my %javascript_backslash = ('b' => "\b", # backspace | ||
| 252 | 'f' => "\f", # formfeed | ||
| 253 | 'n' => "\n", # newline | ||
| 254 | 'r' => "\r", | ||
| 255 | 't' => "\t", # tab | ||
| 256 | 'v' => "\013", # vertical tab | ||
| 257 | ); | ||
| 258 | sub javascript_string_unquote { | ||
| 259 | my ($str) = @_; | ||
| 260 | $str =~ s{\\(?: | ||
| 261 | ((?:[0-3]?[0-7])?[0-7]) # $1 \377 octal latin-1 | ||
| 262 | |x([0-9a-fA-F]{2}) # $2 \xFF hex latin-1 | ||
| 263 | |u([0-9a-fA-F]{4}) # $3 \uFFFF hex unicode | ||
| 264 | |(.) # $4 \n etc escapes | ||
| 265 | ) | ||
| 266 | }{ | ||
| 267 | (defined $1 ? chr(oct($1)) | ||
| 268 | : defined $4 ? ($javascript_backslash{$4} || $4) | ||
| 269 | : chr(hex($2||$3))) # \x,\u hex | ||
| 270 | }egx; | ||
| 271 | return $str; | ||
| 272 | } | ||
| 273 | |||
| 274 | |||
| 275 | #------------------------------------------------------------------------------ | ||
| 276 | |||
| 277 | 1; | ||
| 278 | __END__ | ||