Quote/TreasuryDirect.pm

filter: Statementstmt 30.7% 36 / 117 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 7 totaltotal 21.4% 46 / 215 CC 45 SCAR 60.2
CC
45
Cov
19%
treasurydirect_ymd
812.0
price_from
72.0
parse_prices
56.0
CRAP 411.4
« Quote/AlphaVantage.pm ↑ Summary Quote/IndiaMutual.pm »
1 #!/usr/bin/perl -w
2 #
3 # TreasuryDirect.pm
4 #
5 # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch:
6  
7 =begin comment
8  
9 perl -MData::Dumper -MFinance::Quote -le '$q = Finance::Quote->new(); print Dumper { $q->fetch("treasurydirect", @ARGV) };' 912810QT8 912810QY7
10  
11 =end comment
12  
13 =cut
14  
15 package Finance::Quote::TreasuryDirect;
16 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 #
21 # Modification of Rolf Endres' Finance::Quote::ZA
22 #
23 # Peter Ratzlaff <pratzlaff@gmail.com>
24 # April, 2018
25 #
26  
27 5 our $VERSION = '1.72_01'; # TRIAL VERSION
28  
29 5 ▶use vars qw /$VERSION/ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 5 ▶use HTTP::CookieJar::LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 5 my $TREASURY_DIRECT_FORM_URL = 'https://www.treasurydirect.gov/GA-FI/FedInvest/selectSecurityPriceDate.htm';
37 5 my $TREASURY_DIRECT_URL = 'https://www.treasurydirect.gov/GA-FI/FedInvest/securityPriceDetail';
38  
39 5 our $DISPLAY = 'TreasuryDirect - US Treasury Bonds';
40 5 our @LABELS = qw/method source symbol rate maturity bid ask eod price date isodate currency/;
41 5 our $METHODHASH = {subroutine => \&treasurydirect,
42 display => $DISPLAY,
43 labels => \@LABELS};
44  
45 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
46 return (
47 10 treasurydirect => $METHODHASH,
48 );
49 }
50  
51 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
52 5 my %m = methodinfo();
53 5 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
54 }
55  
56 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
57 5 my %m = methodinfo();
58 5 return map {$_ => $m{$_}{subroutine} } keys %m;
59 }
60  
61 0 ▶sub treasurydirect {
Subroutine treasurydirect: not called CC=5 SCAR=34.0
Pod undocumented
62 0 my ($quoter, @symbols) = @_;
63  
64 # check for quotes for today, as well as the last three days
65  
66 0 my $time = time();
67 0 my @times = map { $time-86400*$_ } 0..3;
68  
69 0 for my $t (@times) {
70 0 my ($d, $m, $y) = (localtime($t))[3,4,5];
71 0 $y += 1900;
72 0 $m += 1;
73 0 my @quotes = treasurydirect_ymd($y, $m, $d, @_);
74 0 ▶ return @quotes if @quotes;
Branch: 1 branch
BranchTrueFalseTotal
if @quotes 0 0 0
75 }
76  
77 # Nothing was priced on any of those days. Say so, rather than returning an
78 # empty list the caller cannot tell apart from a lookup that never ran.
79 0 my %info;
80 0 for my $symbol (@symbols) {
81 0 $info{$symbol, 'success'} = 0;
82 0 $info{$symbol, 'errormsg'} = 'No prices published in the last four days';
83 }
84 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
85 }
86  
87 0 ▶sub treasurydirect_ymd {
Subroutine treasurydirect_ymd: not called CC=28 SCAR=67.0
Pod undocumented
88  
89 0 my ($y, $m, $d, $quoter, @symbols) = @_;
90  
91 0 ▶ return unless @symbols;
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols 0 0 0
92  
93 0 my %info;
94  
95 0 $info{$_, 'success'} = 0 for @symbols;
96  
97 # The form is Spring-backed: the POST needs a _csrf token bound to the
98 # cookie set when the form is fetched, and answers with a redirect LWP will
99 # not follow for POST unless asked.
100 0 my $ua = $quoter->user_agent;
101 0 ▶ $ua->cookie_jar(HTTP::CookieJar::LWP->new) unless $ua->cookie_jar;
Branch: 1 branch
BranchTrueFalseTotal
unless $ua->cookie_jar 0 0 0
102 0 $ua->requests_redirectable(['GET', 'HEAD', 'POST']);
103  
104 # Bound our own requests when the caller has expressed no preference. The
105 # retry loop makes up to eight of them and LWP's default is 180 seconds.
106 0 ▶ $ua->timeout(30) unless defined $quoter->get_timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $quoter->get_timeout 0 0 0
107  
108 0 my $form = $ua->request(GET $TREASURY_DIRECT_FORM_URL);
109 0 ▶ unless ($form->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($form->is_success) 0 0 0
110 0 $info{$_, 'errormsg'} = 'Error contacting URL' for @symbols;
111 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
112 }
113  
114 # Depends on the form's markup, so name it when it breaks; otherwise the
115 # resulting 403 reads as a network fault.
116 0 ▶ my ($csrf) = ($form->decoded_content // '') =~ /name="_csrf"\s*value="([^"]+)"/;
Condition0%0/2
$form->decoded_content // ""
l!l
00
MC/DC0%0/1
$form->decoded_content // ""
$form->decoded_content
Truth table0%0/2
A
A= $form->decoded_content
Aresult
11
00
117 0 ▶ unless (defined $csrf) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $csrf) 0 0 0
118 0 $info{$_, 'errormsg'} = 'No CSRF token in price form' for @symbols;
119 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
120 }
121  
122 0 my $response = $ua->request(
123 POST $TREASURY_DIRECT_URL,
124 Referer => $TREASURY_DIRECT_FORM_URL,
125 Content => [
126 priceDateDay => $d,
127 priceDateMonth => $m,
128 priceDateYear => $y,
129 fileType => 'xml',
130 xml => 'XML FORMAT',
131 _csrf => $csrf,
132 ]
133 );
134  
135 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
136 0 $info{$_, 'errormsg'} = 'Error contacting URL' for @symbols;
137 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
138 }
139  
140 0 my $bonds = parse_prices($response->decoded_content);
141 0 ▶ unless (defined $bonds) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bonds) 0 0 0
142 0 $info{$_, 'errormsg'} = 'Parse error' for @symbols;
143 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
144 }
145  
146 # A weekend or holiday. The file is only ever for the date asked for, so
147 # stepping back is the loop's job, not the server's.
148 0 ▶ return unless %{$bonds};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$bonds;} 0 0 0
149  
150 0 for my $symbol (@symbols) {
151  
152 # GENERAL FIELDS
153 0 $info{$symbol, 'method'} = 'treasurydirect';
154 0 $info{$symbol, 'symbol'} = $symbol;
155 0 $info{$symbol, 'source'} = $TREASURY_DIRECT_FORM_URL;
156  
157 # OTHER INFORMATION
158 0 my $bond = $bonds->{uc $symbol};
159 0 ▶ unless ($bond) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bond) 0 0 0
160 0 $info{$symbol, 'errormsg'} = 'no match';
161 0 next;
162 }
163  
164 0 my $price = price_from($bond);
165 0 ▶ unless (defined $price) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $price) 0 0 0
166 0 $info{$symbol, 'errormsg'} = 'no usable price';
167 0 next;
168 }
169  
170 0 $info{$symbol, 'success'} = 1;
171 0 $info{$symbol, 'currency'} = 'USD';
172 0 $info{$symbol, 'price'} = $price;
173  
174 # Publish only what was quoted: fields are strings and may be blank, and
175 # EODPrice is zero until after the close.
176 $info{$symbol, 'maturity'} = $bond->{maturity}
177 0 ▶ if length($bond->{maturity} // '');
Branch: 1 branch
BranchTrueFalseTotal
if length($bond->{"maturity"} // "") 0 0 0
Condition0%0/2
$bond->{"maturity"} // ""
l!l
00
MC/DC0%0/1
$bond->{"maturity"} // ""
$bond->{"maturity"}
Truth table0%0/2
A
A= $bond->{"maturity"}
Aresult
11
00
178 $info{$symbol, $_} = $bond->{$_}
179 0 ▶ for grep { looks_like_number($bond->{$_}) and $bond->{$_} > 0 } qw/bid ask eod/;
Condition0%0/3
&looks_like_number($bond->{$_}) && $bond->{$_} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_number($bond->{$_}) && $bond->{$_} > 0
&looks_like_number($bond->{$_}) $bond->{$_} > 0
Truth table0%0/3
A && B
A= &looks_like_number($bond->{$_})B= $bond->{$_} > 0
ABresult
0X0
100
111
180  
181 # Rate is a decimal fraction here; callers have always had a percentage.
182 $info{$symbol, 'rate'} = sprintf("%.3f%%", $bond->{rate} * 100)
183 0 ▶ if looks_like_number($bond->{rate});
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($bond->{"rate"}) 0 0 0
184  
185 0 $quoter->store_date(\%info, $symbol, {year => $y, month => $m, day => $d});
186 }
187  
188 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
189 }
190  
191 # CUSIP => {rate, maturity, bid, ask, eod}. undef if the document will not
192 # parse; empty hashref for a day with no prices. Separate so it can be tested
193 # without a network round trip.
194 0 ▶sub parse_prices {
Subroutine parse_prices: not called CC=7 SCAR=40.3
Pod undocumented
195 0 my $xml = shift;
196  
197 0 my $dom = eval { XML::LibXML->load_xml(string => $xml) };
198 0 ▶ return unless $dom;
Branch: 1 branch
BranchTrueFalseTotal
unless $dom 0 0 0
199  
200 0 my %bonds;
201 0 for my $security ($dom->getElementsByLocalName('Security')) {
202 0 my %field;
203 0 for my $child ($security->nonBlankChildNodes()) {
204 # nonBlankChildNodes filters whitespace, not comments, and localname is
205 # undef on those.
206 0 ▶ next unless $child->nodeType == XML_ELEMENT_NODE;
Branch: 1 branch
BranchTrueFalseTotal
unless $child->nodeType == 1 0 0 0
207 0 $field{$child->localname} = $child->textContent;
208 }
209  
210 0 my $cusip = $field{Cusip};
211 0 ▶ next unless defined $cusip and length $cusip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cusip and length $cusip 0 0 0
Condition0%0/3
defined $cusip and length $cusip
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cusip and length $cusip
defined $cusip length $cusip
Truth table0%0/3
A and B
A= defined $cusipB= length $cusip
ABresult
0X0
100
111
212  
213 # BUY is what an investor pays, so it is the ask; SELL is what they
214 # receive, so it is the bid. BuyPrice is never below SellPrice, and the
215 # reverse mapping gives a negative spread.
216 $bonds{uc $cusip} = {
217 rate => $field{Rate},
218 maturity => $field{MaturityDate},
219 ask => $field{BuyPrice},
220 bid => $field{SellPrice},
221 eod => $field{EODPrice},
222 0 };
223 }
224  
225 0 return \%bonds;
226 }
227  
228 # The price for one security, or undef if the file quoted nothing usable.
229 0 ▶sub price_from {
Subroutine price_from: not called CC=8 SCAR=42.8
Pod undocumented
230 0 my $bond = shift;
231  
232 # Schema-required but string-typed, so present without being numeric -
233 # CallDate is empty in every record.
234 0 ▶ my %num = map { $_ => looks_like_number($bond->{$_}) ? $bond->{$_} + 0 : 0 }
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($bond->{$_}) ? : 0 0 0
235 qw/bid ask eod/;
236  
237 # A security may be quoted on one side only, and averaging the quoted side
238 # against a zero halves it. EODPrice is zero until after the close, so it
239 # cannot carry the fallback alone.
240 my $price = ($num{bid} > 0 and $num{ask} > 0) ? 0.5*($num{bid} + $num{ask})
241 : $num{eod} > 0 ? $num{eod}
242 : $num{bid} > 0 ? $num{bid}
243 : $num{ask} > 0 ? $num{ask}
244 0 ▶ : undef;
Branch: 4 branches
BranchTrueFalseTotal
$num{'bid'} > 0 && $num{'ask'} > 0 ? : 0 0 0
$num{'eod'} > 0 ? : 0 0 0
$num{'bid'} > 0 ? : 0 0 0
$num{'ask'} > 0 ? : 0 0 0
Condition0%0/3
$num{"bid"} > 0 and $num{"ask"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$num{"bid"} > 0 and $num{"ask"} > 0
$num{"bid"} > 0 $num{"ask"} > 0
Truth table0%0/3
A and B
A= $num{"bid"} > 0B= $num{"ask"} > 0
ABresult
0X0
100
111
245  
246 0 ▶ return defined $price ? sprintf("%.6f", $price) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $price ? : 0 0 0
247 }
248  
249 5 1;
250  
251 __END__
« Quote/AlphaVantage.pm ↑ Summary Quote/IndiaMutual.pm »