| 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 |
use strict; |
| 17 |
use warnings; |
| 18 |
|
| 19 |
|
| 20 |
# |
| 21 |
# Modification of Rolf Endres' Finance::Quote::ZA |
| 22 |
# |
| 23 |
# Peter Ratzlaff <[email protected]> |
| 24 |
# April, 2018 |
| 25 |
# |
| 26 |
|
| 27 |
our $VERSION = '1.71'; # VERSION |
| 28 |
|
| 29 |
use vars qw /$VERSION/ ; |
| 30 |
|
| 31 |
use LWP::UserAgent; |
| 32 |
use HTTP::Request::Common; |
| 33 |
use HTML::TableExtract; |
| 34 |
use HTTP::Request; |
| 35 |
|
| 36 |
my $TREASURY_DIRECT_URL = 'https://www.treasurydirect.gov/GA-FI/FedInvest/todaySecurityPriceDate.htm'; |
| 37 |
|
| 38 |
our $DISPLAY = 'TreasuryDirect - US Treasury Bonds'; |
| 39 |
our @LABELS = qw/method source symbol rate bid ask price date isodate/; |
| 40 |
our $METHODHASH = {subroutine => \&treasurydirect, |
| 41 |
display => $DISPLAY, |
| 42 |
labels => \@LABELS}; |
| 43 |
|
| 44 |
sub methodinfo { |
| 45 |
return ( |
| 46 |
treasurydirect => $METHODHASH, |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
sub labels { |
| 51 |
my %m = methodinfo(); |
| 52 |
return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 53 |
} |
| 54 |
|
| 55 |
sub methods { |
| 56 |
my %m = methodinfo(); |
| 57 |
return map {$_ => $m{$_}{subroutine} } keys %m; |
| 58 |
} |
| 59 |
|
| 60 |
sub treasurydirect { |
| 61 |
|
| 62 |
# check for quotes for today, as well as the last three days |
| 63 |
|
| 64 |
my $time = time(); |
| 65 |
my @times = map { $time-86400*$_ } 0..3; |
| 66 |
|
| 67 |
for my $t (@times) { |
| 68 |
my ($d, $m, $y) = (localtime($t))[3,4,5]; |
| 69 |
$y += 1900; |
| 70 |
$m += 1; |
| 71 |
my @quotes = treasurydirect_ymd($y, $m, $d, @_); |
| 72 |
return @quotes if @quotes; |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
sub treasurydirect_ymd { |
| 78 |
|
| 79 |
my ($y, $m, $d, $quoter, @symbols) = @_; |
| 80 |
|
| 81 |
return unless @symbols; |
| 82 |
|
| 83 |
my %info; |
| 84 |
|
| 85 |
$info{$_, 'success'} = 0 for @symbols; |
| 86 |
|
| 87 |
my $ua = $quoter->user_agent; |
| 88 |
$ua->timeout(10); |
| 89 |
$ua->ssl_opts( verify_hostname => 0 ); |
| 90 |
|
| 91 |
my $content; |
| 92 |
my $url = $TREASURY_DIRECT_URL; |
| 93 |
#print "[debug]: ", $url, "\n"; |
| 94 |
|
| 95 |
if (0) { |
| 96 |
my $response = $ua->request(GET $url); |
| 97 |
#print "[debug]: ", $response->content, "\n"; |
| 98 |
if (!$response->is_success) { |
| 99 |
$info{$_, 'errormsg'} = 'Error contacting URL' for @symbols; |
| 100 |
return wantarray() ? %info : \%info; |
| 101 |
} |
| 102 |
$content = $response->content; |
| 103 |
} |
| 104 |
|
| 105 |
# this is no longer working, for some reason |
| 106 |
elsif (0) { |
| 107 |
my $url = 'https://www.treasurydirect.gov/GA-FI/FedInvest/selectSecurityPriceDate'; |
| 108 |
# my $post_data = [ "priceDate.month" => "4", "priceDate.day" => "13", "priceDate.year" => "2018", "submit" => "Show+Prices" ]; |
| 109 |
my $post_data = [ 'priceDate.month' => $m, |
| 110 |
'priceDate.day' => $d, |
| 111 |
'priceDate.year' => $y, |
| 112 |
'submit' => 'Show Prices', |
| 113 |
]; |
| 114 |
|
| 115 |
my $request = POST( $url, $post_data); |
| 116 |
my $resp = $ua->request($request); |
| 117 |
if ($resp->is_success) { |
| 118 |
$content = $resp->decoded_content; |
| 119 |
# print "[debug]: ", $content, "\n"; |
| 120 |
} else { |
| 121 |
$info{$_, 'errormsg'} = 'Error contacting URL' for @symbols; |
| 122 |
return wantarray() ? %info : \%info; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
else { |
| 127 |
my $url = 'https://www.treasurydirect.gov/GA-FI/FedInvest/selectSecurityPriceDate'; |
| 128 |
#my $data= 'priceDate.month=1&priceDate.day=4&priceDate.year=2021&submit=Show+Prices'; |
| 129 |
|
| 130 |
my $data = |
| 131 |
'priceDate.month=' . $m . |
| 132 |
'&priceDate.day=' . $d . |
| 133 |
'&priceDate.year=' . $y . |
| 134 |
'&submit=Show+Prices'; |
| 135 |
|
| 136 |
$content = `wget --no-check-certificate --post-data='$data' $url -O - 2>/dev/null`; |
| 137 |
} |
| 138 |
|
| 139 |
# submitted a future date |
| 140 |
return if $content =~ /Submitted date must be equal to/; |
| 141 |
|
| 142 |
# weekends, holidays (doesn't work like this any more) |
| 143 |
return if $content =~ /No data for selected date range/; |
| 144 |
|
| 145 |
my ($date, $isodate); |
| 146 |
if ($content =~ /Prices For:\s+(\w+)\s+(\d+),\s+(\d+)/) { |
| 147 |
my @months = qw/ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec /; |
| 148 |
my %months; @months{@months} = 1..12; |
| 149 |
my ($year, $month, $day) = ($3, $months{$1}, $2); |
| 150 |
$date = sprintf "%02d/%02d/%04d", $month, $day, $year; |
| 151 |
$isodate = sprintf "%04d-%02d-%02d", $year, $month, $day; |
| 152 |
} |
| 153 |
|
| 154 |
my $te = new HTML::TableExtract(); |
| 155 |
$te->parse($content); |
| 156 |
# print "[debug]: (parsed HTML)",$te, "\n"; |
| 157 |
|
| 158 |
unless ($te->first_table_found()) { |
| 159 |
#print STDERR "no tables on this page\n"; |
| 160 |
$info{$_, 'errormsg'} = 'Parse error' for @symbols; |
| 161 |
return wantarray() ? %info : \%info; |
| 162 |
} |
| 163 |
|
| 164 |
# Debug to dump all tables in HTML... |
| 165 |
|
| 166 |
=begin comment |
| 167 |
|
| 168 |
print "\n \n \n \n[debug]: ++++ ==== ++++ ==== ++++ ==== ++++ ==== START OF TABLE DUMP ++++ ==== ++++ ==== ++++ ==== ++++ ==== \n \n \n \n"; |
| 169 |
|
| 170 |
for my $ts ($te->table_states) { |
| 171 |
|
| 172 |
printf "\n \n \n \n[debug]: //// \\\\ //// \\\\ //// \\\\ //// \\\\ START OF TABLE %d,%d //// \\\\ //// \\\\ //// \\\\ //// \\\\ \n \n \n \n", |
| 173 |
$ts->depth, $ts->count; |
| 174 |
|
| 175 |
for my $row ($ts->rows) { |
| 176 |
print '[debug]: ', join('|', map { defined $_ ? $_ : 'undef' } @$row), "\n"; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
print "\n \n \n \n[debug]: ++++ ==== ++++ ==== ++++ ==== ++++ ==== END OF TABLE DUMP ++++ ==== ++++ ==== ++++ ==== ++++ ==== \n \n \n \n"; |
| 181 |
|
| 182 |
=end comment |
| 183 |
|
| 184 |
=cut |
| 185 |
|
| 186 |
my %bonds; |
| 187 |
for my $ts ($te->table_states) { |
| 188 |
for my $row ($ts->rows) { |
| 189 |
$bonds{$row->[0]} = { |
| 190 |
rate => $row->[2], |
| 191 |
maturity => $row->[3], |
| 192 |
bid => $row->[5], |
| 193 |
ask => $row->[6], |
| 194 |
}; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
# no bonds were returned, probably due to being a weekend or holiday |
| 199 |
return unless keys(%bonds) > 1; |
| 200 |
|
| 201 |
for my $symbol (@symbols) { |
| 202 |
|
| 203 |
# GENERAL FIELDS |
| 204 |
$info{$symbol, 'method'} = 'treasurydirect'; |
| 205 |
$info{$symbol, 'symbol'} = $symbol; |
| 206 |
$info{$symbol, 'source'} = $TREASURY_DIRECT_URL; |
| 207 |
|
| 208 |
# OTHER INFORMATION |
| 209 |
if (exists $bonds{$symbol}) { |
| 210 |
|
| 211 |
$info{$symbol, 'success'} = 1; |
| 212 |
$info{$symbol, 'currency'} = 'USD'; |
| 213 |
|
| 214 |
$info{$symbol, $_} = $bonds{$symbol}{$_} for keys %{$bonds{$symbol}}; |
| 215 |
|
| 216 |
$info{$symbol, 'price'} = sprintf("%.2f", 0.5*($info{$symbol, 'bid'} + $info{$symbol, 'ask'})); |
| 217 |
|
| 218 |
$info{$symbol, 'date'} = $date if defined $date; |
| 219 |
$info{$symbol, 'isodate'} = $isodate if defined $isodate; |
| 220 |
} |
| 221 |
else { |
| 222 |
$info{$symbol, 'errormsg'} = 'no match'; |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
return wantarray() ? %info : \%info; |
| 228 |
} |
| 229 |
|
| 230 |
1; |
| 231 |
|
| 232 |
__END__ |
| 233 |
|
| 234 |
=head1 NAME |
| 235 |
|
| 236 |
Finance::Quote::TreasuryDirect - Obtain bond quotes from Treasury Direct |
| 237 |
|
| 238 |
=head1 SYNOPSIS |
| 239 |
|
| 240 |
use Finance::Quote; |
| 241 |
|
| 242 |
$q = Finance::Quote->new; |
| 243 |
|
| 244 |
%info = $q->fetch('treasurydirect', '912810QT8'); |
| 245 |
|
| 246 |
=head1 DESCRIPTION |
| 247 |
|
| 248 |
This module obtains individual bond quotes by CUSIP number from |
| 249 |
treasurydirect.gov |
| 250 |
|
| 251 |
=head1 LABELS RETURNED |
| 252 |
|
| 253 |
Information available from Treasury Direct may include the following labels: |
| 254 |
|
| 255 |
method source symbol rate bid ask price date isodate |
| 256 |
|
| 257 |
=head1 SEE ALSO |
| 258 |
|
| 259 |
treasurydirect.gov |
| 260 |
|
| 261 |
Finance::Quote |
| 262 |
|
| 263 |
=cut |