Quote/USBonds.pm

filter: Statementstmt 19.3% 39 / 202 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 84 MC/DCmcdc 0.0% 0 / 51 Subroutinesub 52.6% 10 / 19 Podpod 0.0% 0 / 10 totaltotal 10.8% 49 / 452 CC 73 SCAR 58.6
CC
73
Cov
10%
isDateValid
650.0
parseSymbol
552.0
getQuoteDates
182.0
CRAP 350.1
« Quote/Sinvestor.pm ↑ Summary Quote/Union.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 expandtab noai ic showmode showmatch:
3 #
4 # USBonds.pm
5 # A GNUCash quote source that uses the treasurydirect.gov website to obtain
6 # current prices for U.S. Treasury bonds, such as E, EE, I, etc.
7 # The user provides the necessary specifications for their bonds and this
8 # routine queries the website for the prices.
9 #
10 # Kenneth J. Farley
11 # 2018-02-03
12 #
13 # This program is free software; you can redistribute it and/or modify it
14 # under the terms of the GNU General Public License as published by the
15 # Free Software Foundation; either version 2 of the License, or (at your
16 # option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful, but WITHOUT
19 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # this program; if not, write the Free Software Foundation, Inc., 59 Temple
24 # Place - Suite 330, Boston, MA 02111-1307, USA.
25 #
26  
27 package Finance::Quote::USBonds ;
28 5 require 5.010 ;
29  
30 5 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 5 ▶use LWP::UserAgent ;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use HTTP::Request::Common ;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use HTML::TreeBuilder ;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
38 5 ▶use if DEBUG, 'Smart::Comments', '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 5 our $VERSION = '1.72_01'; # TRIAL VERSION
41  
42 #
43 # Define constants.
44 #
45  
46 5 our $DISPLAY = 'USBonds - US Treasury Bonds';
47 5 our @LABELS = qw/exchange method source symbol currency date isodate version price/;
48 5 our $METHODHASH = {subroutine => \&usbonds,
49 display => $DISPLAY,
50 labels => \@LABELS};
51  
52 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
53 return (
54 10 usbonds => $METHODHASH,
55 );
56 }
57  
58 # --- [ labels ] --------------------------------------------------------------
59 #
60 # This method also seems to be a requirement of a Finance::Quote module, but
61 # there doesn't seem to be any documentation of what it is supposed to include,
62 # or why.
63 #
64  
65 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
66 5 my %m = methodinfo();
67 5 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
68 }
69  
70 # --- [ methods ] -------------------------------------------------------------
71 #
72 # A Finance::Quote module must have a methods() subroutine, which is called by
73 # when it loads the module. It's used to determine which methods the module
74 # provides. Thus, though there are a number of subroutines defined, only those
75 # we want to make 'public' are included here.
76 #
77  
78 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
79 5 my %m = methodinfo();
80 5 return map {$_ => $m{$_}{subroutine} } keys %m;
81 }
82  
83 5 my $urlBonds = "https://www.treasurydirect.gov/BC/SBCPrice" ;
84  
85 #
86 # Package variables, necessary to allow the different subroutines to access the
87 # same data.
88 #
89  
90 5 my %quotes ;
91 5 my ( @listDenomination, @listSeries, @listIssueDate, @listDateNums ) ;
92 5 my ( @listQuoteDate, @listPrices, @validSymbols ) ;
93  
94 # --- [ getTodaysDate ] -------------------------------------------------------
95 #
96 # Returns a string that contains today's date in the format 'YYYY-MM-DD'. It uses
97 # the 'localtime' function, adjusting and formatting the data appropriately.
98 #
99  
100 sub getTodaysDate
101 0 ▶{
Subroutine getTodaysDate: not called CC=3 SCAR=24.8
Pod undocumented
102 0 my $useIsoFormat = $_[0] ;
103 0 ▶ if ( defined $useIsoFormat )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $useIsoFormat) { } 0 0 0
104 {
105 0 $useIsoFormat = ( $useIsoFormat > 0 ) ;
106 }
107 else
108 {
109 0 $useIsoFormat = ( 0 == 1 ) ;
110 }
111 0 my ( $s, $m, $h, $dayToday, $monthToday, $yearToday, $w, $y, $i ) = localtime ;
112 0 $monthToday += 1 ;
113 0 $yearToday += 1900 ;
114 0 ▶ if ( $useIsoFormat )
Branch: 1 branch
BranchTrueFalseTotal
if ($useIsoFormat) { } 0 0 0
115 {
116 0 $yearToday . '-' . $monthToday . '-' . $dayToday ;
117 }
118 else
119 {
120 0 $monthToday . '/' . $dayToday . '/' . $yearToday ;
121 }
122 }
123  
124 # --- [ isDateValid ] ---------------------------------------------------------
125 #
126 # Savings bonds, like any government organization, have a confusing melange of
127 # valid issue dates, depending upon the series and denomination. This routine
128 # will determine, given a set of parameters, whether the bond described is
129 # valid.
130 #
131 # The algorithm, if it can be dignified with that title, calculates an integer
132 # date value which is 12 times the year added to the month. The number is used
133 # to compare the date to the acceptable range of dates for the given bond
134 # series.
135 #
136 # Usage: isDateValid ( denom, series, month, year )
137 #
138 # Arguments
139 # denom : the monetary denomination, in USD.
140 # series : the series of the bond.
141 # month : an integer from 1 (January) to 12 (December).
142 # year : a four digit integer representing the year.
143 #
144 # Returns a boolean value indicating if the given combination of parameters
145 # describes a valid bond.
146 #
147  
148 sub isDateValid
149 0 ▶{
Subroutine isDateValid: not called CC=25 SCAR=64.8
Pod undocumented
150 0 my $denom = $_[0] ;
151 0 my $series = $_[1] ;
152 0 my $datenum = $_[2] + 12 * $_[3] ;
153 0 my $result = ( 0 == 1 ) ;
154  
155 0 ▶ if ( $series eq 'E' )
Branch: 1 branch
BranchTrueFalseTotal
if ($series eq "E") 0 0 0
156 {
157 0 ▶ $result = ( $datenum > 4 + 1941 * 12 && $datenum < 7 + 1980 * 12 ) ;
Condition0%0/3
$datenum > 23296 && $datenum < 23767
!ll&&!rl&&r
000
MC/DC0%0/2
$datenum > 23296 && $datenum < 23767
$datenum > 23296 $datenum < 23767
Truth table0%0/3
A && B
A= $datenum > 23296B= $datenum < 23767
ABresult
0X0
100
111
158 0 ▶ if ( $result && $denom == 10 )
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $denom == 10) 0 0 0
Condition0%0/3
$result and $denom == 10
!ll&&!rl&&r
000
MC/DC0%0/2
$result and $denom == 10
$result $denom == 10
Truth table0%0/3
A and B
A= $resultB= $denom == 10
ABresult
0X0
100
111
159 {
160 0 ▶ $result = ( $datenum > 5 + 1944 * 12 && $datenum < 4 + 1950 * 12 ) ;
Condition0%0/3
$datenum > 23333 && $datenum < 23404
!ll&&!rl&&r
000
MC/DC0%0/2
$datenum > 23333 && $datenum < 23404
$datenum > 23333 $datenum < 23404
Truth table0%0/3
A && B
A= $datenum > 23333B= $datenum < 23404
ABresult
0X0
100
111
161 }
162 0 ▶ if ( $result && $denom == 75 )
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $denom == 75) 0 0 0
Condition0%0/3
$result and $denom == 75
!ll&&!rl&&r
000
MC/DC0%0/2
$result and $denom == 75
$result $denom == 75
Truth table0%0/3
A and B
A= $resultB= $denom == 75
ABresult
0X0
100
111
163 0 ▶ { $result = ( $result && $datenum > 4 + 1964 * 12 ) ; }
Condition0%0/3
$result && $datenum > 23572
!ll&&!rl&&r
000
MC/DC0%0/2
$result && $datenum > 23572
$result $datenum > 23572
Truth table0%0/3
A && B
A= $resultB= $datenum > 23572
ABresult
0X0
100
111
164 0 ▶ if ( $result && $denom == 200 )
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $denom == 200) 0 0 0
Condition0%0/3
$result and $denom == 200
!ll&&!rl&&r
000
MC/DC0%0/2
$result and $denom == 200
$result $denom == 200
Truth table0%0/3
A and B
A= $resultB= $denom == 200
ABresult
0X0
100
111
165 0 ▶ { $result = ( $result && $datenum > 9 + 1945 * 12 ) ; }
Condition0%0/3
$result && $datenum > 23349
!ll&&!rl&&r
000
MC/DC0%0/2
$result && $datenum > 23349
$result $datenum > 23349
Truth table0%0/3
A && B
A= $resultB= $datenum > 23349
ABresult
0X0
100
111
166 0 ▶ if ( $result && $denom == 10000 )
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $denom == 10000) 0 0 0
Condition0%0/3
$result and $denom == 10000
!ll&&!rl&&r
000
MC/DC0%0/2
$result and $denom == 10000
$result $denom == 10000
Truth table0%0/3
A and B
A= $resultB= $denom == 10000
ABresult
0X0
100
111
167 0 ▶ { $result = ( $result && $datenum > 4 + 1952 * 12 ) ; }
Condition0%0/3
$result && $datenum > 23428
!ll&&!rl&&r
000
MC/DC0%0/2
$result && $datenum > 23428
$result $datenum > 23428
Truth table0%0/3
A && B
A= $resultB= $datenum > 23428
ABresult
0X0
100
111
168 }
169  
170 #
171 # Series I
172 # Issue Dates 1998-09 to 2009-08
173 # Denominations : 200 and 10000
174 #
175 0 ▶ if ( $series eq 'I' )
Branch: 1 branch
BranchTrueFalseTotal
if ($series eq "I") 0 0 0
176 {
177 0 ▶ $result = ( $datenum > 8 + 1998 * 12 && $datenum < 9 + 2009 * 12 ) ;
Condition0%0/3
$datenum > 23984 && $datenum < 24117
!ll&&!rl&&r
000
MC/DC0%0/2
$datenum > 23984 && $datenum < 24117
$datenum > 23984 $datenum < 24117
Truth table0%0/3
A && B
A= $datenum > 23984B= $datenum < 24117
ABresult
0X0
100
111
178 0 ▶ if ( $result && $denom == 200 )
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $denom == 200) 0 0 0
Condition0%0/3
$result and $denom == 200
!ll&&!rl&&r
000
MC/DC0%0/2
$result and $denom == 200
$result $denom == 200
Truth table0%0/3
A and B
A= $resultB= $denom == 200
ABresult
0X0
100
111
179 0 ▶ { $result = ( $result && $datenum > 4 + 1999 * 12 ) ; }
Condition0%0/3
$result && $datenum > 23992
!ll&&!rl&&r
000
MC/DC0%0/2
$result && $datenum > 23992
$result $datenum > 23992
Truth table0%0/3
A && B
A= $resultB= $datenum > 23992
ABresult
0X0
100
111
180 0 ▶ if ( $result && $denom == 10000 )
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $denom == 10000) 0 0 0
Condition0%0/3
$result and $denom == 10000
!ll&&!rl&&r
000
MC/DC0%0/2
$result and $denom == 10000
$result $denom == 10000
Truth table0%0/3
A and B
A= $resultB= $denom == 10000
ABresult
0X0
100
111
181 {
182 0 ▶ $result = ( $datenum > 4 + 1999 * 12 && $datenum < 12 + 2007 * 12 ) ;
Condition0%0/3
$datenum > 23992 && $datenum < 24096
!ll&&!rl&&r
000
MC/DC0%0/2
$datenum > 23992 && $datenum < 24096
$datenum > 23992 $datenum < 24096
Truth table0%0/3
A && B
A= $datenum > 23992B= $datenum < 24096
ABresult
0X0
100
111
183 }
184 }
185  
186 #
187 # Series EE
188 # Issue Dates 1980-01 to present
189 # Denominations : 50, 75, 100, 200, 500, 1000, 2000, 5000, and 10000
190 #
191 0 ▶ if ( $series eq 'EE' )
Branch: 1 branch
BranchTrueFalseTotal
if ($series eq "EE") 0 0 0
192 {
193 0 $result = ( $denom =~ /10{2,4}|50{1,3}|75|20{2,3}/ ) ;
194 0 ▶ $result = ( $result && $datenum > 12 + 1979 * 12 ) ;
Condition0%0/3
$result && $datenum > 23760
!ll&&!rl&&r
000
MC/DC0%0/2
$result && $datenum > 23760
$result $datenum > 23760
Truth table0%0/3
A && B
A= $resultB= $datenum > 23760
ABresult
0X0
100
111
195 }
196 0 $result ;
197 }
198  
199 # --- [ buildForm ] -----------------------------------------------------------
200 #
201 # Builds a form that is to be 'posted' to the website that provides pricing
202 # data for bonds. The form contains the minimum data necessary to get data
203 # from the site. Note that some of the data series stored in the form are just
204 # blank placeholders.
205 #
206 # Arguments
207 # listDen : a reference to an array of the monetary denomination, in USD.
208 # listSer : a reference to an array the series of the bond.
209 # listDat : a reference to an array of issue dates
210 #
211 # Returns a form populated with the data provided in the lists.
212 #
213  
214 sub buildForm
215 0 ▶{
Subroutine buildForm: not called CC=1 SCAR=6.9
Pod undocumented
216 0 my ( $yr, $mo ) = ( &getTodaysDate ( 1 ) =~ m/([0-9]+)\-([0-9]+)\-[0-9]+/ ) ;
217 0 my @listDen = @{ $_[0] } ;
218 0 my @listSer = @{ $_[1] } ;
219 0 my @listDat = @{ $_[2] } ;
220 0 my %result =
221 (
222 SerialNumList => ' ;' x ( scalar @listDen ),
223 RedemptionDate => $mo . "/" . $yr,
224 IssueDateList => join ( ';', @listDat ) . ';',
225 SeriesList => join ( ';', @listSer ) . ';',
226 DenominationList => join ( ';', @listDen ) . ';',
227 IssuePriceList => ' ;' x ( scalar @listDen ),
228 InterestList => ' ;' x ( scalar @listDen ),
229 YTDInterestList => ' ;' x ( scalar @listDen ),
230 ValueList => ' ;' x ( scalar @listDen ),
231 InterestRateList => ' ;' x ( scalar @listDen ),
232 NextAccrualDateList => ' ;' x ( scalar @listDen ),
233 MaturityDateList => ' ;' x ( scalar @listDen ),
234 NoteList => ' ;' x ( scalar @listDen ),
235 OldRedemptionDate => ' ;' x ( scalar @listDen ),
236 ViewPos => '1',
237 ViewType => 'Partial',
238 Version => '6',
239 'btnUpdate.x' => 'UPDATE',
240 ) ;
241 }
242  
243 # --- [ parseSymbol ] ---------------------------------------------------------
244 #
245 # Parses a provided symbol, determines if it is valid, and if so, adds its data
246 # to the global arrays of information and the global validSymbols list.
247 #
248 # The valid symbol pattern is
249 #
250 # SS-DDD-YYYY-MM
251 #
252 # Where
253 # SS = series I, E, or EE
254 # DDD = denomination 50, 75, 100, 200, 500, 1000 5000 or 10000
255 # YYYY = year of issuance
256 # MM = month of issuance
257 #
258 # Symbols parsed are checked for validity according to the above rules, and
259 # return a hopefully helpful indication of what is wrong, whether it be the
260 # overall format, or the individual tokens of the symbol.
261 #
262  
263 sub parseSymbol
264 0 ▶{
Subroutine parseSymbol: not called CC=23 SCAR=63.1
Pod undocumented
265 0 my $symbol = uc $_[0] ;
266 0 my $regexpSymbol = "^([A-Za-z]{1,2})-(\\d{2,5})-(\\d{4})-(\\d{2})" ;
267 0 my ( $ser, $den, $yr, $mo ) = ( $symbol =~ m/$regexpSymbol/ ) ;
268 0 ▶ my $okFormat = ( $den && $ser && $yr && $mo ) ;
Condition0%0/9
$den && $ser && $yr && $mo
!ll&&!rl&&r
000
$den and $ser and $yr
!ll&&!rl&&r
000
$den and $ser
!ll&&!rl&&r
000
MC/DC0%0/4
$den && $ser && $yr && $mo
$den $ser $yr $mo
Truth table0%0/5
A and B and C && D
A= $denB= $serC= $yrD= $mo
ABCDresult
0XXX0
10XX0
110X0
11100
11111
269 0 my $errorMsg = "" ;
270 0 my $okTokens = ( 1 == 1 ) ;
271 0 ▶ if ( $okFormat )
Branch: 1 branch
BranchTrueFalseTotal
if ($okFormat) { } 0 0 0
272 {
273 0 my $okDenom = ( $den =~ /10{1,4}|50{1,3}|25|75|200/ ) ;
274 0 my $okSeries = ( $ser =~ /E{1,2}|I/ ) ;
275 0 my $okYear = ( $yr =~ /194[1-9]|19[5-9]\d|20\d{2}/ ) ;
276 0 my $okMonth = ( $mo =~ /0[1-9]|1[0-2]/ ) ;
277 0 ▶ if ( $okSeries && $okDenom )
Branch: 1 branch
BranchTrueFalseTotal
if ($okSeries and $okDenom) 0 0 0
Condition0%0/3
$okSeries and $okDenom
!ll&&!rl&&r
000
MC/DC0%0/2
$okSeries and $okDenom
$okSeries $okDenom
Truth table0%0/3
A and B
A= $okSeriesB= $okDenom
ABresult
0X0
100
111
278 {
279 0 ▶ if ( $ser eq 'I' || $ser eq 'EE' )
Branch: 1 branch
BranchTrueFalseTotal
if ($ser eq "I" or $ser eq "EE") 0 0 0
Condition0%0/3
$ser eq "I" or $ser eq "EE"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ser eq "I" or $ser eq "EE"
$ser eq "I" $ser eq "EE"
Truth table0%0/3
A or B
A= $ser eq "I"B= $ser eq "EE"
ABresult
1X1
011
000
280 0 { $okDenom = ( $den > 26 ) ; }
281 0 ▶ if ( $ser eq 'E' )
Branch: 1 branch
BranchTrueFalseTotal
if ($ser eq "E") 0 0 0
282 0 { $okDenom = ( $den != 5000 ) ; }
283 }
284 0 my $okDate = &isDateValid ( $den, $ser, $mo, $yr ) ;
285 0 ▶ my $okTokens = ( $okDenom && $okSeries && $okYear && $okMonth && $okDate) ;
Condition0%0/12
$okDenom && $okSeries && $okYear && $okMonth && $okDate
!ll&&!rl&&r
000
$okDenom and $okSeries and $okYear and $okMonth
!ll&&!rl&&r
000
$okDenom and $okSeries and $okYear
!ll&&!rl&&r
000
$okDenom and $okSeries
!ll&&!rl&&r
000
MC/DC0%0/5
$okDenom && $okSeries && $okYear && $okMonth && $okDate
$okDenom $okSeries $okYear $okMonth $okDate
Truth table0%0/6
A and B and C and D && E
A= $okDenomB= $okSeriesC= $okYearD= $okMonthE= $okDate
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
286 0 ▶ if ( $okTokens )
Branch: 1 branch
BranchTrueFalseTotal
if ($okTokens) { } 0 0 0
287 {
288 0 push @listDenomination, $den ;
289 0 push @listSeries, $ser ;
290 0 push @listIssueDate, $mo . "/" . $yr ;
291 0 push @listDateNums, ( $yr * 12 + $mo - 23296 ) ;
292 0 push @validSymbols, $symbol ;
293 }
294 else
295 {
296 0 $errorMsg = "Symbol \"" . $symbol . "\" Bad tokens:\n" ;
297 0 ▶ if ( ! $okDenom )
Branch: 1 branch
BranchTrueFalseTotal
unless ($okDenom) 0 0 0
298 0 { $errorMsg .= " denomination \"" . $den . "\"" ; }
299 0 ▶ if ( ! $okSeries )
Branch: 1 branch
BranchTrueFalseTotal
unless ($okSeries) 0 0 0
300 0 { $errorMsg .= " series \"" . $ser . "\"" ; }
301 0 ▶ if ( ! $okYear )
Branch: 1 branch
BranchTrueFalseTotal
unless ($okYear) 0 0 0
302 0 { $errorMsg .= " year \"" . $yr . "\"" ; }
303 0 ▶ if ( ! $okMonth )
Branch: 1 branch
BranchTrueFalseTotal
unless ($okMonth) 0 0 0
304 0 { $errorMsg .= " month \"" . $mo . "\"" ; }
305 0 ▶ if ( ! $okDate )
Branch: 1 branch
BranchTrueFalseTotal
unless ($okDate) 0 0 0
306 0 { $errorMsg .= " date \"" . $yr . "-" . $mo . "\"" ; }
307 }
308 }
309 else
310 {
311 0 $errorMsg = "Symbol \"" . $symbol . "\" incorrect format" ;
312 }
313 0 ▶ $quotes { $symbol, "success" } = $okFormat && $okTokens ;
Condition0%0/3
$okFormat && $okTokens
!ll&&!rl&&r
000
MC/DC0%0/2
$okFormat && $okTokens
$okFormat $okTokens
Truth table0%0/3
A && B
A= $okFormatB= $okTokens
ABresult
0X0
100
111
314 0 ▶ unless ( $okFormat && $okTokens )
Branch: 1 branch
BranchTrueFalseTotal
unless ($okFormat and $okTokens) 0 0 0
Condition0%0/3
$okFormat and $okTokens
!ll&&!rl&&r
000
MC/DC0%0/2
$okFormat and $okTokens
$okFormat $okTokens
Truth table0%0/3
A and B
A= $okFormatB= $okTokens
ABresult
0X0
100
111
315 {
316 0 $quotes { $symbol, "errormsg" } = $errorMsg ;
317 0 $quotes { $symbol, "success" } = 0;
318 }
319 }
320  
321 # --- [ getQuoteDates ] ----------------------------------------------------
322 #
323 # Interest is accrued for some bonds every 6 months, and for others monthly.
324 # The treasurydirect bond pricing calculator provides a 'next accrual date'
325 # field for each bond.
326 # This routine traverses the list of valid bonds and finds the latest accrual
327 # date that has passed. By doing so, when the pricing information is provided
328 # to GnuCash, it will not generate duplicate quotes, even if this module is
329 # used multiple times in the same month.
330 #
331 # The algorithm used is as follows:
332 # (1) Get the date today from the 'getTodaysDate' function.
333 # (2) Starting last month, get the next accrual date for all the bonds.
334 # (3) For any accrual dates that are less than or equal to this month, record
335 # them in the appropriate list.
336 # (4) Go to the next month back, and get the next accrual date for any bonds
337 # that have not got any yet.
338 # (5) Continue to the next month back for any bonds that aren't matched, etc.
339 #
340 # All the quote dates are saved as the first day of the month, or '01/MM/YYYY'
341 # and an isodate of 'YYYY-MM-01'.
342 #
343  
344 sub getQuoteDates
345 0 ▶{
Subroutine getQuoteDates: not called CC=13 SCAR=52.0
Pod undocumented
346 0 my ( $yr, $mo ) = ( &getTodaysDate ( 1 ) =~ m/([0-9]+)\-([0-9]+)\-[0-9]+/ ) ;
347 0 my $dateNumToday = $yr * 12 + $mo - 23296 ;
348 0 @listQuoteDate = split ';', ( '-;' x ( scalar @validSymbols ) ) ;
349 0 my $userAgent = LWP::UserAgent->new ;
350  
351 0 my $moNext = $mo ;
352 0 my $yrNext = $yr ;
353 0 ▶ for ( my $deltaMonth = 1 ; $deltaMonth <= 6 ; $deltaMonth++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($deltaMonth <= 6) 0 0 0
354 {
355 0 $moNext-- ;
356 0 ▶ if ( $moNext < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($moNext < 1) 0 0 0
357 {
358 0 $moNext += 12 ;
359 0 $yrNext -= 1 ;
360 }
361 0 my $countItems = 0 ;
362 0 my ( @listDat, @listDen, @listSer ) ;
363 0 ▶ for ( my $index = 0 ; $index < ( scalar @validSymbols ) ; $index++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($index < scalar @validSymbols) 0 0 0
364 {
365 0 ▶ if ( $listQuoteDate [ $index ] eq "-" )
Branch: 1 branch
BranchTrueFalseTotal
if ($listQuoteDate[$index] eq "-") 0 0 0
366 {
367 0 push @listDat, $listDateNums [ $index ] ;
368 0 push @listDen, $listDenomination [ $index ] ;
369 0 push @listSer, $listSeries [ $index ] ;
370 0 $countItems++ ;
371 }
372 }
373 0 ▶ if ( $countItems > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($countItems > 0) 0 0 0
374 {
375 0 my %formQuery = &buildForm ( \@listDen, \@listSer, \@listDat ) ;
376 0 $formQuery{ 'RedemptionDate' } = $moNext . '/' . $yrNext ;
377 0 my $resData = $userAgent->post ( $urlBonds, \%formQuery ) ;
378 0 ▶ if ( $resData->is_success )
Branch: 1 branch
BranchTrueFalseTotal
if ($resData->is_success) 0 0 0
379 {
380 0 my $treeQuery = HTML::TreeBuilder->new_from_content ( $resData->content ) ;
381 0 $treeQuery->elementify () ;
382 my $valueQuery =
383 (
384 $treeQuery->look_down
385 (
386 sub
387 0 ▶ {
Subroutine __ANON__: not called CC=3 SCAR=24.8
388 0 ▶ $_[0]->tag() eq 'input'
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->tag eq "input" and $_[0]->attr("type") eq "hidden" and $_[0]->attr("name") eq "NextAccrualDateList" 0 0 0
Condition0%0/3
$_[0]->tag eq "input" and $_[0]->attr("type") eq "hidden"
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]->tag eq "input" and $_[0]->attr("type") eq "hidden"
$_[0]->tag eq "input" $_[0]->attr("type") eq "hidden"
Truth table0%0/3
A and B
A= $_[0]->tag eq "input"B= $_[0]->attr("type") eq "hidden"
ABresult
0X0
100
111
389 and $_[0]->attr ( 'type' ) eq 'hidden'
390 and $_[0]->attr ( 'name' ) eq 'NextAccrualDateList'
391 }
392 )
393 0 ) ;
394 0 ▶ if ( defined ( $valueQuery ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valueQuery) 0 0 0
395 {
396 0 my @listNext = split ";", $valueQuery->{value} ;
397 0 my $indexRes = 0 ;
398 0 ▶ for ( my $indexDate = 0 ; $indexDate < ( scalar @validSymbols ) ; $indexDate++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($indexDate < scalar @validSymbols) 0 0 0
399 {
400 0 ▶ if ( $listQuoteDate [ $indexDate ] eq "-" )
Branch: 1 branch
BranchTrueFalseTotal
if ($listQuoteDate[$indexDate] eq "-") 0 0 0
401 {
402 0 my $dateNum = $listNext [ $indexRes ] ;
403 0 ▶ if ( $dateNum <= $dateNumToday )
Branch: 1 branch
BranchTrueFalseTotal
if ($dateNum <= $dateNumToday) 0 0 0
404 {
405 0 my $yr = int ( ( $listNext [ $indexRes ] + 23296 ) / 12 ) ;
406 0 my $mo = ( $listNext [ $indexRes ] + 23296 ) % 12 ;
407 0 ▶ if ( $mo < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($mo < 1) 0 0 0
408 {
409 0 $mo += 12 ;
410 0 $yr -= 1 ;
411 }
412 0 ▶ if ( $mo < 10 ) { $mo = '0' . $mo ; }
Branch: 1 branch
BranchTrueFalseTotal
if ($mo < 10) 0 0 0
413 0 $listQuoteDate [ $indexDate ] = $mo . '/' . $yr ;
414 }
415 0 $indexRes++ ;
416 }
417 }
418 }
419 }
420 }
421 }
422 } # getQuoteDates
423  
424 # --- [ getQuotes ] -----------------------------------------------------------
425 #
426 # Uses the lists that have been built pertaining to the bonds, to get the
427 # current prices by posting a form with the data to the treasurydirect website.
428 # The prices are extracted from the resulting data using HTML::TreeBuilder.
429 #
430  
431 sub getQuotes
432 0 ▶{
Subroutine getQuotes: not called CC=3 SCAR=24.8
Pod undocumented
433 0 my %formBonds = &buildForm ( \@listDenomination, \@listSeries, \@listIssueDate ) ;
434 0 my $userAgent = LWP::UserAgent->new ;
435 0 my $resData = $userAgent->post ( $urlBonds, \%formBonds ) ;
436 0 ▶ if ( $resData->is_success )
Branch: 1 branch
BranchTrueFalseTotal
if ($resData->is_success) 0 0 0
437 {
438 0 my $treeBonds = HTML::TreeBuilder->new_from_content ( $resData->content ) ;
439 0 $treeBonds->elementify () ;
440 my $valueBonds =
441 ( $treeBonds->look_down
442 ( sub
443 0 ▶ {
Subroutine __ANON__: not called CC=3 SCAR=24.8
444 0 ▶ $_[0]->tag() eq 'input' and
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->tag eq "input" and $_[0]->attr("type") eq "hidden" and $_[0]->attr("name") eq "ValueList" 0 0 0
Condition0%0/3
$_[0]->tag eq "input" and $_[0]->attr("type") eq "hidden"
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]->tag eq "input" and $_[0]->attr("type") eq "hidden"
$_[0]->tag eq "input" $_[0]->attr("type") eq "hidden"
Truth table0%0/3
A and B
A= $_[0]->tag eq "input"B= $_[0]->attr("type") eq "hidden"
ABresult
0X0
100
111
445 $_[0]->attr ( 'type' ) eq 'hidden' and
446 $_[0]->attr ( 'name' ) eq 'ValueList'
447 }
448 )
449 0 ) ;
450 0 ▶ if ( defined ( $valueBonds ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valueBonds) 0 0 0
451 {
452 0 $valueBonds->{value} =~ s/;$// ;
453 0 @listPrices = split ';', $valueBonds->{value} ;
454 }
455 }
456 } # end getQuotes
457  
458 # --- [ usbonds ] -------------------------------------------------------------
459 #
460 # The main routine, which is the option that will be selected within GnuCash to
461 # get prices for bonds.
462 #
463 # The algorithm used is as follows:
464 #
465 # (1) Parse the symbol to determine the series, face value, issue date, etc.
466 # (2) For the valid symbols that result from the parsing, get quotes.
467 # (3) Build the hash table by adding all the valid symbols with their pertin-
468 # ent data.
469 # (4) Return the hash table.
470 #
471 # Arguments
472 # quoter : string that identifies the module. This argument is ignored.
473 # symbol1 : 1st symbol
474 # symbol2 : 2nd symbol
475 # .
476 # .
477 # .
478 # symboln : nth symbol
479 #
480  
481 sub usbonds
482 0 ▶{
Subroutine usbonds: not called CC=7 SCAR=40.3
Pod undocumented
483 0 my $quoter = shift ;
484 0 my @symbols = @_ ;
485  
486 0 ▶ return unless @symbols ;
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols 0 0 0
487  
488 0 foreach my $symbol ( @symbols )
489 {
490 0 &parseSymbol ( $symbol ) ;
491 }
492  
493 0 ▶ if ( scalar @validSymbols > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @validSymbols > 0) 0 0 0
494 {
495 0 &getQuotes ;
496 0 &getQuoteDates ;
497  
498 ### listPrices: @listPrices
499  
500 0 ▶ for ( my $index = 0 ; $index < scalar @validSymbols ; $index++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < scalar @validSymbols) 0 0 0
501 0 my $keyNow = $validSymbols [ $index ] ;
502 0 ▶ if ( defined $listPrices [ $index ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $listPrices[$index]) { } 0 0 0
503 0 my ( $mo, $yr ) = ( $listQuoteDate [ $index ] =~ m/([0-9]+)\/([0-9]+)/ ) ;
504 0 $quotes { $keyNow, "exchange" } = "Treasury Direct" ;
505 0 $quotes { $keyNow, "method" } = "usbonds" ;
506 0 $quotes { $keyNow, "price" } = $listPrices [ $index ] ;
507 0 $quotes { $keyNow, "last" } = $listPrices [ $index ] ;
508 0 $quotes { $keyNow, "symbol" } = $validSymbols [ $index ] ;
509 0 $quotes { $keyNow, "currency" } = "USD" ;
510 0 $quotes { $keyNow, "source" } = "USBonds" ;
511 0 $quotes { $keyNow, "date" } = $mo . "/01/" . $yr ;
512 0 $quotes { $keyNow, "isodate" } = $yr . "-" . $mo . "-01" ;
513 0 $quotes { $keyNow, "version" } = '1.10' ;
514 0 $quotes { $keyNow, "success" } = 1 ;
515 } else {
516 0 $quotes { $keyNow, "success" } = 0;
517 0 $quotes { $keyNow, "errormsg" } = "No price data for $keyNow";
518 }
519 }
520 }
521 0 ▶ return wantarray() ? %quotes : \%quotes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
522 }
523  
524 5 1 ;
525  
526 =head1 NAME
527  
528 Finance::Quote::USBonds - Obtain quotes for US Federal Bonds in the E, EE, or I series
529 from the Treasury Direct website, http://www.treasurydirect.gov.
530  
531 =head1 SYNOPSIS
532  
533 use Finance::Quote ;
534  
535 $q = Finance::Quote->new ;
536  
537 %quote = $q->fetch ( "usbonds", "\EE-100-1993-10" ) ;
538  
539 =head1 DESCRIPTION
540  
541 Module obtains quote information from the Treasury Direct website, www.treasurydirect.gov.
542 Given a bond symbol, obtains the current prices for it.
543  
544 The symbol nomenclature is as follows:
545  
546 SS-DDDDD-YYYY-MM
547  
548 Where
549 SS = series (EE, E, or I)
550 DDDDD = denomination
551 YYYY = year issued
552 MM = month issued
553  
554 For example, 'EE-500-1988-02'
555  
556 The symbols supplied are checked for format and validity. For example, bonds
557 were only issued after 1942, only the specific denominations issued for EE
558 bonds are allowed, etc.
559  
560 =head1 LABELS RETURNED
561  
562 The following labels may be returned by Finance::Quote::USBonds:
563 exchange method source symbol currency date isodate version price
564  
565 =head1 SEE ALSO
566  
567 Treasury bond value web interface - http://www.treasurydirect.gov/BC/SBCPrice
568  
569 Finance::Quote
570  
571 =head1 AUTHOR
572  
573 Kenneth J. Farley
574  
575 =cut
« Quote/Sinvestor.pm ↑ Summary Quote/Union.pm »