Quote/Consorsbank.pm

filter: Statementstmt 35.3% 47 / 133 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 4 totaltotal 25.4% 60 / 236 CC 35 SCAR 63.5
CC
35
Cov
23%
consorsbank
930.0
__ANON__
42.0
BEGIN
1.0
CRAP 574.8
« Quote/FTfunds.pm ↑ Summary Quote/AlphaVantage.pm »
1 #!/usr/bin/perl -w
2  
3 # Copyright (C) 2023, Stephan Gambke <s7eph4n@gmail.com>
4  
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 # 02110-1301, USA
17  
18 5 require 5.005;
19  
20 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 package Finance::Quote::Consorsbank;
24  
25 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Date::Parse qw(str2time);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use if DEBUG, 'Data::Dumper';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 5 our $VERSION = '1.72'; # VERSION
36  
37 5 my $CONSORS_URL = 'https://www.consorsbank.de/web-financialinfo-service/api/marketdata/stocks?';
38 5 my $CONSORS_SOURCE_BASE_URL = 'https://www.consorsbank.de/web/Wertpapier/';
39  
40 5 our $DISPLAY = 'Consorsbank - Consorsbank API';
41 5 our $FEATURES = { 'EXCHANGE' => 'select market place (i.e. "gettex", "Xetra", "Tradegate")' };
42  
43 # Correspondence of FQ labels to Consorsbank API fields
44  
45 # success Did the stock successfully return information? (true/false)
46 # errormsg Info.Errors.ERROR_MESSAGE If success is false, this field may contain the reason why.
47 # symbol Info.ID.SYMBOL ticker symbol
48 # wkn Info.ID.WKN WKN
49 # symbol Info.ID.ISIN ISIN
50 # name BasicV1.NAME_SECURITY Company or Mutual Fund Name
51 # method 'consorsbank' The module (as could be passed to fetch) which found this information.
52 # source Source URL, either general website or direct human-readable deep link
53 # exchange CONSORS_EXCHANGE_NAME The exchange the information was obtained from.
54 # currency ISO_CURRENCY ISO currency code
55  
56 # ask ASK Ask
57 # avg_vol Average Daily Vol
58 # bid BID Bid
59 # cap Market Capitalization
60 # close PREVIOUS_LAST Previous Close
61 # date DATETIME_PRICE Last Trade Date (MM/DD/YY format)
62 # day_range HIGH, LOW Day's Range
63 # div Dividend per Share
64 # div_date Dividend Pay Date
65 # div_yield Dividend Yield
66 # eps Earnings per Share
67 # ex_div Ex-Dividend Date.
68 # high HIGH Highest trade today
69 # last PRICE Last Price
70 # low LOW Lowest trade today
71 # nav Net Asset Value
72 # net PERFORMANCE Net Change
73 # open FIRST Today's Open
74 # p_change PERFORMANCE_PCT Percent Change from previous day's close
75 # pe P/E Ratio
76 # time DATETIME_PRICE Last Trade Time
77 # type The type of equity returned
78 # volume TOTAL_VOLUME Volume
79 # year_range HIGH_PRICE_1_YEAR - LOW_PRICE_1_YEAR 52-Week Range
80 # yield Yield (usually 30 day avg)
81  
82 5 our @LABELS = qw/
83 symbol wkn isin
84 name
85 method
86 source
87 exchange
88 exchanges
89 currency
90 ask
91 bid
92 date
93 day_range
94 high
95 last
96 low
97 net
98 open
99 close
100 p_change
101 volume
102 year_range
103 /;
104  
105 5 our $METHODHASH = {subroutine => \&consorsbank,
106 display => $DISPLAY,
107 labels => \@LABELS,
108 features => $FEATURES};
109  
110 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
111 return (
112 10 consorsbank => $METHODHASH,
113 europe => $METHODHASH,
114 germany => $METHODHASH,
115 );
116 }
117  
118 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
119 5 my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m;
120 }
121  
122 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
123 5 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
124 }
125  
126 0 ▶sub consorsbank {
Subroutine consorsbank: not called CC=30 SCAR=68.4
Pod undocumented
127  
128 # a Finance::Quote object
129 0 my Finance::Quote $quoter = shift;
130  
131 # a list of zero or more symbol names
132 0 ▶ my @symbols = @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@symbols) = or return 0 0 0
133  
134 # user_agent() provides a ready-to-use LWP::UserAgent
135 0 my $ua = $quoter->user_agent;
136  
137 0 my %info;
138  
139 0 my %mapping = (# in QuotesV1, PriceV2, ExchangesV2
140 'exchange' => 'CONSORS_EXCHANGE_NAME',
141 'currency' => 'ISO_CURRENCY', # also 'UNIT_PRICE' exists
142 'p_change' => 'PERFORMANCE_PCT');
143  
144 0 my %map_price_v2 = (# QuotesV1 & PriceV2 keys
145 'ask' => 'ASK',
146 'bid' => 'BID',
147 'close' => 'PREVIOUS_LAST',
148 'open' => 'FIRST',
149 'high' => 'HIGH',
150 'last' => 'PRICE',
151 'low' => 'LOW',
152 'net' => 'PERFORMANCE',
153 'volume' => 'TOTAL_VOLUME');
154  
155 0 my %map_quotes_v1 = ( %map_price_v2,
156 # QuotesV1 keys
157 'year_low' => 'LOW_PRICE_1_YEAR',
158 'year_high' => 'HIGH_PRICE_1_YEAR');
159  
160 0 my %map_exchanges_v2 = (# ExchangesV2 keys
161 'last' => 'PRICE',
162 'ask' => 'PRICE_ASK',
163 'bid' => 'PRICE_BID');
164  
165 0 my $labels = $quoter->get_required_labels;
166 0 ▶ $labels = \@LABELS unless(scalar(@$labels));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$labels 0 0 0
167  
168 0 my $require_quotes_v1 = grep { exists($map_quotes_v1{$_})
169 0 ▶ and not exists($map_exchanges_v2{$_})
Condition0%0/3
exists $map_quotes_v1{$_} && !exists $map_exchanges_v2{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $map_quotes_v1{$_} && !exists $map_exchanges_v2{$_}
exists $map_quotes_v1{$_} !exists $map_exchanges_v2{$_}
Truth table0%0/3
A && not(B)
A= exists $map_quotes_v1{$_}B= !exists $map_exchanges_v2{$_}
ABresult
0X0
100
111
170 } @$labels;
171  
172 0 my $require_exchanges = grep { $_ eq 'exchanges' } @$labels;
173  
174 my $exchange = exists $quoter->{module_specific_data}->{consorsbank}->{EXCHANGE} ?
175 0 ▶ $quoter->{module_specific_data}->{consorsbank}->{EXCHANGE} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $quoter->{'module_specific_data'}{'consorsbank'}{'EXCHANGE'} ? : 0 0 0
176  
177 0 ▶ if ($exchange and not $require_quotes_v1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exchange and not $require_quotes_v1) { } 0 0 0
Condition0%0/3
$exchange and not $require_quotes_v1
!ll&&!rl&&r
000
MC/DC0%0/2
$exchange and not $require_quotes_v1
$exchange not $require_quotes_v1
Truth table0%0/3
A and not(B)
A= $exchangeB= not $require_quotes_v1
ABresult
0X0
100
111
178 0 %mapping = (%mapping, %map_exchanges_v2);
179 } else {
180 0 %mapping = (%mapping, %map_quotes_v1);
181 }
182  
183 0 my %map_basic_v1_id = (# BasicV1.ID keys
184 'symbol' => 'SYMBOL',
185 'wkn' => 'WKN',
186 'isin' => 'ISIN',
187 'notation_id' => 'ID_NOTATION');
188  
189 0 for my $symbol (@symbols) {
190  
191 ### $symbol
192  
193 0 $info{ $symbol, 'symbol' } = $symbol;
194 0 $info{ $symbol, 'success' } = 0;
195 0 $info{ $symbol, 'errormsg' } = '';
196  
197 0 ▶ my $get_json = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
198 0 my $query = shift;
199 0 my $response = $ua->get($query);
200  
201 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
202 0 $info{ $symbol, 'errormsg' } = "Unable to fetch data from the Consorsbank server for $symbol. Error: " . $response->status_line;
203 0 return;
204 }
205  
206 0 ▶ unless ($response->header('content-type') =~ m|application/json|i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->header("content-type") =~ m[application/json]i) 0 0 0
207 0 $info{ $symbol, 'errormsg' } = "Invalid content-type from Consorsbank server for $symbol. Expected: application/json, received: " . $response->header('content-type');
208 0 return;
209 }
210  
211 0 my $json = encode_utf8($response->content);
212  
213 ### [<here>] $json:
214 ### $json
215  
216 0 my $data;
217 0 eval { $data = JSON::decode_json($json) };
218  
219 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
220 0 $info{ $symbol, 'errormsg' } = "Failed to parse JSON data for $symbol. Error: $@.";
221 ### $@
222 0 return;
223 }
224  
225 ### [<here>] $data:
226 ### $data
227  
228 0 ▶ if ( defined $data->[0]{'Info'}{'Errors'} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->[0]{"Info"}{"Errors"}) 0 0 0
229 ### API Error: $data->[0]{'Info'}{'Errors'}
230  
231 0 ▶ if ( $data->[0]{'Info'}{'Errors'}[0]{'ERROR_CODE'} eq 'IDMS' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($data->[0]{'Info'}{'Errors'}[0]{'ERROR_CODE'} eq 'IDMS') { } 0 0 0
232 0 $info{ $symbol, 'errormsg' } = "Invalid symbol: $symbol";
233 } else {
234 0 $info{ $symbol, 'errormsg' } = $data->[0]{'Info'}{'Errors'}[0]{'ERROR_MESSAGE'}
235 }
236 0 return;
237 }
238  
239 0 return $data;
240 0 };
241  
242 0 my ($data, $quote);
243 0 ▶ if ($exchange) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exchange) { } 0 0 0
244 0 ▶ $data = &$get_json($CONSORS_URL . "id=$symbol&field=ExchangesV2&field=BasicV1") or next;
Branch: 1 branch
BranchTrueFalseTotal
$data = &$get_json($CONSORS_URL . "id=$symbol&field=ExchangesV2&field=BasicV1") or next 0 0 0
245 0 $info{ $symbol, 'exchanges' } = [ map { $_->{'CONSORS_EXCHANGE_NAME'} } @{$data->[0]{'ExchangesV2'}} ];
246  
247 ($quote) = grep { $_->{'CONSORS_EXCHANGE_NAME'} eq $exchange
248 0 ▶ or $_->{'CONSORS_EXCHANGE_CODE'} eq '_' .$exchange
Condition0%0/3
$_->{'CONSORS_EXCHANGE_NAME'} eq $exchange || $_->{'CONSORS_EXCHANGE_CODE'} eq '_' . $exchange
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{'CONSORS_EXCHANGE_NAME'} eq $exchange || $_->{'CONSORS_EXCHANGE_CODE'} eq '_' . $exchange
$_->{'CONSORS_EXCHANGE_NAME'} eq $exchange $_->{'CONSORS_EXCHANGE_CODE'} eq '_' . $exchange
Truth table0%0/3
A || B
A= $_->{'CONSORS_EXCHANGE_NAME'} eq $exchangeB= $_->{'CONSORS_EXCHANGE_CODE'} eq '_' . $exchange
ABresult
1X1
011
000
249 0 } @{$data->[0]{'ExchangesV2'}};
250 0 ▶ unless($quote) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quote) 0 0 0
251 0 $info{ $symbol, 'errormsg' } = "Marketplace not found: $exchange";
252 0 next;
253 }
254 0 ▶ if ($require_quotes_v1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($require_quotes_v1) 0 0 0
255 0 my ($id, $code) = ($quote->{'CONSORS_ID'}, $quote->{'CONSORS_EXCHANGE_CODE'});
256 0 ▶ $data = &$get_json($CONSORS_URL . "id=$id&field=QuotesV1&field=BasicV1&rtExchangeCode=$code") or next;
Branch: 1 branch
BranchTrueFalseTotal
$data = &$get_json($CONSORS_URL . "id=$id&field=QuotesV1&field=BasicV1&rtExchangeCode=$code") or next 0 0 0
257 0 $quote = $data->[0]{'QuotesV1'}[0];
258 }
259 } else {
260 0 ▶ if ($require_exchanges) {
Branch: 1 branch
BranchTrueFalseTotal
if ($require_exchanges) { } 0 0 0
261 0 ▶ $data = &$get_json($CONSORS_URL . "id=$symbol&field=QuotesV1&field=BasicV1&field=ExchangesV2") or next;
Branch: 1 branch
BranchTrueFalseTotal
$data = &$get_json($CONSORS_URL . "id=$symbol&field=QuotesV1&field=BasicV1&field=ExchangesV2") or next 0 0 0
262 0 $info{ $symbol, 'exchanges' } = [ map { $_->{'CONSORS_EXCHANGE_NAME'} } @{$data->[0]{'ExchangesV2'}} ];
263 } else {
264 0 ▶ $data = &$get_json($CONSORS_URL . "id=$symbol&field=QuotesV1&field=BasicV1") or next;
Branch: 1 branch
BranchTrueFalseTotal
$data = &$get_json($CONSORS_URL . "id=$symbol&field=QuotesV1&field=BasicV1") or next 0 0 0
265 }
266 0 $quote = $data->[0]{'QuotesV1'}[0];
267  
268 #$quote = $data->[0]{'PriceV2'};
269 }
270  
271 ### [<here>] $symbol:
272 ### $symbol
273  
274 0 ▶ while ((my $fqkey, my $cbkey) = each (%map_basic_v1_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %map_basic_v1_id) 0 0 0
275 0 ▶ $info{ $symbol, $fqkey } = $data->[0]{'BasicV1'}{'ID'}{$cbkey} if (defined $data->[0]{'BasicV1'}{'ID'}{$cbkey});
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->[0]{"BasicV1"}{"ID"}{$cbkey} 0 0 0
276 }
277  
278 0 ▶ $info{ $symbol, 'name' } = $data->[0]{'BasicV1'}{'NAME_SECURITY'} if (defined $data->[0]{'BasicV1'}{'NAME_SECURITY'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->[0]{"BasicV1"}{"NAME_SECURITY"} 0 0 0
279 0 $info{ $symbol, 'method' } = 'consorsbank';
280 0 $info{ $symbol, 'source' } = $CONSORS_SOURCE_BASE_URL . $data->[0]{'Info'}{'ID'};
281  
282 0 ▶ while ((my $fqkey, my $cbkey) = each (%mapping)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %mapping) 0 0 0
283 0 ▶ $info{ $symbol, $fqkey } = $quote->{$cbkey} if (exists $quote->{ $cbkey } and defined $quote->{ $cbkey } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $quote->{$cbkey} and defined $quote->{$cbkey} 0 0 0
Condition0%0/3
exists $quote->{$cbkey} and defined $quote->{$cbkey}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $quote->{$cbkey} and defined $quote->{$cbkey}
exists $quote->{$cbkey} defined $quote->{$cbkey}
Truth table0%0/3
A and B
A= exists $quote->{$cbkey}B= defined $quote->{$cbkey}
ABresult
0X0
100
111
284 }
285  
286 # in QuotesV1, PriceV2, ExchangesV2
287 0 ▶ if (defined($quote->{'DATETIME_PRICE'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $quote->{"DATETIME_PRICE"}) 0 0 0
288 0 my $utc_timestamp = str2time($quote->{'DATETIME_PRICE'});
289 0 $info{ $symbol, 'time' } = strftime("%H:%M", localtime($utc_timestamp)); # local time zone
290 0 $quoter->store_date(\%info, $symbol, {isodate => strftime("%Y-%m-%d", localtime($utc_timestamp))});
291 }
292  
293 $info{ $symbol, 'day_range' } = $info{ $symbol, 'low' } . ' - ' . $info{ $symbol, 'high' }
294 0 ▶ if (exists $info{ $symbol, 'low' } && exists $info{ $symbol, 'high' });
Branch: 1 branch
BranchTrueFalseTotal
if exists $info{join $;, $symbol, "low"} and exists $info{join $;, $symbol, "high"} 0 0 0
Condition0%0/3
exists $info{join $;, $symbol, "low"} and exists $info{join $;, $symbol, "high"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $info{join $;, $symbol, "low"} and exists $info{join $;, $symbol, "high"}
exists $info{join $;, $symbol, "low"} exists $info{join $;, $symbol, "high"}
Truth table0%0/3
A and B
A= exists $info{join $;, $symbol, "low"}B= exists $info{join $;, $symbol, "high"}
ABresult
0X0
100
111
295  
296 $info{ $symbol, 'year_range' } = $info{ $symbol, 'year_low' } . ' - ' . $info{ $symbol, 'year_high' }
297 0 ▶ if (exists $info{ $symbol, 'year_low' } && exists $info{ $symbol, 'year_high' });
Branch: 1 branch
BranchTrueFalseTotal
if exists $info{join $;, $symbol, "year_low"} and exists $info{join $;, $symbol, "year_high"} 0 0 0
Condition0%0/3
exists $info{join $;, $symbol, "year_low"} and exists $info{join $;, $symbol, "year_high"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $info{join $;, $symbol, "year_low"} and exists $info{join $;, $symbol, "year_high"}
exists $info{join $;, $symbol, "year_low"} exists $info{join $;, $symbol, "year_high"}
Truth table0%0/3
A and B
A= exists $info{join $;, $symbol, "year_low"}B= exists $info{join $;, $symbol, "year_high"}
ABresult
0X0
100
111
298  
299 0 ▶ unless (defined $info{ $symbol, 'last'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info{join $;, $symbol, "last"}) 0 0 0
300 0 $info{ $symbol, 'errormsg' } = "The server did not return a price for $symbol.";
301 0 next;
302 }
303  
304 0 $info{ $symbol, 'success' } = 1;
305 }
306  
307 ### [<here>] %info:
308 ### %info
309  
310 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
311 }
312 5 1;
313 __END__
« Quote/FTfunds.pm ↑ Summary Quote/AlphaVantage.pm »