| 1 | #!/usr/bin/perl -w | ||||||||||||||
| 2 | # | ||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||||
| 7 | # | ||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||||
| 12 | # | ||||||||||||||
| 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 | # | ||||||||||||||
| 19 | # $Id: $ | ||||||||||||||
| 20 | |||||||||||||||
| 21 | package Finance::Quote::TesouroDireto; | ||||||||||||||
| 22 | 5 | require 5.10.1; | |||||||||||||
| 23 | |||||||||||||||
| 24 | 5 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 5 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 5 | ▶ | use if DEBUG, 'Smart::Comments'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | |||||||||||||||
| 30 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 5 | ▶ | use HTTP::Request::Common; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 5 | ▶ | use Time::Piece; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 5 | our $VERSION = '1.72'; # VERSION | |||||||||||||
| 35 | |||||||||||||||
| 36 | 5 | our $DISPLAY = 'Brazilian Govt Bonds, tesouro_direto'; | |||||||||||||
| 37 | 5 | our $FEATURES = {}; | |||||||||||||
| 38 | 5 | our @LABELS = qw/exchange date isodate symbol name price last method currency/; | |||||||||||||
| 39 | 5 | our $METHODHASH = {subroutine => \&tesouro, | |||||||||||||
| 40 | display => $DISPLAY, | ||||||||||||||
| 41 | labels => \@LABELS, | ||||||||||||||
| 42 | features => $FEATURES}; | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 10 | ▶ | sub methodinfo { | ||||||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 45 | return ( | ||||||||||||||
| 46 | 10 | tesouro_direto => $METHODHASH, | |||||||||||||
| 47 | ); | ||||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 5 | ▶ | sub methods { | ||||||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 51 | 5 | my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 5 | ▶ | sub labels { | ||||||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 55 | 5 | my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | # Sanitizes input from tesouro's csv (x.xxx,xx) to US values (xxxx.xx) | ||||||||||||||
| 59 | 0 | ▶ | sub convert_price { | ||||||||||||
|
Subroutine
convert_price: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 60 | 0 | $_ = shift; | |||||||||||||
| 61 | |||||||||||||||
| 62 | # Replaces ',' with '.' and removes the other "extra" characters | ||||||||||||||
| 63 | 0 | tr/,. /./d ; | |||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | return $_; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | # Returns the bond name (concatenation of bond type and year). | ||||||||||||||
| 69 | # Some "special" bonds are paid in installments; the name of these | ||||||||||||||
| 70 | # bonds uses the year of the first installment, however the due date | ||||||||||||||
| 71 | # from the csv refers to the last one. | ||||||||||||||
| 72 | 0 | ▶ | sub get_bond_name { | ||||||||||||
|
Subroutine
get_bond_name: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 73 | 0 | my ($name, $date) = @_; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | my $year = substr($date, 6, 4); | |||||||||||||
| 76 | |||||||||||||||
| 77 | # Apply the conditional logic based on the value of $name. | ||||||||||||||
| 78 | 0 | ▶ | if ($name eq 'Tesouro Renda+ Aposentadoria Extra') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 79 | 0 | return "$name " . ($year - 19); | |||||||||||||
| 80 | } elsif ($name eq 'Tesouro Educa+') { | ||||||||||||||
| 81 | 0 | return "$name " . ($year - 4); | |||||||||||||
| 82 | } else { | ||||||||||||||
| 83 | # Otherwise, return a string of the name and the original year. | ||||||||||||||
| 84 | 0 | return "$name $year"; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | sub tesouro | ||||||||||||||
| 89 | 0 | ▶ | { | ||||||||||||
|
Subroutine
tesouro: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||
| 90 | 0 | my $quoter = shift; | |||||||||||||
| 91 | 0 | my @funds = @_; | |||||||||||||
| 92 | 0 | ▶ | return unless @funds; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | my $ua = $quoter->user_agent; | |||||||||||||
| 95 | |||||||||||||||
| 96 | 0 | my (%fundsymbol, %fundhash, @q, %info); | |||||||||||||
| 97 | |||||||||||||||
| 98 | # create hash of all funds requested | ||||||||||||||
| 99 | 0 | foreach my $fund (@funds) | |||||||||||||
| 100 | { | ||||||||||||||
| 101 | 0 | $fundhash{$fund} = 0; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 0 | my $url = "https://www.tesourotransparente.gov.br/ckan/dataset/df56aa42-484a-4a59-8184-7676580c81e3/resource/796d2059-14e9-44e3-80c9-2d9e30b405c1/download/precotaxatesourodireto.csv"; | |||||||||||||
| 105 | 0 | my $response = $ua->request(GET $url); | |||||||||||||
| 106 | |||||||||||||||
| 107 | 0 | ▶ | if ($response->is_success) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 108 | |||||||||||||||
| 109 | # process csv data | ||||||||||||||
| 110 | 0 | foreach (split('\015?\012',$response->content)) { | |||||||||||||
| 111 | |||||||||||||||
| 112 | 0 | ▶ | @q = $quoter->parse_csv_semicolon($_) or next; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | 0 | ▶ | next unless (defined $q[0]); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | |||||||||||||||
| 115 | 0 | my $bond_name = get_bond_name($q[0], $q[1]); | |||||||||||||
| 116 | |||||||||||||||
| 117 | 0 | ▶ | if (exists $fundhash{$bond_name}) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | { | ||||||||||||||
| 119 | 0 | my $t = Time::Piece->strptime($q[2], "%d/%m/%Y"); | |||||||||||||
| 120 | 0 | ▶ | next unless ($t->epoch > $fundhash{$bond_name}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | |||||||||||||||
| 122 | 0 | $fundhash{$bond_name} = $t->epoch; | |||||||||||||
| 123 | |||||||||||||||
| 124 | 0 | $info{$bond_name, "exchange"} = "Tesouro Direto"; | |||||||||||||
| 125 | 0 | $info{$bond_name, "name"} = $bond_name; | |||||||||||||
| 126 | 0 | $info{$bond_name, "symbol"} = $bond_name; | |||||||||||||
| 127 | 0 | $info{$bond_name, "price"} = convert_price($q[6]); | |||||||||||||
| 128 | 0 | $info{$bond_name, "last"} = convert_price($q[6]); | |||||||||||||
| 129 | 0 | $quoter->store_date(\%info, $bond_name, {eurodate => $q[2]}); | |||||||||||||
| 130 | 0 | $info{$bond_name, "method"} = "tesouro_direto"; | |||||||||||||
| 131 | 0 | $info{$bond_name, "currency"} = "BRL"; | |||||||||||||
| 132 | 0 | $info{$bond_name, "success"} = 1; | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | # check to make sure a value was returned for every fund requested | ||||||||||||||
| 137 | 0 | foreach my $fund (keys %fundhash) | |||||||||||||
| 138 | { | ||||||||||||||
| 139 | 0 | ▶ | if ($fundhash{$fund} == 0) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 140 | { | ||||||||||||||
| 141 | 0 | $info{$fund, "success"} = 0; | |||||||||||||
| 142 | 0 | $info{$fund, "errormsg"} = "No data returned"; | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | } | ||||||||||||||
| 145 | } | ||||||||||||||
| 146 | else | ||||||||||||||
| 147 | { | ||||||||||||||
| 148 | 0 | foreach my $fund (@funds) | |||||||||||||
| 149 | { | ||||||||||||||
| 150 | 0 | $info{$fund, "success"} = 0; | |||||||||||||
| 151 | 0 | $info{$fund, "errormsg"} = "HTTP error"; | |||||||||||||
| 152 | } | ||||||||||||||
| 153 | } | ||||||||||||||
| 154 | |||||||||||||||
| 155 | ### result: %info | ||||||||||||||
| 156 | |||||||||||||||
| 157 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 158 | } | ||||||||||||||
| 159 | |||||||||||||||
| 160 | |||||||||||||||
| 161 | 5 | 1; | |||||||||||||
| 162 | |||||||||||||||
| 163 | =head1 NAME | ||||||||||||||
| 164 | |||||||||||||||
| 165 | Finance::Quote::TesouroDireto - Obtain quotes for Brazilian government bounds | ||||||||||||||
| 166 | |||||||||||||||
| 167 | =head1 SYNOPSIS | ||||||||||||||
| 168 | |||||||||||||||
| 169 | use Finance::Quote; | ||||||||||||||
| 170 | $q = Finance::Quote->new; | ||||||||||||||
| 171 | |||||||||||||||
| 172 | %stockinfo = $q->fetch("tesouro_direto", "Tesouro IPCA+ 2045"); | ||||||||||||||
| 173 | |||||||||||||||
| 174 | =head1 DESCRIPTION | ||||||||||||||
| 175 | |||||||||||||||
| 176 | This module obtains quotes for Brazilian government bounds, obtained from | ||||||||||||||
| 177 | the dataset released by Secretaria do Tesouro Nacional on Tesouro | ||||||||||||||
| 178 | Transparente under the Open Database License (ODbL): | ||||||||||||||
| 179 | |||||||||||||||
| 180 | https://www.tesourotransparente.gov.br/ckan/dataset/taxas-dos-titulos-ofertados-pelo-tesouro-direto | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =head1 LABELS RETURNED | ||||||||||||||
| 183 | |||||||||||||||
| 184 | The following labels may be returned by Finance::Quote::TesouroDireto: | ||||||||||||||
| 185 | exchange, name, symbol, date, price, last, method, currency. | ||||||||||||||
| 186 | |||||||||||||||
| 187 | =head1 SEE ALSO | ||||||||||||||
| 188 | |||||||||||||||
| 189 | =cut | ||||||||||||||