-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObligacjeSP.pm
257 lines (208 loc) · 8.7 KB
/
ObligacjeSP.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Copyright (C) 1998, Dj Padzensky <[email protected]>
# Copyright (C) 1998, 1999 Linas Vepstas <[email protected]>
# Copyright (C) 2000, Yannick LE NY <[email protected]>
# Copyright (C) 2000, Paul Fenwick <[email protected]>
# Copyright (C) 2000, Brent Neal <[email protected]>
# Copyright (C) 2000, Keith Refson <[email protected]>
# Copyright (C) 2003, Tomas Carlsson <[email protected]>
# Copytight (C) 2022-2024 Kaligula <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA
#
#
# This code was derived from the work on the packages Finance::Yahoo::*
# This code was derived from the work on the packages Finance::SEB
# This code was derived from the work on the packages Finance::Stooq
#
package Finance::Quote::ObligacjeSP;
require 5.004;
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use vars qw($VERSION $NN_OFE_URL);
use utf8;
use DateTime;
$VERSION = '0.10';
sub methods { return (obligacje_sp => \&obligacje_sp); }
{
my @labels = qw/date isodate method source name currency last price/;
sub labels { return (obligacje_sp => \@labels); }
}
sub obligacje_sp {
my $quoter = shift;
my @symbols = @_;
return unless @symbols;
my %stocks;
my %duration = ( #in years
'OTS' => 0.25,
'ROR' => 1,
'DOR' => 2,
'TOS' => 3,
'COI' => 4,
'EDO' => 10,
'ROS' => 6,
'ROD' => 12
);
# TODO: Wywal błąd jeśli nie ma takiego symbolu
my $debug = '';
#
# get CPI from API
#
#find year of the oldest inflation rated bond
my $CPIYearStart = 9999;
my $CPIMonthStart = 12;
foreach my $symbol (@symbols) {
my ($type, $seriesMonth, $seriesYearTo, $seriesDay, $rateFirstYear, $rateNextYears) = ($symbol =~ /([A-Z]{3})(\d{2})(\d{2})-d(\d{2})-p(\d+\.\d+)(?:-i(\d+\.\d+))/);
$seriesYearTo += 2000;
my $seriesYearFrom = $seriesYearTo - $duration{$type};
if ($type eq "COI" || $type eq "EDO" || $type eq "ROS" || $type eq "ROD" ) { #indeksowane inflacją
if ($seriesYearFrom <= $CPIYearStart) {
$CPIYearStart = $seriesYearFrom;
if ($seriesMonth < $CPIMonthStart) { $CPIMonthStart = $seriesMonth; }
}
}
}
my ($nowDay, $nowMonth, $nowYear) = (localtime())[3,4,5];
$nowMonth++;
$nowYear += 1900;
# CPI ogłaszana w m-cu ($seriesMonth-1) czyli de facto za m-c ($seriesMonth-2)
# https://stooq.pl/q/d/l/?s=cpiypl.m&d1=20220901&d2=20220931&i=m&o=0101000&c=1
my %CPIHash;
#HTTP request
unless ($CPIYearStart == 9999) {
# pobierz ze Stooq, odfiltruj i zapisz
my $ua = $quoter->user_agent;
my $url = 'https://stooq.pl/q/d/l/?s=cpiypl.m&d1='.$CPIYearStart.sprintf("%02d",$CPIMonthStart).'01&d2='.$nowYear.sprintf("%02d",$nowMonth).'31&i=m&o=0101000&c=1';
$debug = "$debug\nurl: $url";
my $reply = $ua->request(GET $url);
unless ($reply->is_success) {
foreach my $symbol (@symbols) {
$stocks{$symbol, "success"} = 0;
$stocks{$symbol, "errormsg"} = "HTTP failure";
return wantarray ? %stocks : \%stocks;
}
}
#my ($line) = split(/\n/, $reply->content, 1);
my @lines = split(/\n/, $reply->content);
foreach my $line (@lines) {
#chomp($line);
# Format:
# Date;Open;High;Low;Close
# 2022-09-30;17.2;17.2;17.2;17.2
my ($date, $open, $high, $low, $last) = split ';', $line;
unless ($date eq 'Data') { #unless 1st line
my @dateArr = split '-', $date;
my $dateShort = $dateArr[0].$dateArr[1];
$CPIHash{$dateShort} = $last/100;
}
}
}
foreach my $symbol (@symbols) {
$debug = "$debug\n$symbol";
my ($type, $seriesMonth, $seriesYearTo, $seriesDay, $rateFirstYear, $rateNextYears) = ($symbol =~ /([A-Z]{3})(\d{2})(\d{2})-d(\d{2})-p(\d+\.\d+)(?:-i(\d+\.\d+))/);
$rateFirstYear = $rateFirstYear / 100;
$rateNextYears = $rateNextYears / 100;
my $series = $type.$seriesMonth.$seriesYearTo; #e.g. 'EDO1131'
$seriesYearTo += 2000; #e.g. '2031'
my $seriesYearFrom = $seriesYearTo - $duration{$type};
# my $dateTo = $year.$month.$day;
# ($day, $month, $year) = (localtime(time()-7*24*3600))[3,4,5];
# $month++;
# $year += 1900;
# my $dateFrom = $year.$month.$day;
my $yearsPassed = $nowYear - $seriesYearFrom; #how many full years have passed since series start
if ( ($nowMonth < $seriesMonth) || ($nowMonth == $seriesMonth && $nowDay < $seriesDay) ) {
$yearsPassed--;
}
my $dt1 = DateTime->new(year => $seriesYearFrom+$yearsPassed, month => $seriesMonth, day => $seriesDay); #series start
my $dt2 = DateTime->new(year => $nowYear, month => $nowMonth, day => $nowDay); #today
my $dt3 = DateTime->new(year => $seriesYearFrom+$yearsPassed+1, month => $seriesMonth, day => $seriesDay); #series 1st year end
my $daysPassed = $dt2->delta_days($dt1)->delta_days(); #days passed until today
my $daysInYear = $dt3->delta_days($dt1)->delta_days(); #days in the first year
$debug = "$debug\nrates: $rateFirstYear / infl+$rateNextYears";
$debug = "$debug\nyearsPassed: $yearsPassed";
my $ileTeraz = 1;
if ($yearsPassed == 0) {
$ileTeraz = 1 + $rateFirstYear*($daysPassed/$daysInYear);
} else {
# TODO: domyślnie dolicza inflację, ale nie wszystkie bondy są inflacyjne!
my $CPIJanFebCorrectionY = 0;
my $CPIJanFebCorrectionM = 0;
if ($seriesMonth < 3) { # dla obligacji ze stycznia/lutego odczyt CPI jest z poprz. roku
$CPIJanFebCorrectionY = -1;
$CPIJanFebCorrectionM = 12;
}
# w TOS/EDO/ROS/ROD odsetki co rok są kapitalizowane, a w in. nie! (są wypłacane)
if ($type eq "TOS" || $type eq "EDO" || $type eq "ROS" || $type eq "ROD" ) {
$ileTeraz = 1 + $rateFirstYear; #after 1 year
$debug = "$debug\nilepo1: $ileTeraz";
for (2..$yearsPassed) { #after next years
$ileTeraz = $ileTeraz * (1 + $CPIHash{($seriesYearFrom+$_-1+$CPIJanFebCorrectionY).(sprintf("%02d",$seriesMonth-2+$CPIJanFebCorrectionM))}+$rateNextYears);
$debug = "$debug\nCPIHash: ".($seriesYearFrom+$_-1+$CPIJanFebCorrectionY).(sprintf("%02d",$seriesMonth-2+$CPIJanFebCorrectionM));
$debug = "$debug\nCPI: ".$CPIHash{($seriesYearFrom+$_-1+$CPIJanFebCorrectionY).(sprintf("%02d",$seriesMonth-2+$CPIJanFebCorrectionM))};
$debug = "$debug\nilepo$_: $ileTeraz";
}
}
# odsetki naliczone w bieżącym roku odsetkowym
$ileTeraz = $ileTeraz * (1 + ($CPIHash{($seriesYearFrom+$yearsPassed+$CPIJanFebCorrectionY).(sprintf("%02d",$seriesMonth-2+$CPIJanFebCorrectionM))}+$rateNextYears)*($daysPassed/$daysInYear) );
$debug = "$debug\nCPIHash: ".($seriesYearFrom+$yearsPassed+$CPIJanFebCorrectionY).(sprintf("%02d",$seriesMonth-2+$CPIJanFebCorrectionM));
$debug = "$debug\nCPI: ".$CPIHash{($seriesYearFrom+$yearsPassed+$CPIJanFebCorrectionY).(sprintf("%02d",$seriesMonth-2+$CPIJanFebCorrectionM))};
$debug = "$debug\nileTeraz: $ileTeraz";
$debug = "$debug\ndaysPasseddaysInYear: ".($daysPassed/$daysInYear);
}
$ileTeraz = sprintf("%.2f", 100 * $ileTeraz ); #round to 2 decimal points
$debug = "$debug\nileTeraz: $ileTeraz";
# Format:
$stocks{$symbol, 'symbol'} = $symbol;
# $quoter->store_date(\%stocks, $symbol, { isodate => $date });
$quoter->store_date(\%stocks, $symbol, { today => 1 });
$stocks{$symbol, 'method'} = 'obligacje_sp';
$stocks{$symbol, 'source'} = 'Finance::Quote::ObligacjeSP';
$stocks{$symbol, 'name'} = $type.$seriesMonth.($seriesYearTo-2000);
$stocks{$symbol, 'currency'} = 'PLN';
$stocks{$symbol, 'last'} = $ileTeraz;
$stocks{$symbol, 'price'} = $ileTeraz;
$stocks{$symbol, 'debug'} = $debug;
$stocks{$symbol, 'success'} = 1;
}
# Check for undefined symbols
foreach my $symbol (@symbols) {
unless ($stocks{$symbol, 'success'}) {
$stocks{$symbol, "success"} = 0;
$stocks{$symbol, "errormsg"} = "Stock name not found";
}
}
return %stocks if wantarray;
return \%stocks;
}
1;
=head1 NAME
Finance::Quote::ObligacjeSP - Obtain prices of gov bonds
=head1 SYNOPSIS
use Finance::Quote;
$q = Finance::Quote->new;
%stockinfo = $q->fetch("obligacje_sp","COI0127-d29-p7.00-i1.00"); # the letter ticker
=head1 DESCRIPTION
[todo]
=head1 BOND NAMES
[todo]
=head1 LABELS RETURNED
[todo]
date method source name currency price last
=head1 SEE ALSO
Gov Bonds website - https://www.obligacjeskarbowe.pl/
STOOQ website - https://stooq.com/ or https://stooq.pl/
=cut