I recently stumbled upon a neat little perl module called
Finance::YahooQuote that gets quote data from yahoo.  The example script
included with the package gets quotes and echoes them on the command
line.

I expanded upon that and created the script (attached) to read a
.portfolio file and echo the current price and gain/gain percentage.
Those who have some money invested may find it useful.  The output looks
as follows:

                             MY PORTFOLIO

STOCK     Price     Change    % Ch    Worth      Paid       Gain      % Gain
-----------------------------------------------------------------------------
RHAT      19.3750  -0.6875   -3.43%   $ 1937.50  $ 1500.00  $ 437.50   29.17%
LNUX      38.7500  -0.3750   -0.96%   $ 3875.00  $ 2300.00  $1575.00   68.48%
-----------------------------------------------------------------------------
                                      $ 5812.50  $ 3800.00  $2012.50   52.96%


The .portfolio file for this portfolio is:
rhat  100  15
lnux  100  23

With the format being: ticker, shares, price at purchase
separated by white space.

Note: Finance::YahooQuote requires a few other modules.  If you get an
error at run-time, install the modules that are stated missing from you
@INC.  I used CPAN via:

perl -MCPAN -e 'shell'

then:

CPAN> install Finance::YahooQuote


-Rob.

portfolio.pl follows...

#!/usr/local/bin/perl
use Finance::YahooQuote;
## getquote returns:
#    0  -> Symbol
#    1  -> Name
#    2  -> Last
#    3  -> Trade Date
#    4  -> Trade Time
#    5  -> Change
#    6  -> % Change
#    7  -> Volume
#    8  -> Avg. Daily Volume
#    9  -> Bid
#    10 -> Ask
#    11 -> Prev. Close
#    12 -> Open
#    13 -> Day's Range
#    14 -> 52-Week Range
#    15 -> EPS
#    16 -> P/E Ratio
#    17 -> Div. Pay Rate
#    18 -> Div/Share
#    19 -> Div. Yield
#    20 -> Mkt. Cap
#    21 -> Exchange

$Finance::YahooQuote::TIMEOUT = 30;
open (IN, $ENV{HOME} . "/.portfolio") || die "Can't open .portfolio file";
for ($x = 0; <IN>; $x++) {
  push @stocks, [ split ('[\s]+', $_) ];
  $stock_list .= $stocks[$x][0] . " ";
}

for $stock (@stocks) {
  @q = getquote ($stock_list);
}

$gain_total = 0;
$paid_total = 0;
$worth_total = 0;
$x = 0;

# Start to print results
format STDOUT_TOP =
                             
                             MY PORTFOLIO

STOCK     Price     Change    % Ch    Worth      Paid       Gain      % Gain
-----------------------------------------------------------------------------
.
format STDOUT =
@<<<<< @####.#### @##.#### @###.##%   $@####.##  $@####.##  $@###.## @###.##%
$ticker, $price, $change, $pctchange, $worth, $paid, $gain, $pctgain
.

foreach $a (@q) {
  $shares = $stocks[$x][1];
  $priceper = $stocks[$x][2];
  $ticker = $$a[0];
  $price  = $$a[2];
  $change = $$a[5];
  $pctchange = "$$a[6] \%";
  $paid = sprintf ("%.2f", ($shares * $priceper));
  $worth = sprintf ("%.2f", ($shares * $price));
  $gain = sprintf ("%.2f", ($worth - $paid));
  $pctgain = sprintf ("%.2f", (($gain / $paid) * 100));

  $gain_total += $gain;
  $worth_total += $worth;
  $paid_total += $paid;

  write;

  $x++;
}

$pctgain_total = sprintf ("%.2f", (($gain_total/$paid_total)*100));

format TOTALS =
-----------------------------------------------------------------------------
                                      $@####.##  $@####.##  $@###.## @###.##%
$worth_total, $paid_total, $gain_total, $pctgain_total
.

$~ = TOTALS;
write;

print "\n\n";

Reply via email to