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

filter: Statementstmt 0.0% 0 / 115 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 / 181 CC 14 SCAR 42.1
CC
14
Cov
0%
moex
182.0
getfields
6.0
methods
2.0
CRAP 67.2
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.5';
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 my $facevalue;
175
176 foreach my $stock (@stocks) # Обнуляем признаки обработки тикеров
177 {
178 $stockhash{$stock} = 0;
179 }
180  
181 if ($is_bond)
182 {
183 $price_field = "PREVWAPRICE"; # Колонка с ценой для облигаций (средневзвешенная цена)
184 $nkd_field = "ACCRUEDINT"; # Колнока с НКД для облигаций
185
186 # Get url
187 $url = "https://iss.moex.com/iss/engines/stock/markets/bonds/boardgroups/$url_group/securities.csv";
188 }
189 else
190 {
191 $price_field = "PREVLEGALCLOSEPRICE"; # Колонка с ценой для акций (цена закрытия)
192 # Get url
193 $url = "https://iss.moex.com/iss/engines/stock/markets/shares/boardgroups/$url_group/securities.csv";
194 }
195
196
197 my $ua = $quoter->user_agent; #http
198 my $response = $ua->request(GET $url); #http begin
199
200 unless ($response->is_success)
201 {
202 foreach my $stock (@stocks)
203 {
204 $info{$stock, "success"} = 0;
205 $info{$stock, "errormsg"} = "HTTP failure";
206 }
207
208 return wantarray() ? %info : \%info;
209 } #http end
210
211 my $content = $response->content; #http
212
213 my @lines = split(/\n/,$content); # split by lines
214
215 shift @lines;
216 shift @lines;
217
218 my %fields = &getfields(shift @lines);
219
220 foreach (@lines)
221 {
222 if ($_ eq "") # После пустой строки все игнорируем
223 {
224 last;
225 }
226
227 @q = split(/;/,$_); # split by columns
228
229 $sym = $q[$fields{'SECID'}]; #Код бумаги
230 if ($sym)
231 {
232 foreach my $stock (@stocks)
233 {
234 if ( $sym eq $stock )
235 {
236
237 #print (Msg "Found sym $stock close=$q[5] date=$q[1] \n");
238 $info{$stock, "symbol"} = $stock; #Код
239 $info{$stock, "name"} = $stock;
240 #$info{$stock, "shortname"} = $q[$fields{'SHORTNAME'}];
241
242 $currency = $q[$fields{'CURRENCYID'}];
243 if ($currency eq 'SUR')
244 {
245 $currency = "RUB";
246 }
247 $info{$stock, "currency"} = $currency;
248 $info{$stock, "method"} = "moex";
249
250 $price = $q[$fields{$price_field}];
251 if ($price)
252 {
253 if ($is_bond)
254 {
255 $facevalue = $q[$fields{"FACEVALUE"}]; # Номинал облигации
256 $price = $price * $facevalue / 100;
257 if ($is_bond=="2")
258 {
259 $price = $price+$q[$fields{$nkd_field}]; # Прибавляем к цене облигации стоимость НКД
260 }
261 }
262
263 $info{$stock, "price"} = $price;
264 $stockhash{$stock} = 1;
265 $info{$stock, "success"} = 1;
266 }
267
268 #$quoter->store_date(\%info, $stock, {isodate => _my_time('isodate')});
269 $quoter->store_date(\%info, $stock, {isodate => $q[$fields{'PREVDATE'}]});
270
271 #print ("Found sym $stock last=$info{$stock,'last'} date=$info{$stock,'date'} \n");
272 }
273 }
274 }
275  
276 }
277
278 # check to make sure a value was returned for every fund requested
279 foreach my $stock (keys %stockhash)
280 {
281 if ($stockhash{$stock} == 0)
282 {
283 $info{$stock, "success"} = 0;
284 $info{$stock, "errormsg"} = "Stock lookup failed";
285 }
286 }
287
288 return wantarray() ? %info : \%info;
289
290
291 }
292  
293  
294 # возвращает ассоциативный массив - ключ - имя поля, значение - порядковый номр колонки
295 # По строке с названиями полей ИмяПоля;ИмяПоля1;ИмяПоля2
296 sub getfields
297 {
298 my ($head_line) = @_;
299
300 my @fields_names = split(/;/,$head_line); # split by columns
301 my $i=0;
302 my %fields;
303 foreach my $field_name (@fields_names)
304 {
305 $fields{$field_name} = $i;
306 $i = $i + 1;
307 }
308 return wantarray() ? %fields : \%fields;
309 }
310  
311 1;
312 __END__
313 =head1 NAME
314  
315 Finance::Quote::Moex - Perl module. Obtain quotes from Moex exchange.
316  
317 =head1 SYNOPSIS
318  
319 use Finance::Quote;
320  
321 my $quoter = Finance::Quote->new("Moex");
322 my %info = $quoter->fetch("moex_bond_tplus", "SU26218RMFS6"); # ОФЗ 26218
323 print "$info{'SU26218RMFS6','date'} $info{'SU26218RMFS6','price'}\n";
324  
325 =head1 DESCRIPTION
326  
327 This module fetches bond share quotes information from the Moex russian exchange http://www.moex.com and http://rbc.ru
328
329 It's not loaded as default Finance::Quote module, so you need create it
330 by Finance::Quote->new("Moex"). If you want it to load by default,
331 make changes to Finance::Quote default module loader, or use
332 FQ_LOAD_QUOTELET environment variable. Gnucash example:
333 FQ_LOAD_QUOTELET="-defaults Moex" gnucash
334  
335 =head1 LABELS RETURNED
336  
337 The following labels may be returned by Finance::Quote::Moex :
338  
339 name price date isodate currency
340
341 =head1 AUTHOR
342  
343 Partizand, partizand@gmail.com, https://github.com/partizand/finquote
344  
345 =head1 COPYRIGHT AND LICENSE
346  
347 Copyright (C) 2019 by Partizand. All rights reserved.
348  
349 This library is free software; you can redistribute it and/or modify
350 it under the same terms as Perl itself, either Perl version 5.0.8 or,
351 at your option, any later version of Perl 5 you may have available.
352  
353 =cut