Debian Codegraph @

lib/Finance/Quote/TSP.pm

raw
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch:
3 #
4 # Copyright (C) 1998, Dj Padzensky <[email protected]>
5 # Copyright (C) 1998, 1999 Linas Vepstas <[email protected]>
6 # Copyright (C) 2000, Yannick LE NY <[email protected]>
7 # Copyright (C) 2000, Paul Fenwick <[email protected]>
8 # Copyright (C) 2000, Brent Neal <[email protected]>
9 # Copyright (C) 2001, Rob Sessink <[email protected]>
10 # Copyright (C) 2004, Frank Mori Hess <[email protected]>
11 # Trent Piepho <[email protected]>
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301, USA
27 #
28 #
29 # This code is derived from version 0.9 of the AEX.pm module.
30
31 use strict;
32
33 use constant DEBUG => $ENV{DEBUG};
34 use if DEBUG, 'Smart::Comments';
35
36 package Finance::Quote::TSP;
37
38 use vars qw( $TSP_URL $TSP_MAIN_URL @HEADERS );
39
40 use LWP::UserAgent;
41 use HTTP::Request::Common;
42 use POSIX;
43
44 our $VERSION = '1.71'; # VERSION
45
46 # URLs of where to obtain information
47 $TSP_URL = 'https://www.tsp.gov/data/fund-price-history.csv';
48 $TSP_MAIN_URL = 'http://www.tsp.gov';
49 @HEADERS = ('user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36');
50
51 our $DISPLAY = 'TSP - US Gov Thrift Savings Plan';
52 our @LABELS = qw/name date isodate currency close/;
53 our $METHODHASH = {subroutine => \&tsp,
54 display => $DISPLAY,
55 labels => \@LABELS};
56
57 sub methodinfo {
58 return (
59 tsp => $METHODHASH,
60 );
61 }
62
63 sub labels {
64 my %m = methodinfo();
65 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
66 }
67
68 sub methods {
69 my %m = methodinfo();
70 return map {$_ => $m{$_}{subroutine} } keys %m;
71 }
72
73 sub format_name {
74 my $name = shift;
75 $name =~ s/ //g;
76 $name = lc($name);
77
78 return $1 if $name =~ /^(.)fund$/;
79 return $name;
80 }
81
82 # ==============================================================================
83 sub tsp {
84 my $quoter = shift;
85 my @symbols = @_;
86
87 return unless @symbols;
88
89 my %info;
90 my @line;
91
92 # Ask for the last 7 days
93 my $startdate = strftime("%Y-%m-%d", localtime (time - 7*24*3600));
94 my $enddate = strftime("%Y-%m-%d", localtime time);
95
96 my $ua = $quoter->user_agent;
97 my $url = "$TSP_URL?startdate=$startdate&enddate=$enddate&Lfunds=1&InvFunds=1&download=1";
98 my $reply = $ua->get($url, @HEADERS);
99 ### [<now>] url : $url
100 ### [<now>] reply: $reply
101
102 unless (($reply->is_success) && (@line = split(/\n/, $reply->content)) && (@line > 1)) {
103 foreach my $symbol (@symbols) {
104 $info{$symbol, "success"} = 0;
105 $info{$symbol, "errormsg"} = "TSP fetch failed. No data for $symbol.";
106 }
107 ### Failure: %info
108 return wantarray ? %info : \%info;
109 }
110
111 my @header = split(/,/, $line[0]);
112 my %column = map { format_name($header[$_]) => $_ } 0 .. $#header;
113 my @latest = split(/,/, $line[1]);
114
115 ### [<now>] header: @header
116 ### [<now>] column: %column
117 ### [<now>] latest: @latest
118
119 foreach (@symbols) {
120 my $symbol = lc $_;
121
122 if(exists $column{$symbol}) {
123 $info{$_, 'success'} = 1;
124 $quoter->store_date(\%info, $_, {isodate => $latest[$column{'date'}]});
125 ($info{$_, 'last'} = $latest[$column{$symbol}]) =~ s/[^0-9]*([0-9.,]+).*/$1/s;
126 $info{$_, 'currency'} = 'USD';
127 $info{$_, 'method'} = 'tsp';
128 $info{$_, 'source'} = $TSP_MAIN_URL;
129 $info{$_, 'symbol'} = $_;
130 }
131 else {
132 $info{$_, 'success'} = 0;
133 $info{$_, 'errormsg'} = "Fund not found";
134 }
135 }
136
137 return %info if wantarray;
138 return \%info;
139 }
140 1;
141
142 =head1 NAME
143
144 Finance::Quote::TSP - Obtain fund prices for US Federal Government Thrift Savings Plan
145
146 =head1 SYNOPSIS
147
148 use Finance::Quote;
149
150 $q = Finance::Quote->new;
151
152 %info = $q->fetch('tsp','c'); #get value of C - Common Stock Index Investment Fund
153 %info = $q->fetch('tsp','l2040'); #get value of the L2040 Lifecycle Fund
154 %info = $q->fetch('tsp','lincome'); #get value of the LINCOME Lifecycle Fund
155
156 =head1 DESCRIPTION
157
158 This module fetches fund information from the "Thrift Savings Plan"
159
160 http://www.tsp.gov
161
162 The quote symbols are
163
164 C common stock fund
165 F fixed income fund
166 G government securities fund
167 I international stock fund
168 S small cap stock fund
169 LX lifecycle fund X (eg 2050 or INCOME)
170
171 =head1 LABELS RETURNED
172
173 The following labels are returned by Finance::Quote::TSP :
174
175 date latest date, eg. "21/02/10"
176 isodate latest date, eg. "2010-02-21"
177 last latest available price, eg. "16.1053"
178 currency "USD"
179 method "tsp"
180 source TSP URL
181
182 =head1 SEE ALSO
183
184 Thrift Savings Plan, http://www.tsp.gov
185
186 =cut