App/Chart/Suffix/LJ.pm untested

filter: Statementstmt 0.0% 0 / 142 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 232 CC 17 SCAR 40.0
CC
17
Cov
0%
bts_parse
156.0
podatki_url
12.0
latest_download
6.0
CRAP 54.8
1 # Ljubljana Stock Exchange (LJSE) setups.
2  
3 # Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2016 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
8 # the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 3, or (at your option) any later
10 # version.
11 #
12 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 # details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Chart. If not, see <http://www.gnu.org/licenses/>.
19  
20  
21 package App::Chart::Suffix::LJ;
22 use 5.010;
23 use strict;
24 use warnings;
25 use Locale::TextDomain 'App-Chart';
26  
27 use App::Chart::Glib::Ex::MoreUtils;
28 use App::Chart;
29 use App::Chart::DownloadHandler::IndivChunks;
30 use App::Chart::LatestHandler;
31 use App::Chart::Sympred;
32 use App::Chart::TZ;
33 use App::Chart::Weblink;
34  
35  
36 my $timezone_ljubljana = App::Chart::TZ->new
37 (name => __('Ljubljana'),
38 choose => [ 'Europe/Ljubljana' ],
39 fallback => 'CET-1');
40  
41 my $pred = App::Chart::Sympred::Suffix->new ('.LJ');
42 $timezone_ljubljana->setup_for_symbol ($pred);
43  
44  
45 # (source-help! ljubljana-symbol?
46 # __p('manual-node','Ljubljana Stock Exchange'))
47  
48  
49 #-----------------------------------------------------------------------------
50 # weblink - company info
51 #
52 # eg.
53 # Slovenian: http://www.ljse.si/cgi-bin/jve.cgi?SecurityID=DRKR&doc=818
54 # English: http://www.ljse.si/cgi-bin/jve.cgi?SecurityID=DRKR&doc=3131
55 #
56  
57 App::Chart::Weblink->new
58 (pred => $pred,
59 name => __('LJSE _Company Information'),
60 desc => __('Open web browser at the Ljubljana Stock Exchange information page for this company'),
61 proc => sub {
62 my ($symbol) = @_;
63 $symbol = URI::Escape::uri_escape(App::Chart::symbol_sans_suffix($symbol));
64 my $lang = App::Chart::Glib::Ex::MoreUtils::lang_select (en => '3131',
65 sl => '818');
66 return "http://www.ljse.si/cgi-bin/jve.cgi?SecurityID=$symbol&doc=$lang";
67 });
68  
69  
70 #-----------------------------------------------------------------------------
71 # latest
72 #
73 # This uses the text file at
74 #
75 # http://www.ljse.si/cgi-bin/jve.cgi?doc=2111
76 #
77 # which is
78 #
79 use constant BTS_URL => 'http://www.ljse.si/datoteke/BTStecajEUR.txt';
80 #
81 # The charset isn't specified in the http headers nor the file format specs
82 # but it's codepage 1250. The server provides ETag and Last-Modified.
83 #
84 # The symbols in each file are cached, so it's usually possible to go
85 # straight to the right one. If some symbols aren't where expected then
86 # both are downloaded to recheck.
87 #
88  
89 App::Chart::LatestHandler->new
90 (pred => $pred,
91 available_tdate => \&available_tdate,
92 proc => \&latest_download);
93  
94 # per specs pdf file, available around 16:30 each day
95 sub available_tdate {
96 App::Chart::Download::tdate_today_after (16,30, $timezone_ljubljana);
97 }
98  
99 sub latest_download {
100 my ($symbol_list) = @_;
101  
102 App::Chart::Download::status (__('LJSE price file'));
103 my $resp = App::Chart::Download->get (BTS_URL,
104 url_tags_key => 'LJ-BTStecajEUR');
105 if (! $resp->is_success) {
106 return; # 304 not modified
107 }
108 App::Chart::Download::write_latest_group (bts_parse ($resp));
109 }
110  
111 my ($L0010, $L0020);
112  
113 sub bts_parse {
114 my ($resp) = @_;
115  
116 my @data = ();
117 my $h = { source => __PACKAGE__,
118 url_tags_key => 'LJ-BTStecajEUR',
119 resp => $resp,
120 currency => 'EUR',
121 date_format => 'dmy',
122 suffix => '.TSP',
123 data => \@data };
124 # charset not specified, but is codepage 1250
125 my $content = $resp->decoded_content (raise_error => 1,
126 default_charset => 'cp1250');
127 $content =~ s/\r//g;
128  
129 # 0001 file format marker
130 # $content =~ /^ 0001 110 /
131 # or die 'Ljubljana: BTS file missing 0001 id line';
132  
133 # my $date = txt_to_date ($content);
134  
135 foreach my $line (split /\n+/, $content) {
136 my $elem;
137  
138 if ($line =~ /^ 0010 /) {
139 # index
140 $L0010 ||= make_parser (code => 4,
141 symbol => 8,
142 name => 40,
143 close => '15.',
144 change => '15.',
145 percent_change => '15.');
146  
147 $elem = $L0010->($line);
148 $elem->{'symbol'} = '^' . $elem->{'symbol'};
149  
150 } elsif ($line =~ /^ 0020 /) {
151 # stock
152 $L0020 ||= make_parser (code => 4,
153 tier => 4,
154 type => 4,
155 symbol => 8,
156 isin => 20,
157 name => 40,
158 dividend => '15.',
159 note_num => 10,
160 average_price => '15.',
161 change => '15.',
162 percent_change => '15.',
163 last_date => 10,
164 bid => '15.',
165 offer => '15.',
166 high => '15.',
167 low => '15.',
168 open => '15.',
169 close => '15.',
170 volume => 12,
171 volume_offmarket => 12,
172 turnover => 12,
173 turnover_bas => 12,
174 IF_1 => 15,
175 IF_2 => 15,
176 IF_percent => 15,
177 note => 10,
178 shares_issued => 12,
179 dividend_date => 10,
180 p_e => 10,
181 principle => 20,
182 interest => 20,
183 coupon_num => 5,
184 market_discount => 15,
185 name_and_city => 80,
186 trading_mode => 5,
187 market_maker_cont => 5,
188 transactions => 15,
189 num_units => 15,
190 turnover_nonblock => 15);
191 $elem = $L0020->($line);
192  
193 # bid/offer -1 for market order
194 if ($elem->{'bid'} eq '-1') { delete $elem->{'bid'}; }
195 if ($elem->{'offer'} eq '-1') { delete $elem->{'offer'}; }
196  
197 # empty when no trades, show 0 instead of letting it go undef in
198 # crunch_h() as if no data
199 if ($elem->{'volume'} eq '') {
200 $elem->{'volume'} = 0;
201 }
202  
203 # dividend date is empty, apparently, otherwise would show "ex" as note
204 #
205 # other notes:
206 # A - cross
207 # B - block trades
208 # o - utilized tax allowance
209 # S - temporary suspension
210 # Z - temporary halt
211 # * - 10% limit move
212 # NP - data not received
213 # D - shareholders meeting will decide dividend
214 # V - interim dividend
215 # Q - P/E calculated prev year, without Q two years before
216 #
217 if ($elem->{'note'} =~ /Z/ || $elem->{'note'} =~ /S/) {
218 $elem->{'halt'} = 1;
219 }
220 if ($elem->{'note'} =~ /\*/) {
221 if ($elem->{'change'} >= 0) {
222 $elem->{'limit_up'} = 1;
223 } else {
224 $elem->{'limit_down'} = 1;
225 }
226 }
227 if ($elem->{'note'} =~ /D/) {
228 $elem->{'dividend_to_be_advised'} = 1;
229 }
230 }
231  
232 if ($elem) {
233 $elem->{'symbol'} .= '.LJ';
234  
235 # as of May 2007 the change field is empty, but the change % field is
236 # supplied
237 if ($elem->{'close'} ne ''
238 && exists $elem->{'change'} && $elem->{'change'} eq '') {
239 $elem->{'change'} = percent_change_to_change
240 ($elem->{'close'}, $elem->{'percent_change'});
241 }
242 push @data, { %$elem };
243 }
244 }
245 return $h;
246 }
247  
248 sub make_parser {
249 my @desc = @_;
250 my @field_list;
251 my @space_list;
252 my @decimal_list;
253 my $format = '';
254 my $i = 0;
255 while (@desc) {
256 my $space_field = "space_$i";
257 push @field_list, $space_field;
258 push @space_list, $space_field;
259 $i++;
260 $format .= 'A1';
261  
262 my $field = shift @desc;
263 push @field_list, $field;
264 my $width = shift @desc;
265 if ($width =~ s/\.$//) { push @decimal_list, $field; }
266 $format .= "A$width";
267 }
268 return sub {
269 my ($line) = @_;
270 my %elem;
271 @elem{@field_list} = unpack $format, $line;
272 foreach (values %elem) {
273 s/^ +//;
274 s/ +$//;
275 }
276 foreach my $field (@decimal_list) {
277 $elem{$field} =~ tr/,/./;
278 }
279 foreach (delete @elem{@space_list}) {
280 $_ eq '' or die "LJSE: bad fixed with separator on $line";
281 }
282 return \%elem;
283 };
284 }
285  
286 # pick out the "0002" trading day from a .txt file
287 sub txt_to_date {
288 my ($content) = @_;
289 $content =~ /^ 0002 ([0-9][0-9])\.([0-9][0-9])\.([0-9][0-9][0-9][0-9]) /m
290 or die 'Ljubljana: txt file missing 0002 date line';
291 return "$3-$2-$1";
292 }
293  
294 # VALUE is a string like "1234.50" which is a latest index value, and
295 # change-% is a string like "-0.09" which is a percentage change from the
296 # previous index value. Return a change amount as a string in points
297 # instead of percentage.
298 #
299 sub percent_change_to_change {
300 my ($value, $percent_change) = @_;
301 my $decimals = App::Chart::count_decimals ($value);
302  
303 # value = prev * (1 + change%/100)
304 # so prev = value * 100/(100+change%)
305 # want change = value - prev
306 # which is change = value * (1 - 100/(100+change%))
307 # = value * -change% / (100+change%)
308 # = value * change% / (100+change%)
309 #
310 return sprintf '%.*f', $decimals,
311 $value * $percent_change / ($percent_change + 100);
312 }
313  
314  
315 #-----------------------------------------------------------------------------
316 # download - individual
317 #
318 # This uses the data archives at
319 #
320 # http://www.ljse.si/cgi-bin/jve.cgi?doc=2069
321 #
322 # which gives a csv download like
323 #
324 # http://www.ljse.si/cgi-bin/jve.cgi?sid=cUg7wt8bhOKsEJiy&doc=2137&date1=02.07.2007&date2=04.07.2007&IndexCode=SBI20&SecurityId=ACLG&x=53&y=10
325 #
326 # "sid" is a session id of some sort, it works to drop it and the x and y.
327 # The "csv" option isn't shown on the web page any more, but still works.
328 # Thus,
329 #
330 # http://www.ljse.si/cgi-bin/jve.cgi?doc=2137&date1=02.07.2007&date2=04.07.2007&IndexCode=SBI20&SecurityId=ACLG&csv=1
331 #
332 # When there's no trading days in the requested range (public holidays),
333 # the file obtained is empty.
334  
335 App::Chart::DownloadHandler::IndivChunks->new
336 (name => __('Yahoo'),
337 pred => $pred,
338 available_tdate => \&available_tdate,
339 url_func => \&podatki_url,
340 parse => \&podatki_parse,
341 chunk_size => 2500);
342  
343 # return a url string, as per the examples shown above
344 sub podatki_url {
345 my ($symbol, $lo_tdate, $hi_tdate) = @_;
346  
347 my $index = ($symbol =~ /^\^/);
348 $symbol =~ s/^\^//;
349 $symbol = App::Chart::symbol_sans_suffix ($symbol);
350 my ($lo_year, $lo_month, $lo_day) = App::Chart::tdate_to_ymd ($lo_tdate);
351 my ($hi_year, $hi_month, $hi_day) = App::Chart::tdate_to_ymd ($hi_tdate);
352  
353 return sprintf 'http://www.ljse.si/cgi-bin/jve.cgi?doc=2137&date1=%02d.%02d.%04d&date2=%02d.%02d.%04d&IndexCode=%s&SecurityId=%s&csv=1',
354 $lo_day, $lo_month, $lo_year,
355 $hi_day, $hi_month, $hi_year,
356 ($index ? $symbol : ''), # index symbol, or empty
357 ($index ? 'X' : $symbol); # share symbol, or dummy
358 }
359  
360 sub podatki_parse {
361 my ($symbol, $resp) = @_;
362 my $content = $resp->decoded_content (raise_error => 1);
363 my $index = ($symbol =~ /^\^/); # if an index symbol
364  
365 my @data = ();
366 my $h = { source => __PACKAGE__,
367 currency => 'EUR',
368 date_format => 'mdy', # eg. 'Jul 28. 2006'
369 # last_download => 1,
370 data => \@data };
371  
372 foreach my $line (split /\n/, $content) {
373 my ($date, $index_price, $stock_price) = split /;/, $line;
374 push @data, { date => $date,
375 symbol => $symbol,
376 close => ($index ? $index_price : $stock_price) };
377 }
378 return $h;
379 }
380  
381 1;
382 __END__