Debian Codegraph @

lib/Finance/Quote/SIX.pm

raw
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch:
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
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 #
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 package Finance::Quote::SIX;
20
21 use strict;
22 use warnings;
23
24 use constant DEBUG => $ENV{DEBUG};
25 use if DEBUG, 'Smart::Comments';
26
27 use LWP::UserAgent;
28 use JSON qw( decode_json );
29 use String::Util qw(trim);
30 use Scalar::Util qw(looks_like_number);
31
32 our $VERSION = '1.71'; # VERSION
33
34 our $DISPLAY = 'SIX - Swiss Stock Exchange';
35 our @LABELS = qw/last close date isodate symbol currency isin name/;
36 our $METHODHASH = {subroutine => \&six,
37 display => $DISPLAY,
38 labels => \@LABELS};
39
40 sub methodinfo {
41 return (
42 six => $METHODHASH,
43 );
44 }
45
46 sub labels {
47 my %m = methodinfo();
48 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
49 }
50
51 sub methods {
52 my %m = methodinfo();
53 return map {$_ => $m{$_}{subroutine} } keys %m;
54 }
55
56 sub six {
57 my $quoter = shift;
58 my @symbols = @_;
59 my $ua = $quoter->user_agent();
60 my %info;
61
62 foreach my $symbol (@_) {
63 eval {
64 # 1. Search for the security
65 my $url = 'https://www.six-group.com/fqs/snap.json?select=ValorId,PortalSegment,ProductLine&where=PortalSegment=EQ|BO|FU|EP|IN&pagesize=2&match=' . $symbol;
66 my $reply = $ua->get($url);
67 my $search = JSON::decode_json $reply->content;
68
69 ### Search : $url, $reply->code
70 ### Search : $search
71
72 # 2. Get security metadata
73 my $valorid = $search->{rowData}->[0][0];
74 die "$symbol not found" unless defined $valorid;
75
76 $url = 'https://www.six-group.com/fqs/ref.json?select=DividendEntitlementFlag,FirstTradingDate,LastTradingDate,ISIN,IssuerNameFull,IssuerNameShort,MarketDate,NominalCurrency,NominalValue,NumberInIssue,ProductLine,SecTypeDesc,ShortName,SmallestTradeableUnit,TitleSegment,TitleSegmentDesc,TradingBaseCurrency,ValorNumber,ValorSymbol&where=ValorId=' . $valorid;
77 $reply = $ua->get($url);
78 my $metadata = JSON::decode_json $reply->content;
79
80 ### Metadata : $url, $reply->code
81 ### Metadata : $metadata
82
83 my @metacols = @{$metadata->{colNames}};
84 my %metamap = map {$metacols[$_] => $_} (0 .. $#metacols);
85 my $metarow = $metadata->{rowData}->[0];
86
87 $info{$symbol, 'symbol'} = $metarow->[$metamap{ValorSymbol}];
88 $info{$symbol, 'isin'} = $metarow->[$metamap{ISIN}];
89 $info{$symbol, 'name'} = $metarow->[$metamap{IssuerNameFull}];
90 $info{$symbol, 'currency'} = $metarow->[$metamap{TradingBaseCurrency}];
91
92 $quoter->store_date(\%info, $symbol, {isodate => $metarow->[$metamap{MarketDate}]});
93
94 $url = 'https://www.six-group.com/fqs/movie.json?select=AskPrice,AskVolume,BidPrice,BidVolume,ClosingDelta,ClosingPerformance,ClosingPrice,DailyHighPrice,DailyHighTime,DailyLowPrice,DailyLowTime,LatestTradeVolume,MarketMakers,MarketTime,MidSpread,OffBookTrades,OffBookTurnover,OffBookVolume,OnMarketTrades,OnMarketTurnover,OnMarketVolume,OpeningPrice,PreviousClosingPrice,SwissAtMidTrades,SwissAtMidTurnover,SwissAtMidVolume,TotalVolume,VWAP60Price,YearAgoPerformance,YearlyHighDate,YearlyHighPrice,YearlyLowDate,YearlyLowPrice,YearToDatePerformance,YieldToWorst&where=ValorId=' . $valorid;
95 $reply = $ua->get($url);
96 my $data = JSON::decode_json $reply->content;
97
98 ### Data : $url, $reply->code
99 ### Data : $data
100
101 my @datacols = @{$data->{colNames}};
102 my %datamap = map {$datacols[$_] => $_} (0 .. $#datacols);
103 my $datarow = $data->{rowData}->[0];
104
105 $info{$symbol, 'ask'} = $datarow->[$datamap{AskPrice}] if $datarow->[$datamap{AskPrice}] and looks_like_number($datarow->[$datamap{AskPrice}]);
106 $info{$symbol, 'close'} = $datarow->[$datamap{ClosingPrice}] if $datarow->[$datamap{ClosingPrice}];
107 $info{$symbol, 'high'} = $datarow->[$datamap{DailyHighPrice}] if $datarow->[$datamap{DailyHighPrice}];
108 $info{$symbol, 'low'} = $datarow->[$datamap{DailyLowPrice}] if $datarow->[$datamap{DailyLowPrice}];
109 $info{$symbol, 'open'} = $datarow->[$datamap{OpeningPrice}] if $datarow->[$datamap{OpeningPrice}];
110 $info{$symbol, 'last'} = $datarow->[$datamap{ClosingPrice}] if $datarow->[$datamap{ClosingPrice}];
111 $info{$symbol, 'volume'} = $datarow->[$datamap{TotalVolume}] if $datarow->[$datamap{TotalVolume}];
112 $info{$symbol, 'success'} = 1;
113 };
114
115 if ($@) {
116 my $error = "SIX failed: $@";
117 $info{$symbol, 'success'} = 0;
118 $info{$symbol, 'errormsg'} = trim($error);
119 }
120 }
121
122 return wantarray() ? %info : \%info;
123 }
124
125 1;
126
127 =head1 NAME
128
129 Finance::Quote::SIX - Obtain quotes from the Swiss Stock Exchange
130
131 =head1 SYNOPSIS
132
133 use Finance::Quote;
134
135 $q = Finance::Quote->new;
136
137 %info = Finance::Quote->fetch('six', 'NESN');
138
139 =head1 DESCRIPTION
140
141 This module fetches information from the Swiss Stock Exchange,
142 https://www.six-group.com.
143
144 This module is loaded by default on a Finance::Quote object. It's also possible
145 to load it explicitly by placing 'SIX' in the argument list to
146 Finance::Quote->new().
147
148 =head1 LABELS RETURNED
149
150 The following labels may be returned by Finance::Quote::SIX :
151 isin name currency date isodate ask close high low open volume success
152
153 =head1 TERMS & CONDITIONS
154
155 Use of www.six-group.com is governed by any terms & conditions of that site.
156
157 Finance::Quote is released under the GNU General Public License, version 2,
158 which explicitly carries a "No Warranty" clause.
159
160 =cut
161