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

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 173 CC 14 SCAR 42.6
CC
14
Cov
0%
moex
182.0
getfields
6.0
methods
2.0
CRAP 71.1
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.6';
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  
49 sub methods { return (moex_stock => \&moex_stock,
50
51 moex_bond_tplus => \&moex_bond_tplus,
52 moex_bond_tplus_nkd => \&moex_bond_tplus_nkd,
53
54 moex_bond_ofz => \&moex_bond_tplus, # deprecated
55 moex_bond_ofz_nkd => \&moex_bond_tplus_nkd, # deprecated
56
57 moex_bond_tplus_usd => \&moex_bond_tplus_usd,
58 moex_bond_tplus_usd_nkd => \&moex_bond_tplus_usd_nkd,
59
60 moex_bond_tplus_eur => \&moex_bond_tplus_eur,
61 moex_bond_tplus_eur_nkd => \&moex_bond_tplus_eur_nkd,
62
63 moex_bond_tplus_pir => \&moex_bond_tplus_pir,
64 moex_bond_tplus_pir_nkd => \&moex_bond_tplus_pir_nkd,
65
66 moex_bond_tplus_pir_usd => \&moex_bond_tplus_pir_usd,
67 moex_bond_tplus_pir_usd_nkd => \&moex_bond_tplus_pir_usd_nkd
68 ),
69 }
70  
71 {
72 my @labels_moex = qw/name price date isodate currency/;
73 my @labels_micex = qw/name last open low high close waprice date isodate currency/;
74
75 sub labels { return (moex_bond_ofz => \@labels_moex,
76 moex_stock => \@labels_moex,
77 moex_bond_ofz_nkd => \@labels_moex,
78
79 moex_bond_tplus => \@labels_moex,
80 moex_bond_tplus_nkd => \@labels_moex,
81
82 moex_bond_tplus_usd => \@labels_moex,
83 moex_bond_tplus_usd_nkd => \@labels_moex,
84
85 moex_bond_tplus_eur => \@labels_moex,
86 moex_bond_tplus_eur_nkd => \@labels_moex,
87
88 moex_bond_tplus_pir => \@labels_moex,
89 moex_bond_tplus_pir_nkd => \@labels_moex,
90
91 moex_bond_tplus_pir_usd => \@labels_moex,
92 moex_bond_tplus_pir_usd_nkd => \@labels_moex,
93
94 #micex => \@labels_micex
95 ) }
96 }
97 # get qutes from moex.com
98  
99 sub moex_stock
100 {
101 &moex("57", "", @_);
102 }
103  
104 # tplus
105 sub moex_bond_tplus
106 {
107 &moex($bond_group_urls{"tplus"}, "1", @_);
108 }
109 sub moex_bond_tplus_nkd
110 {
111 &moex($bond_group_urls{"tplus"}, "2", @_);
112 }
113 # tplus_usd
114 sub moex_bond_tplus_usd
115 {
116 &moex($bond_group_urls{"tplus_usd"}, "1", @_);
117 }
118 sub moex_bond_tplus_usd_nkd
119 {
120 &moex($bond_group_urls{"tplus_usd"}, "2", @_);
121 }
122 # tplus_eur
123 sub moex_bond_tplus_eur
124 {
125 &moex($bond_group_urls{"tplus_eur"}, "1", @_);
126 }
127 sub moex_bond_tplus_eur_nkd
128 {
129 &moex($bond_group_urls{"tplus_eur"}, "2", @_);
130 }
131 # tplus_pir
132 sub moex_bond_tplus_pir
133 {
134 &moex($bond_group_urls{"tplus_pir"}, "1", @_);
135 }
136 sub moex_bond_tplus_pir_nkd
137 {
138 &moex($bond_group_urls{"tplus_pir"}, "2", @_);
139 }
140 # tplus_pir_usd
141 sub moex_bond_tplus_pir_usd
142 {
143 &moex($bond_group_urls{"tplus_pir_usd"}, "1", @_);
144 }
145 sub moex_bond_tplus_pir_usd_nkd
146 {
147 &moex($bond_group_urls{"tplus_pir_usd"}, "2", @_);
148 }
149
150 sub moex {
151 my $url_group = shift;
152 my $is_bond = shift;
153 my $quoter = shift;
154 my @stocks = @_;
155
156 my $sym;
157 my $stock;
158 my %info;
159 my %stockhash;
160 my @q;
161
162 my $price;
163 my $price_field;
164 my $nkd_field;
165 my $currency;
166 my $url;
167 my $facevalue;
168
169 foreach my $stock (@stocks) # Обнуляем признаки обработки тикеров
170 {
171 $stockhash{$stock} = 0;
172 }
173  
174 if ($is_bond)
175 {
176 $price_field = "PREVWAPRICE"; # Колонка с ценой для облигаций (средневзвешенная цена)
177 $nkd_field = "ACCRUEDINT"; # Колнока с НКД для облигаций
178
179 # Get url
180 $url = "https://iss.moex.com/iss/engines/stock/markets/bonds/boardgroups/$url_group/securities.csv";
181 }
182 else
183 {
184 $price_field = "PREVLEGALCLOSEPRICE"; # Колонка с ценой для акций (цена закрытия)
185 # Get url
186 $url = "https://iss.moex.com/iss/engines/stock/markets/shares/boardgroups/$url_group/securities.csv";
187 }
188
189
190 my $ua = $quoter->user_agent; #http
191 my $response = $ua->request(GET $url); #http begin
192
193 unless ($response->is_success)
194 {
195 foreach my $stock (@stocks)
196 {
197 $info{$stock, "success"} = 0;
198 $info{$stock, "errormsg"} = "HTTP failure";
199 }
200
201 return wantarray() ? %info : \%info;
202 } #http end
203
204 my $content = $response->content; #http
205
206 my @lines = split(/\n/,$content); # split by lines
207
208 shift @lines;
209 shift @lines;
210
211 my %fields = &getfields(shift @lines);
212
213 foreach (@lines)
214 {
215 if ($_ eq "") # После пустой строки все игнорируем
216 {
217 last;
218 }
219
220 @q = split(/;/,$_); # split by columns
221
222 $sym = $q[$fields{'SECID'}]; #Код бумаги
223 if ($sym)
224 {
225 foreach my $stock (@stocks)
226 {
227 if ( $sym eq $stock )
228 {
229
230 #print (Msg "Found sym $stock close=$q[5] date=$q[1] \n");
231 $info{$stock, "symbol"} = $stock; #Код
232 $info{$stock, "name"} = $stock;
233 #$info{$stock, "shortname"} = $q[$fields{'SHORTNAME'}];
234
235 $currency = $q[$fields{'CURRENCYID'}];
236 if ($currency eq 'SUR')
237 {
238 $currency = "RUB";
239 }
240 $info{$stock, "currency"} = $currency;
241 $info{$stock, "method"} = "moex";
242
243 $price = $q[$fields{$price_field}];
244 if ($price)
245 {
246 if ($is_bond)
247 {
248 $facevalue = $q[$fields{"FACEVALUE"}]; # Номинал облигации
249 $price = $price * $facevalue / 100;
250 if ($is_bond=="2")
251 {
252 $price = $price+$q[$fields{$nkd_field}]; # Прибавляем к цене облигации стоимость НКД
253 }
254 }
255
256 $info{$stock, "price"} = $price;
257 $stockhash{$stock} = 1;
258 $info{$stock, "success"} = 1;
259 }
260
261 #$quoter->store_date(\%info, $stock, {isodate => _my_time('isodate')});
262 $quoter->store_date(\%info, $stock, {isodate => $q[$fields{'PREVDATE'}]});
263
264 #print ("Found sym $stock last=$info{$stock,'last'} date=$info{$stock,'date'} \n");
265 }
266 }
267 }
268  
269 }
270
271 # check to make sure a value was returned for every fund requested
272 foreach my $stock (keys %stockhash)
273 {
274 if ($stockhash{$stock} == 0)
275 {
276 $info{$stock, "success"} = 0;
277 $info{$stock, "errormsg"} = "Stock lookup failed";
278 }
279 }
280
281 return wantarray() ? %info : \%info;
282
283
284 }
285  
286  
287 # возвращает ассоциативный массив - ключ - имя поля, значение - порядковый номр колонки
288 # По строке с названиями полей ИмяПоля;ИмяПоля1;ИмяПоля2
289 sub getfields
290 {
291 my ($head_line) = @_;
292
293 my @fields_names = split(/;/,$head_line); # split by columns
294 my $i=0;
295 my %fields;
296 foreach my $field_name (@fields_names)
297 {
298 $fields{$field_name} = $i;
299 $i = $i + 1;
300 }
301 return wantarray() ? %fields : \%fields;
302 }
303  
304 1;
305 __END__