Quote/Consorsbank.pm

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