| 1 | # Bendigo Stock Exchange (BSX) setups. | ||
| 2 | |||
| 3 | # Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2012 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::BEN; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Locale::TextDomain ('App-Chart'); | ||
| 23 | |||
| 24 | use App::Chart; | ||
| 25 | use App::Chart::Sympred; | ||
| 26 | use App::Chart::TZ; | ||
| 27 | use App::Chart::Weblink; | ||
| 28 | |||
| 29 | |||
| 30 | # Victoria is the same as NSW these days, but let's not assume that rare | ||
| 31 | # outbreak of interstate cooperation will be permanent (in the past the | ||
| 32 | # dates to begin or end daylight savings have varied). | ||
| 33 | # | ||
| 34 | my $timezone_bendigo = App::Chart::TZ->new | ||
| 35 | (name => __('Bendigo'), | ||
| 36 | choose => [ 'Australia/Melbourne' ], | ||
| 37 | fallback => 'EST-10'); | ||
| 38 | |||
| 39 | my $pred = App::Chart::Sympred::Suffix->new ('.BEN'); | ||
| 40 | $timezone_bendigo->setup_for_symbol ($pred); | ||
| 41 | |||
| 42 | # App::Chart::setup_source_help | ||
| 43 | # ($pred, __p('manual-node','Bendigo Stock Exchange')); | ||
| 44 | |||
| 45 | |||
| 46 | #------------------------------------------------------------------------------ | ||
| 47 | # weblink - company info | ||
| 48 | |||
| 49 | App::Chart::Weblink->new | ||
| 50 | (pred => $pred, | ||
| 51 | name => __('BSX _Company Information'), | ||
| 52 | desc => __('Open web browser at the Bendigo Stock Exchange information page for this company'), | ||
| 53 | proc => sub { | ||
| 54 | my ($symbol) = @_; | ||
| 55 | return 'http://www.bsx.com.au/markets_pricesresearch_com.asp?security=' | ||
| 56 | . secode_for_symbol ($symbol); | ||
| 57 | }); | ||
| 58 | # weblink_message __('Fetching BSX symbol codes ...') | ||
| 59 | |||
| 60 | |||
| 61 | #----------------------------------------------------------------------------- | ||
| 62 | # security codes | ||
| 63 | # | ||
| 64 | # The home page | ||
| 65 | # | ||
| 66 | # http://www.bsx.com.au | ||
| 67 | # | ||
| 68 | # has a pull-down menu of symbols, with the code numbers to be used for | ||
| 69 | # quotes and trade history. | ||
| 70 | # | ||
| 71 | # The cache here saves the code numbers for one day (the current trading | ||
| 72 | # day, bendigo time). This ensures we don't download every time, but still | ||
| 73 | # have a fresh set of codes, in case there's additions or perhaps changes. | ||
| 74 | # | ||
| 75 | # Thought was given to a more sophisticated caching scheme, for instance use | ||
| 76 | # an old code and if the quote turns out to be for something else and the | ||
| 77 | # cache isn't fresh then refill it and get the quote again. But for the | ||
| 78 | # data download it'd be a case of checking the name against the database | ||
| 79 | # name, since there's no symbol on that page, except that for the initial | ||
| 80 | # download when there's no database name yet the cache would have to be | ||
| 81 | # pre-freshened. This seems unnecessarily complicated to merely avoid | ||
| 82 | # downloading one page, especially when it's compressed to about 3kbytes if | ||
| 83 | # we've got gzip or zlib. | ||
| 84 | |||
| 85 | |||
| 86 | # return option code string, or undef if unknown | ||
| 87 | sub secode_for_symbol { | ||
| 88 | my ($symbol) = @_; | ||
| 89 | require App::Chart::Pagebits; | ||
| 90 | my $h = App::Chart::Pagebits::get | ||
| 91 | (name => __('BSX stock numbers'), | ||
| 92 | url => 'http://www.bsx.com.au', | ||
| 93 | key => 'bendigo-codes', | ||
| 94 | freq_days => 1, | ||
| 95 | timezone => $timezone_bendigo, | ||
| 96 | parse => \&secodes_parse); | ||
| 97 | return $h->{$symbol}; | ||
| 98 | } | ||
| 99 | |||
| 100 | # return hash reference { $symbol => $code, ... } | ||
| 101 | sub secodes_parse { | ||
| 102 | my ($content) = @_; | ||
| 103 | my %hash; | ||
| 104 | while ($content =~ m{<option value=\"([0-9]+)\">([A-Z]+)</option>}g) { | ||
| 105 | $hash{"$2.BEN"} = $1; | ||
| 106 | } | ||
| 107 | return \%hash; | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; | ||
| 111 | __END__ | ||
| 112 | |||
| 113 | #------------------------------------------------------------------------------ | ||
| 114 | |||
| 115 | |||
| 116 | # This uses the quotes pages like | ||
| 117 | # | ||
| 118 | # http://www.bsx.com.au/markets_pricesresearch_pri.asp?security=20 | ||
| 119 | # | ||
| 120 | # with the security code number from the symbols menu on the home page. | ||
| 121 | # | ||
| 122 | # www.bsx.com.au sends gzipped data, which is good since it compresses | ||
| 123 | # about 19kbytes of javascript junk down to about 3kbytes sent. | ||
| 124 | # | ||
| 125 | # The trade history pages like | ||
| 126 | # | ||
| 127 | # http://www.bsx.com.au/markets_pricesresearch_tra.asp?security=1 | ||
| 128 | # | ||
| 129 | # also have a current bid/offer and show the date/time of the last trade. | ||
| 130 | # Not sure if there's much value showing open/high/low/volume from trading | ||
| 131 | # that could be weeks ago. | ||
| 132 | |||
| 133 | # return list (ADATE TIME) for latest quote | ||
| 134 | # | ||
| 135 | # the BSX system takes changes only during 9:30am-11:30am weekdays (order | ||
| 136 | # entry 9:30 to 11, then trading 11 to 11:30), so outside those hours lock | ||
| 137 | # to 11:30am | ||
| 138 | # | ||
| 139 | # http://www.bsx.com.au/markets_aboutbsxmarkets_tra.asp | ||
| 140 | # | ||
| 141 | sub quote_date_time { | ||
| 142 | # (tm->adate-time-within (localtime (current-time) (timezone-bendigo)) | ||
| 143 | # #,(hms->seconds 9 30 0) | ||
| 144 | # #,(hms->seconds 11 30 0))) | ||
| 145 | } | ||
| 146 | |||
| 147 | sub bendigo_quote { | ||
| 148 | |||
| 149 | sub quote_parse { | ||
| 150 | my ($resp) = @_; | ||
| 151 | my $content = $resp->decoded_content (raise_error => 1); | ||
| 152 | |||
| 153 | my @data = (); | ||
| 154 | my $h = { source => __PACKAGE__, | ||
| 155 | resp => $resp, | ||
| 156 | date_format => 'dmy', | ||
| 157 | data => \@data }; | ||
| 158 | |||
| 159 | require HTML::TableExtract; | ||
| 160 | |||
| 161 | my $te = HTML::TableExtract->new | ||
| 162 | (headers => ['code', 'bid', 'offer', 'last'], | ||
| 163 | keep_html => 1); | ||
| 164 | $te->parse($content); | ||
| 165 | |||
| 166 | my ($ts) = $te->tables || die 'BSX: quote columns not found'; | ||
| 167 | $te = HTML::TableExtract->new | ||
| 168 | (depth => $ts->depth, | ||
| 169 | count => $ts->count + 1); | ||
| 170 | $te->tables || die 'BSX: reparse table not found'; | ||
| 171 | |||
| 172 | # (receive-list (quote-adate quote-time) | ||
| 173 | # (bendigo-quote-adate-time) | ||
| 174 | |||
| 175 | # # eg: <span class="Blue"><br><br><b>Capilano Honey Limited</b><br><font color=red>TRADING HALT</font><br></span><br> | ||
| 176 | # (m (regexp-exec #,(regexp "Blue.*<b>.*red[^>]*>([^<]+)" regexp/icase regexp/newline) body)) | ||
| 177 | # (note (and m (match:substring m 1)))) | ||
| 178 | |||
| 179 | |||
| 180 | foreach my $row ($te->rows) { | ||
| 181 | my ($code, $bid, $offer, $last) = @$row; | ||
| 182 | $code =~ /(.*)\(([^)]+)\)/ or next; | ||
| 183 | my $name = $1; | ||
| 184 | my $symbol = "$2.BEN"; | ||
| 185 | push @data, { symbol => $symbol, | ||
| 186 | name => $name, | ||
| 187 | bid => $bid, | ||
| 188 | offer => $offer, | ||
| 189 | last => $last }; | ||
| 190 | |||
| 191 | #:quote-adate quote-adate | ||
| 192 | #:quote-time quote-time | ||
| 193 | #:note note | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | (define (bendigo-latest-get symbol-list extra-list proc) | ||
| 198 | (for-each (lambda (symbol) | ||
| 199 | (define option (symbol->option symbol)) | ||
| 200 | |||
| 201 | (if (not option) | ||
| 202 | (proc (list (latest-new-unknown symbol))) | ||
| 203 | |||
| 204 | (receive (headers body) | ||
| 205 | (http-request (string-append "http://www.bsx.com.au/markets_pricesresearch_pri.asp?security=" option) | ||
| 206 | #:want-ok #t) | ||
| 207 | (proc (body->latest-list body))))) | ||
| 208 | |||
| 209 | symbol-list)) | ||
| 210 | |||
| 211 | (latest-handler! #:selector bendigo-symbol? | ||
| 212 | #:handler bendigo-latest-get | ||
| 213 | #:adate-time bendigo-quote-adate-time) | ||
| 214 | |||
| 215 | |||
| 216 | |||
| 217 | ;;----------------------------------------------------------------------------- | ||
| 218 | ;; download | ||
| 219 | ;; | ||
| 220 | ;; This uses the "view all" trade history pages like | ||
| 221 | ;; | ||
| 222 | ;; http://www.bsx.com.au/markets_pricesresearch_tra_popup.asp?security=20 | ||
| 223 | ;; | ||
| 224 | ;; with the security number from the home page menu (cached above). | ||
| 225 | ;; | ||
| 226 | ;; This history has each trade individually, but we collapse that to daily | ||
| 227 | ;; open/high/low/close. | ||
| 228 | |||
| 229 | |||
| 230 | ;; Eg: | ||
| 231 | ;; <span class="Blue"><b>Capilano Honey Limited</b></span><br> | ||
| 232 | ;; <span class="Blue"><br>Trade History<br></span><br> | ||
| 233 | ;; <table ... | ||
| 234 | ;; | ||
| 235 | ;; Or when ex dividend: | ||
| 236 | ;; <span class="Blue"><b>Capilano Honey Limited</b><br><font color=red>XD</font></span><br> | ||
| 237 | ;; <span class="Blue"><br>Trade History<br></span><br> | ||
| 238 | ;; | ||
| 239 | ;; Or when halted: | ||
| 240 | ;; <span class="Blue"><b>Capilano Honey Limited</b><br><font color=red>TRADING HALT</font></span><br> | ||
| 241 | ;; <span class="Blue"><br>Trade History<br></span><br> | ||
| 242 | ;; | ||
| 243 | ;; match 1 is company name | ||
| 244 | ;; | ||
| 245 | (define history-regexp | ||
| 246 | "<b>([^<]+)[^\n]*\n[^\n]*<br>Trade History") | ||
| 247 | |||
| 248 | (define (bendigo-process-download symbol url headers body) | ||
| 249 | (define commodity (chart-symbol-sans-dot symbol)) | ||
| 250 | |||
| 251 | (let* ((m (must-match (string-match history-regexp body))) | ||
| 252 | (name (match:substring m 1)) | ||
| 253 | (row-list (html-table-rows body | ||
| 254 | ;; skip empty table, the second is wanted | ||
| 255 | (string-contains-after-ci body "<table" | ||
| 256 | (match:end m))))) | ||
| 257 | |||
| 258 | (let ((headings (map! string-trim-right (first row-list)))) | ||
| 259 | (or (equal? headings '("Qty" "Price" "Date" "Time")) | ||
| 260 | (error "BSX: unrecognised data columns:" headings))) | ||
| 261 | (set! row-list (cdr row-list)) | ||
| 262 | |||
| 263 | (download-process | ||
| 264 | #:module (_ "BSX") | ||
| 265 | #:symbol-list (list symbol) | ||
| 266 | #:name name | ||
| 267 | #:url url | ||
| 268 | #:headers headers | ||
| 269 | #:currency "AUD" | ||
| 270 | #:hi (bendigo-available-tdate) | ||
| 271 | #:last-download #t | ||
| 272 | #:prefer-decimals 2 | ||
| 273 | #:row-list | ||
| 274 | (map (lambda (row-list) | ||
| 275 | (receive-list (volume-1 price-1 date time-1) | ||
| 276 | (first row-list) | ||
| 277 | ;; most recent first in list, so reverse for sessions | ||
| 278 | (list #:tdate (d/m/y-str->tdate date) | ||
| 279 | #:commodity commodity | ||
| 280 | #:sessions (reverse! (map second row-list)) | ||
| 281 | #:volume (apply + (map! string->number-err | ||
| 282 | (map first row-list)))))) | ||
| 283 | (partition-equal-adjacent third row-list))))) | ||
| 284 | |||
| 285 | ;; latest download data available | ||
| 286 | ;; guess today available from 6pm | ||
| 287 | ;; | ||
| 288 | (define (bendigo-available-tdate) | ||
| 289 | (tdate-today-after 18 0 (timezone-melbourne))) | ||
| 290 | |||
| 291 | (define (bendigo-download symbol-list) | ||
| 292 | (for-each | ||
| 293 | (lambda (symbol) | ||
| 294 | (download-status (_ "BSX") (_ "data") symbol) | ||
| 295 | |||
| 296 | (let ((option (symbol->option symbol))) | ||
| 297 | (if (not option) | ||
| 298 | (download-message | ||
| 299 | (string-append (_ "BSX") ": " (_ "unknown symbol") " " symbol)) | ||
| 300 | |||
| 301 | (let ((url (string-append "http://www.bsx.com.au/markets_pricesresearch_tra_popup.asp?security=" option))) | ||
| 302 | (receive (headers body) | ||
| 303 | (http-request url #:want-ok #t) | ||
| 304 | (bendigo-process-download symbol url headers body)))))) | ||
| 305 | symbol-list)) | ||
| 306 | |||
| 307 | (define (bendigo-backto want-tdate symbol-list) | ||
| 308 | (download-message | ||
| 309 | (string-append (_ "BSX") ": " (_ "no further historical data available")))) | ||
| 310 | |||
| 311 | (download-handler! #:selector bendigo-symbol? | ||
| 312 | #:update bendigo-download | ||
| 313 | #:avail-proc bendigo-available-tdate | ||
| 314 | #:backto bendigo-backto) |