document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!perl
  2. use warnings;
  3. use strict;
  4. use diagnostics;
  5.  
  6. use Getopt::Long;
  7. use Date::Manip;
  8. use DBI;
  9. use HTML::TreeBuilder;
  10. use Text::CSV_XS;
  11. use LWP::Simple qw/get/;
  12.  
  13. # TODO: Write out LaTeX report
  14. sub recall {
  15.     my $DATE_FORMAT = \'%Y-%m-%d\';
  16.     my $GOOGLE_DATE_FORMAT = \'%b+%d,+%Y\';
  17.     my $infilename = UnixDate(\'today\', $DATE_FORMAT).".csv";
  18.     my $outfilename = UnixDate(\'today\', $DATE_FORMAT)."_recall.csv";
  19.     open(my $out, \'>\', "$outfilename") or die "$!\\n";
  20.     # http://finance.google.com/finance/historical?output=csv&q=bXXXXXX&startdate=Jan+01,+2007&enddate=Aug+14,+2015&
  21.     my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "$/" });
  22.     open my $fh, "<:encoding(utf8)", "$infilename" or die "$infilename: $!";
  23.     open my $out, ">:encoding(utf8)", "outfilename" or die "$outfilename: $!";
  24.     $csv->print($out, [\'Stock Symbol\',\'Change\']);
  25.     while (my $row = $csv->getline ($fh)) {
  26.         next unless $row->[0] =~ /^[A-Z]+$/;
  27.         my $stock = $row->[0];
  28.         my $startdate = UnixDate(\'yesterday\',$GOOGLE_DATE_FORMAT);
  29.         my $enddate = UnixDate(\'today\', $GOOGLE_DATE_FORMAT);
  30.         my $googleUrl = "http://finance.google.com/finance/historical?output=csv&q=$stock&startdate=$startdate&enddate=$enddate";
  31.         my @data = split /,/, get($googleUrl);
  32.         my $change = $data[14]-$data[9];
  33.         $csv->print($out, [$stock, $change]);
  34.         $csv->print(\\*STDERR, [$stock, $change]);
  35.     }
  36.     close $fh;
  37.     close($outfilename);
  38. }
  39.  
  40. sub precision {
  41.     my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "$/" });
  42.    
  43.     my $DATE_FORMAT = \'%Y-%m-%d\';
  44.     my $today = UnixDate(\'now\', $DATE_FORMAT);
  45.     open (my $out, \'>\', "$today.csv") or die "$!\\n";
  46.     my $URL = \'http://madmoney.thestreet.com/screener/index.cfm?showview=stocks&showrows=500\';
  47.     my $tree = HTML::TreeBuilder->new_from_url($URL);
  48.     my $stockTable = $tree->look_down(\'_tag\',\'table\', sub { $_[0]->{\'id\'} eq \'stockTable\'; });
  49.     my @stocks = [];
  50.     my @s = $stockTable->look_down(\'_tag\',\'td\', sub { $_[0]->as_text =~ /\\(([^)]+)\\)/ && push @stocks, "$1";});
  51.     my @direction = [];
  52.     my @d = $stockTable->look_down(\'_tag\', \'img\', sub { $_[0]->{\'src\'} =~ /([1-5])\\.gif/ && push @direction, "$1"; });
  53.     $csv->print($out, [\'stock\', "Cramer\'s predicted direction"]);
  54.     $csv->eol;
  55.     my $stocks = $#d;
  56.     for (my $i = 0; $i < $stocks; $i++) {
  57.         $csv->print($out, [$stocks[$i], "down"]) if $direction[$i] eq \'1\';
  58.         $csv->print($out, [$stocks[$i], \'up\']) if $direction[$i] eq \'5\';
  59.         $csv->eol if $stocks[$i];
  60.     }
  61.     close($out);
  62.     $tree->delete;
  63. }
  64.  
  65. my $operation;
  66. GetOptions("op=s", \\$operation) or die("Error in command line arguments\\n");
  67. &precision if $operation eq \'get\';
  68. &recall if $operation eq \'check\';
');