blib/lib/Finance/Quote/Moex.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 179 SCAR 53.5
CC
14
Cov
0%
moex
182.0
getfields
6.0
methods
2.0
CRAP 210.0
1 #!/usr/bin/perl -w
2  
3  
4 # Copyright (C) 2017, Partizand
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # Получает котировки предыдущего торгового дня на ММВБ
17 # Получает средневзвешенную цену предыдущего дня.
18  
19 package Finance::Quote::Moex;
20  
21 use 5.000000;
22 use strict;
23 use warnings;
24 #use encoding 'utf-8';
25  
26 use vars qw($VERSION);
27  
28  
29 our $VERSION = '0.4';
30  
31 our %bond_group_urls = (tplus => "58", # Т+: Основной режим - безадрес.
32 tplus_usd => "193", # Т+: Основной режим (USD) - безадрес. 0 1
33 settle => "105", # Поставка по СК 0 1
34 distribution => "123", #Размещение - безадрес. 0 1
35 darkpools => "77", # Крупные пакеты - безадрес. 0 1
36 tplus_eur => "207", # Т+: Облигации (EUR) - безадрес. 0 1
37 darkpools_usd => "167", # Крупные пакеты – Облигации (USD) - безадрес. 0 1
38 tplus_d => "211", #Т+: Облигации Д - безадрес. 0 1
39 tplus_d_usd => "213", #Т+: Облигации Д (USD) - безадрес. 0 1
40 tplus_pir => "227", # Т+ Облигации ПИР - безадрес. 0 1
41 tplus_pir_usd => "233", # Т+: Облигации ПИР (USD) - безадрес. 0 1
42 auct_pact => "257" # Аукцион: адресные заявки 0 1
43 );
44  
45 use LWP::UserAgent;
46 use HTTP::Request::Common;
47  
48 sub methods { return ( micex => \&micex ); }
49  
50 {
51 my @labels = qw/name last open low high close waprica date isodate currency/;
52
53 sub labels { return ( micex => \@labels ); }
54 }
55  
56 sub methods { return (moex_stock => \&moex_stock,
57
58 moex_bond_tplus => \&moex_bond_tplus,
59 moex_bond_tplus_nkd => \&moex_bond_tplus_nkd,
60
61 moex_bond_ofz => \&moex_bond_tplus, # deprecated
62 moex_bond_ofz_nkd => \&moex_bond_tplus_nkd, # deprecated
63
64 moex_bond_tplus_usd => \&moex_bond_tplus_usd,
65 moex_bond_tplus_usd_nkd => \&moex_bond_tplus_usd_nkd,
66
67 moex_bond_tplus_eur => \&moex_bond_tplus_eur,
68 moex_bond_tplus_eur_nkd => \&moex_bond_tplus_eur_nkd,
69
70 moex_bond_tplus_pir => \&moex_bond_tplus_pir,
71 moex_bond_tplus_pir_nkd => \&moex_bond_tplus_pir_nkd,
72
73 moex_bond_tplus_pir_usd => \&moex_bond_tplus_pir_usd,
74 moex_bond_tplus_pir_usd_nkd => \&moex_bond_tplus_pir_usd_nkd
75 ),
76 }
77  
78 {
79 my @labels_moex = qw/name price date isodate currency/;
80 my @labels_micex = qw/name last open low high close waprice date isodate currency/;
81
82 sub labels { return (moex_bond_ofz => \@labels_moex,
83 moex_stock => \@labels_moex,
84 moex_bond_ofz_nkd => \@labels_moex,
85
86 moex_bond_tplus => \@labels_moex,
87 moex_bond_tplus_nkd => \@labels_moex,
88
89 moex_bond_tplus_usd => \@labels_moex,
90 moex_bond_tplus_usd_nkd => \@labels_moex,
91
92 moex_bond_tplus_eur => \@labels_moex,
93 moex_bond_tplus_eur_nkd => \@labels_moex,
94
95 moex_bond_tplus_pir => \@labels_moex,
96 moex_bond_tplus_pir_nkd => \@labels_moex,
97
98 moex_bond_tplus_pir_usd => \@labels_moex,
99 moex_bond_tplus_pir_usd_nkd => \@labels_moex,
100
101 #micex => \@labels_micex
102 ) }
103 }
104 # get qutes from moex.com
105  
106 sub moex_stock
107 {
108 &moex("57", "", @_);
109 }
110  
111 # tplus
112 sub moex_bond_tplus
113 {
114 &moex($bond_group_urls{"tplus"}, "1", @_);
115 }
116 sub moex_bond_tplus_nkd
117 {
118 &moex($bond_group_urls{"tplus"}, "2", @_);
119 }
120 # tplus_usd
121 sub moex_bond_tplus_usd
122 {
123 &moex($bond_group_urls{"tplus_usd"}, "1", @_);
124 }
125 sub moex_bond_tplus_usd_nkd
126 {
127 &moex($bond_group_urls{"tplus_usd"}, "2", @_);
128 }
129 # tplus_eur
130 sub moex_bond_tplus_eur
131 {
132 &moex($bond_group_urls{"tplus_eur"}, "1", @_);
133 }
134 sub moex_bond_tplus_eur_nkd
135 {
136 &moex($bond_group_urls{"tplus_eur"}, "2", @_);
137 }
138 # tplus_pir
139 sub moex_bond_tplus_pir
140 {
141 &moex($bond_group_urls{"tplus_pir"}, "1", @_);
142 }
143 sub moex_bond_tplus_pir_nkd
144 {
145 &moex($bond_group_urls{"tplus_pir"}, "2", @_);
146 }
147 # tplus_pir_usd
148 sub moex_bond_tplus_pir_usd
149 {
150 &moex($bond_group_urls{"tplus_pir_usd"}, "1", @_);
151 }
152 sub moex_bond_tplus_pir_usd_nkd
153 {
154 &moex($bond_group_urls{"tplus_pir_usd"}, "2", @_);
155 }
156
157 sub moex {
158 my $url_group = shift;
159 my $is_bond = shift;
160 my $quoter = shift;
161 my @stocks = @_;
162
163 my $sym;
164 my $stock;
165 my %info;
166 my %stockhash;
167 my @q;
168
169 my $price;
170 my $price_field;
171 my $nkd_field;
172 my $currency;
173 my $url;
174
175 foreach my $stock (@stocks) # Обнуляем признаки обработки тикеров
176 {
177 $stockhash{$stock} = 0;
178 }
179  
180 if ($is_bond)
181 {
182 $price_field = "PREVWAPRICE"; # Колонка с ценой для облигаций (средневзвешенная цена)
183 $nkd_field = "ACCRUEDINT"; # Колнока с НКД для облигаций
184
185 # Get url
186 $url = "https://iss.moex.com/iss/engines/stock/markets/bonds/boardgroups/$url_group/securities.csv";
187 }
188 else
189 {
190 $price_field = "PREVLEGALCLOSEPRICE"; # Колонка с ценой для акций (цена закрытия)
191 # Get url
192 $url = "https://iss.moex.com/iss/engines/stock/markets/shares/boardgroups/$url_group/securities.csv";
193 }
194
195
196 my $ua = $quoter->user_agent; #http
197 my $response = $ua->request(GET $url); #http begin
198
199 unless ($response->is_success)
200 {
201 foreach my $stock (@stocks)
202 {
203 $info{$stock, "success"} = 0;
204 $info{$stock, "errormsg"} = "HTTP failure";
205 }
206
207 return wantarray() ? %info : \%info;
208 } #http end
209
210 my $content = $response->content; #http
211
212 my @lines = split(/\n/,$content); # split by lines
213
214 shift @lines;
215 shift @lines;
216
217 my %fields = &getfields(shift @lines);
218
219 foreach (@lines)
220 {
221 if ($_ eq "") # После пустой строки все игнорируем
222 {
223 last;
224 }
225
226 @q = split(/;/,$_); # split by columns
227
228 $sym = $q[$fields{'SECID'}]; #Код бумаги
229 if ($sym)
230 {
231 foreach my $stock (@stocks)
232 {
233 if ( $sym eq $stock )
234 {
235
236 #print (Msg "Found sym $stock close=$q[5] date=$q[1] \n");
237 $info{$stock, "symbol"} = $stock; #Код
238 $info{$stock, "name"} = $stock;
239 #$info{$stock, "shortname"} = $q[$fields{'SHORTNAME'}];
240
241 $currency = $q[$fields{'CURRENCYID'}];
242 if ($currency eq 'SUR')
243 {
244 $currency = "RUB";
245 }
246 $info{$stock, "currency"} = $currency;
247 $info{$stock, "method"} = "moex";
248
249 $price = $q[$fields{$price_field}];
250 if ($price)
251 {
252 if ($is_bond)
253 {
254 $price = $price * 10;
255 if ($is_bond=="2")
256 {
257 $price = $price+$q[$fields{$nkd_field}]; # Прибавляем к цене облигации стоимость НКД
258 }
259 }
260
261 $info{$stock, "price"} = $price;
262 $stockhash{$stock} = 1;
263 $info{$stock, "success"} = 1;
264 }
265
266 #$quoter->store_date(\%info, $stock, {isodate => _my_time('isodate')});
267 $quoter->store_date(\%info, $stock, {isodate => $q[$fields{'PREVDATE'}]});
268
269 #print ("Found sym $stock last=$info{$stock,'last'} date=$info{$stock,'date'} \n");
270 }
271 }
272 }
273  
274 }
275
276 # check to make sure a value was returned for every fund requested
277 foreach my $stock (keys %stockhash)
278 {
279 if ($stockhash{$stock} == 0)
280 {
281 $info{$stock, "success"} = 0;
282 $info{$stock, "errormsg"} = "Stock lookup failed";
283 }
284 }
285
286 return wantarray() ? %info : \%info;
287
288
289 }
290  
291  
292 # возвращает ассоциативный массив - ключ - имя поля, значение - порядковый номр колонки
293 # По строке с названиями полей ИмяПоля;ИмяПоля1;ИмяПоля2
294 sub getfields
295 {
296 my ($head_line) = @_;
297
298 my @fields_names = split(/;/,$head_line); # split by columns
299 my $i=0;
300 my %fields;
301 foreach my $field_name (@fields_names)
302 {
303 $fields{$field_name} = $i;
304 $i = $i + 1;
305 }
306 return wantarray() ? %fields : \%fields;
307 }
308  
309 1;
310 __END__
311 =head1 NAME
312  
313 Finance::Quote::Moex - Perl module. Obtain quotes from Moex exchange.
314  
315 =head1 SYNOPSIS
316  
317 use Finance::Quote;
318  
319 my $quoter = Finance::Quote->new("Moex");
320 my %info = $quoter->fetch("moex_bond_tplus", "SU26218RMFS6"); # ОФЗ 26218
321 print "$info{'SU26218RMFS6','date'} $info{'SU26218RMFS6','price'}\n";
322  
323 =head1 DESCRIPTION
324  
325 This module fetches bond share quotes information from the Moex russian exchange http://www.moex.com and http://rbc.ru
326
327 It's not loaded as default Finance::Quote module, so you need create it
328 by Finance::Quote->new("Moex"). If you want it to load by default,
329 make changes to Finance::Quote default module loader, or use
330 FQ_LOAD_QUOTELET environment variable. Gnucash example:
331 FQ_LOAD_QUOTELET="-defaults Moex" gnucash
332  
333 =head1 LABELS RETURNED
334  
335 The following labels may be returned by Finance::Quote::Moex :
336  
337 name price date isodate currency
338
339 =head1 AUTHOR
340  
341 Partizand, partizand@gmail.com, https://github.com/partizand/finquote
342  
343 =head1 COPYRIGHT AND LICENSE
344  
345 Copyright (C) 2019 by Partizand. All rights reserved.
346  
347 This library is free software; you can redistribute it and/or modify
348 it under the same terms as Perl itself, either Perl version 5.0.8 or,
349 at your option, any later version of Perl 5 you may have available.
350  
351 =cut