| 1 | # Copyright 2008, 2009, 2010, 2011, 2015, 2016 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Suffix::TSP; | ||
| 18 | use 5.006; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | no warnings 'once'; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use Locale::TextDomain 'App-Chart'; | ||
| 24 | |||
| 25 | use App::Chart; | ||
| 26 | use App::Chart::Download; | ||
| 27 | use App::Chart::DownloadHandler; | ||
| 28 | use App::Chart::FinanceQuote; | ||
| 29 | use App::Chart::IntradayHandler; | ||
| 30 | use App::Chart::Sympred; | ||
| 31 | use App::Chart::TZ; | ||
| 32 | use App::Chart::Weblink; | ||
| 33 | |||
| 34 | # uncomment this to run the ### lines | ||
| 35 | # use Smart::Comments; | ||
| 36 | |||
| 37 | use constant DEBUG => 0; | ||
| 38 | |||
| 39 | |||
| 40 | my $pred = App::Chart::Sympred::Suffix->new ('.TSP'); | ||
| 41 | |||
| 42 | # FIXME: is east coast right? | ||
| 43 | App::Chart::TZ->newyork->setup_for_symbol ($pred); | ||
| 44 | |||
| 45 | App::Chart::setup_source_help | ||
| 46 | ($pred, __p('manual-node','Thrift Savings Plan')); | ||
| 47 | |||
| 48 | App::Chart::FinanceQuote->setup (pred => $pred, | ||
| 49 | suffix => '.TSP', | ||
| 50 | modules => ['TSP'], | ||
| 51 | method => 'tsp'); | ||
| 52 | |||
| 53 | |||
| 54 | #------------------------------------------------------------------------------ | ||
| 55 | # weblink | ||
| 56 | |||
| 57 | # only home page, per "Linkage to the TSP Web Site" requirement | ||
| 58 | App::Chart::Weblink->new | ||
| 59 | (pred => $pred, | ||
| 60 | name => __('_TSP Home Page'), | ||
| 61 | desc => __('Open web browser at the US Government Thrift Savings Plan'), | ||
| 62 | proc => sub { | ||
| 63 | require Finance::Quote::TSP; | ||
| 64 | return $Finance::Quote::TSP::TSP_MAIN_URL; | ||
| 65 | }); | ||
| 66 | |||
| 67 | |||
| 68 | #----------------------------------------------------------------------------- | ||
| 69 | # download | ||
| 70 | # | ||
| 71 | # This uses the historical prices page: | ||
| 72 | # | ||
| 73 | use constant TSP_SHARE_PRICES_URL => | ||
| 74 | 'https://www.tsp.gov/InvestmentFunds/FundPerformance/index.html'; | ||
| 75 | # | ||
| 76 | # The CSV format is a POST like (buried in some flamin javascript) | ||
| 77 | # | ||
| 78 | # reloaded=1&startdate=04%2F27%2F2015&enddate=06%2F02%2F2015&fundgroup=L2020&fundgroup=G&fundgroup=C&whichButton=CSV | ||
| 79 | # | ||
| 80 | # In the past the data on each request was limited to 30 days at time, but | ||
| 81 | # there seems no limit now. Data used to go back to 2003-06-02. | ||
| 82 | # | ||
| 83 | # Old POST: | ||
| 84 | # startdate=20030801&enddate=20031128&submit=Retrieve+Share+Prices&prev=0&next=30&whichButton=CSV | ||
| 85 | # | ||
| 86 | # Old page (now 403 redirects): | ||
| 87 | # https://www.tsp.gov/investmentfunds/shareprice/sharePriceHistory.shtml | ||
| 88 | |||
| 89 | use constant CHUNK_SIZE => 20; | ||
| 90 | |||
| 91 | App::Chart::DownloadHandler->new | ||
| 92 | (name => __('TSP'), | ||
| 93 | pred => $pred, | ||
| 94 | available_tdate => \&available_tdate, | ||
| 95 | proc => \&download); | ||
| 96 | |||
| 97 | # 18:55 still showing prev | ||
| 98 | # 20:22 showing today | ||
| 99 | sub available_tdate { | ||
| 100 | App::Chart::Download::tdate_today_after | ||
| 101 | (20,0, App::Chart::TZ->newyork); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub download { | ||
| 105 | my ($symbol_list) = @_; | ||
| 106 | |||
| 107 | my $avail_tdate = available_tdate(); | ||
| 108 | my $lo_tdate = App::Chart::Download::start_tdate_for_update (@$symbol_list); | ||
| 109 | |||
| 110 | if ($lo_tdate > $avail_tdate) { | ||
| 111 | App::Chart::Download::verbose_message | ||
| 112 | (__('TSP nothing further expected yet')); | ||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | # ask for extra in case available_tdate() is a bit short | ||
| 117 | $avail_tdate += 2; | ||
| 118 | |||
| 119 | my $resp = get_chunk ($symbol_list, $lo_tdate, $avail_tdate); | ||
| 120 | my $h = parse ($resp); | ||
| 121 | $h->{'last_download'} = 1; | ||
| 122 | App::Chart::Download::write_daily_group ($h); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub backto { | ||
| 126 | my ($symbol_list, $backto_tdate) = @_; | ||
| 127 | my $hi_tdate = App::Chart::Download::start_tdate_for_backto (@$symbol_list); | ||
| 128 | my $resp = get_chunk ($backto_tdate, $hi_tdate); | ||
| 129 | my $h = parse ($resp); | ||
| 130 | App::Chart::Download::write_daily_group ($h); | ||
| 131 | } | ||
| 132 | |||
| 133 | # return a HTTP::Response for data $lo_tdate to $hi_tdate, inclusive | ||
| 134 | sub get_chunk { | ||
| 135 | my ($symbol_list, $lo_tdate, $hi_tdate) = @_; | ||
| 136 | |||
| 137 | App::Chart::Download::status | ||
| 138 | (__x('TSP data {date_range}', | ||
| 139 | date_range => | ||
| 140 | App::Chart::Download::tdate_range_string ($lo_tdate, $hi_tdate))); | ||
| 141 | |||
| 142 | my ($lo_year, $lo_month, $lo_day) = App::Chart::tdate_to_ymd ($lo_tdate); | ||
| 143 | my ($hi_year, $hi_month, $hi_day) = App::Chart::tdate_to_ymd ($hi_tdate); | ||
| 144 | my $startdate = sprintf "%02d/%02d/%04d", $lo_month, $lo_day, $lo_year; | ||
| 145 | my $enddate = sprintf "%02d/%02d/%04d", $hi_month, $hi_day, $hi_year; | ||
| 146 | my $fundgroups = join('', map { | ||
| 147 | my $symbol = App::Chart::symbol_sans_suffix($_); | ||
| 148 | if ($symbol eq 'LINCOME') { $symbol = 'Linc'; } | ||
| 149 | "&fundgroup=$symbol" | ||
| 150 | } @$symbol_list); | ||
| 151 | |||
| 152 | return App::Chart::Download->get | ||
| 153 | (TSP_SHARE_PRICES_URL, | ||
| 154 | method => 'POST', | ||
| 155 | data => "reloaded=1&startdate=$startdate&enddate=$enddate$fundgroups&whichButton=CSV"); | ||
| 156 | } | ||
| 157 | |||
| 158 | # $resp is a HTTP::Response object, return a hashref of data | ||
| 159 | sub parse { | ||
| 160 | my ($resp) = @_; | ||
| 161 | |||
| 162 | # From 1 Jul 2008 prices are 4 decimal places, and old data is padded with | ||
| 163 | # zeros. Could prefer_decimals to strip them, but may as well leave at | ||
| 164 | # current precision. | ||
| 165 | # | ||
| 166 | my @data = (); | ||
| 167 | my $h = { source => __PACKAGE__, | ||
| 168 | currency => 'USD', | ||
| 169 | date_format => 'ymd', | ||
| 170 | suffix => '.TSP', | ||
| 171 | data => \@data }; | ||
| 172 | |||
| 173 | my $content = $resp->decoded_content (raise_error=>1); | ||
| 174 | ### $content | ||
| 175 | |||
| 176 | my @lines = App::Chart::Download::split_lines ($content); | ||
| 177 | my ($date, @symbols) = split /,/, $lines[0]; | ||
| 178 | lc($date) eq 'date' | ||
| 179 | or die 'TSP csv doesn\'t start with "date": ', $lines[0]; | ||
| 180 | shift @lines; | ||
| 181 | ### @symbols | ||
| 182 | |||
| 183 | # "L Income" becomes uppercase "LINCOME.TSP" (documented in chart.texi | ||
| 184 | # that way) | ||
| 185 | @symbols = map { my $symbol = $_; | ||
| 186 | $symbol =~ s/ Fund//i; | ||
| 187 | $symbol =~ s/ //g; | ||
| 188 | "\U$symbol.TSP" } | ||
| 189 | @symbols; | ||
| 190 | ### @symbols | ||
| 191 | |||
| 192 | my @names = map {symbol_to_name($_)} @symbols; | ||
| 193 | |||
| 194 | foreach my $line (@lines) { | ||
| 195 | my ($date, @prices) = split /,/, $line; | ||
| 196 | foreach my $c (0 .. $#prices) { | ||
| 197 | push @data, { symbol => $symbols[$c], | ||
| 198 | name => $names[$c], | ||
| 199 | date => $date, | ||
| 200 | close => $prices[$c], | ||
| 201 | }; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | if (DEBUG) { | ||
| 205 | require List::Util; | ||
| 206 | print "min date ",List::Util::minstr(map {$_->{'date'}} @data),"\n"; | ||
| 207 | print "max date ",List::Util::maxstr(map {$_->{'date'}} @data),"\n"; | ||
| 208 | } | ||
| 209 | return $h; | ||
| 210 | } | ||
| 211 | |||
| 212 | # $symbol is like "L2050.TSP", return a name from the table in | ||
| 213 | # Finance::Quote::TSP, or undef if unknown. The names table there is not a | ||
| 214 | # documented feature, so guard with an eval. | ||
| 215 | # | ||
| 216 | sub symbol_to_name { | ||
| 217 | my ($symbol) = @_; | ||
| 218 | # eg. "TSPL2050" or "TSPGFUND" | ||
| 219 | $symbol = 'TSP'.App::Chart::symbol_sans_suffix($symbol); | ||
| 220 | return eval { | ||
| 221 | require Finance::Quote::TSP; | ||
| 222 | $Finance::Quote::TSP::TSP_FUND_NAMES{$symbol} | ||
| 223 | || $Finance::Quote::TSP::TSP_FUND_NAMES{$symbol.'FUND'} | ||
| 224 | }; | ||
| 225 | } | ||
| 226 | |||
| 227 | 1; | ||
| 228 | __END__ | ||
| 229 | |||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | # Old code parsing HTML table. | ||
| 237 | # { | ||
| 238 | # my ($resp) = @_; | ||
| 239 | # | ||
| 240 | # # From 1 Jul 2008 prices are 4 decimal places, and old data is padded with | ||
| 241 | # # zeros. Could prefer_decimals to strip them, but may as well leave at | ||
| 242 | # # current precision. | ||
| 243 | # # | ||
| 244 | # my @data = (); | ||
| 245 | # my $h = { source => __PACKAGE__, | ||
| 246 | # currency => 'USD', | ||
| 247 | # date_format => 'mdy', | ||
| 248 | # suffix => '.TSP', | ||
| 249 | # data => \@data }; | ||
| 250 | # | ||
| 251 | # my $content = $resp->decoded_content (raise_error=>1); | ||
| 252 | # ### $content | ||
| 253 | # | ||
| 254 | # require HTML::TableExtract; | ||
| 255 | # my $te = HTML::TableExtract->new (headers => [qr/^Date$/i], | ||
| 256 | # keep_headers => 1, | ||
| 257 | # slice_columns => 0); | ||
| 258 | # $te->parse($content); | ||
| 259 | # my $ts = $te->first_table_found(); | ||
| 260 | # #### $ts | ||
| 261 | # if (! $ts) { die 'TSP price table not found'; } | ||
| 262 | # | ||
| 263 | # my $rows = $ts->rows(); | ||
| 264 | # my $lastrow = $#$rows; | ||
| 265 | # my $lastcol = $#{$rows->[0]}; | ||
| 266 | # | ||
| 267 | # my @symbol = map { my $symbol = $_; | ||
| 268 | # $symbol =~ s/ Fund//i; | ||
| 269 | # $symbol =~ s/ //g; | ||
| 270 | # # upper case for "LINCOME.TSP" (documented in | ||
| 271 | # # chart.texi that way) | ||
| 272 | # $symbol eq 'Date' ? undef : "\U$symbol.TSP" } @{$rows->[0]}; | ||
| 273 | # | ||
| 274 | # require Finance::Quote::TSP; | ||
| 275 | # my @name = map { my $key = $_; | ||
| 276 | # $key =~ s/ //g; | ||
| 277 | # $Finance::Quote::TSP::TSP_FUND_NAMES{"TSP$key"} } | ||
| 278 | # @{$rows->[0]}; | ||
| 279 | # | ||
| 280 | # foreach my $r (1 .. $lastrow) { | ||
| 281 | # my $row = $rows->[$r]; | ||
| 282 | # if (DEBUG) { require Data::Dumper; | ||
| 283 | # print Data::Dumper::Dumper($row); } | ||
| 284 | # my $date = $row->[0]; | ||
| 285 | # | ||
| 286 | # foreach my $c (1 .. $lastcol) { | ||
| 287 | # my $price = $row->[$c]; | ||
| 288 | # | ||
| 289 | # push @data, { symbol => $symbol[$c], | ||
| 290 | # name => $name[$c], | ||
| 291 | # date => $date, | ||
| 292 | # close => $price, | ||
| 293 | # }; | ||
| 294 | # } | ||
| 295 | # } | ||
| 296 | # if (DEBUG) { | ||
| 297 | # require List::Util; | ||
| 298 | # print "min date ",List::Util::minstr(map {$_->{'date'}} @data),"\n"; | ||
| 299 | # print "max date ",List::Util::maxstr(map {$_->{'date'}} @data),"\n"; | ||
| 300 | # } | ||
| 301 | # return $h; | ||
| 302 | # } |