| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch: | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Copyright (C) 1998, Dj Padzensky <djpadz@padz.net> | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # Copyright (C) 1998, 1999 Linas Vepstas <linas@linas.org> | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # Copyright (C) 2000, Yannick LE NY <y-le-ny@ifrance.com> | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # Copyright (C) 2000, Paul Fenwick <pjf@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # Copyright (C) 2000, Brent Neal <brentn@users.sourceforge.net> | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # Copyright (C) 2001, Rob Sessink <rob_ses@users.sourceforge.net> | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # Copyright (C) 2004, Frank Mori Hess <fmhess@users.sourceforge.net> | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # Trent Piepho <xyzzy@spekeasy.org> | ||||||||||||||||||||||||||||||||||||||||||
| 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 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 5 | ▶ | use if DEBUG, 'Smart::Comments'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | package Finance::Quote::TSP; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | ▶ | use vars qw( $TSP_URL $TSP_MAIN_URL @HEADERS ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | use POSIX; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | our $VERSION = '1.72_02'; # TRIAL VERSION | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | # URLs of where to obtain information | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | $TSP_URL = 'https://www.tsp.gov/data/fund-price-history.csv'; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | $TSP_MAIN_URL = 'http://www.tsp.gov'; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | @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 | 5 | our $DISPLAY = 'TSP - US Gov Thrift Savings Plan'; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | our @LABELS = qw/name date isodate currency close/; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 5 | our $METHODHASH = {subroutine => \&tsp, | |||||||||||||||||||||||||||||||||||||||||
| 54 | display => $DISPLAY, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | labels => \@LABELS}; | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 10 | ▶ | sub methodinfo { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 10 | tsp => $METHODHASH, | |||||||||||||||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | ▶ | sub labels { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | my %m = methodinfo(); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | sub methods { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | my %m = methodinfo(); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub format_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_name: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $name =~ s/ //g; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $name = lc($name); | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | return $1 if $name =~ /^(.)fund$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | return $name; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # ============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub tsp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tsp: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $quoter = shift; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my @symbols = @_; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | return unless @symbols; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my %info; | |||||||||||||||||||||||||||||||||||||||||
| 90 | my @line; | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # Ask for the last 7 days | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my $startdate = strftime("%Y-%m-%d", localtime (time - 7*24*3600)); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my $enddate = strftime("%Y-%m-%d", localtime time); | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $ua = $quoter->user_agent; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my $url = "$TSP_URL?startdate=$startdate&enddate=$enddate&Lfunds=1&InvFunds=1&download=1"; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $reply = $ua->get($url, @HEADERS); | |||||||||||||||||||||||||||||||||||||||||
| 99 | ### [<now>] url : $url | ||||||||||||||||||||||||||||||||||||||||||
| 100 | ### [<now>] reply: $reply | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | unless (($reply->is_success) && (@line = split(/\n/, $reply->content)) && (@line > 1)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$reply->is_success and @line = split(/\n/, $reply->content, 0) and @line > 1
$reply->is_success and @line = split(/\n/, $reply->content, 0)
MC/DC0%0/3
$reply->is_success and @line = split(/\n/, $reply->content, 0) and @line > 1
$reply->is_success
@line = split(/\n/, $reply->content, 0)
@line > 1
Truth table0%0/4
A and B and C
A= $reply->is_successB= @line = split(/\n/, $reply->content, 0)C= @line > 1
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | foreach my $symbol (@symbols) { | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $info{$symbol, "success"} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $info{$symbol, "errormsg"} = "TSP fetch failed. No data for $symbol."; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | ### Failure: %info | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | return wantarray ? %info : \%info; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my @header = split(/,/, $line[0]); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my %column = map { format_name($header[$_]) => $_ } 0 .. $#header; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my @latest = split(/,/, $line[1]); | |||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | ### [<now>] header: @header | ||||||||||||||||||||||||||||||||||||||||||
| 116 | ### [<now>] column: %column | ||||||||||||||||||||||||||||||||||||||||||
| 117 | ### [<now>] latest: @latest | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | foreach (@symbols) { | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my $symbol = lc $_; | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if(exists $column{$symbol}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $info{$_, 'success'} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $quoter->store_date(\%info, $_, {isodate => $latest[$column{'date'}]}); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ($info{$_, 'last'} = $latest[$column{$symbol}]) =~ s/[^0-9]*([0-9.,]+).*/$1/s; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $info{$_, 'currency'} = 'USD'; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | $info{$_, 'method'} = 'tsp'; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $info{$_, 'source'} = $TSP_MAIN_URL; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | $info{$_, 'symbol'} = $_; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $info{$_, 'success'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | $info{$_, 'errormsg'} = "Fund not found"; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | return %info if wantarray; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | return \%info; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 5 | 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 | ||||||||||||||||||||||||||||||||||||||||||