Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Perl, fink and geektool' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Perl, fink and geektool
Authored by: sfr79 on Sep 12, '04 10:40:57AM

Here's my updated attempt at the previous poster's script. I've added a quick way to put in your portfolio.

#!/usr/bin/perl
use lib '/sw/lib/perl5/5.8.1';
use LWP::Simple;
use Term::ANSIColor;

# This is where you enter your currently owned stocks in the following format:
# @Portfolio[0] = "Symbol,Shares,Buy Price";
# ex @Portfolio[0] = "AAPL,100,24.2";

@Portfolio[0] = "AAPL,100,24.2";

# For every stock in your portfolio, get the current info and
# calculate the change.
foreach $array_element(@Portfolio)
{
@info = split(/,/, $array_element);

# Historical Portfolio Info
$Stock = @info[0];
$quantity = @info[1];
$boughtAt = @info[2];
$firstWorth = $quantity * $boughtAt;

# Get Current Quote from Yahoo. Note the sl1d1t1c1ohgv
# part might have to be edited by going to yahoo's site
$theString = get("http://finance.yahoo.com/d/quotes.csv?s=$Stock&f=sl1d1t1c1ohgv&e=.csv");
die "Failed to get Stock Data" unless defined $theString;
$theString =~ s/\"//g; #"
@data = split(/,/, $theString);

# Current Quote Stats
$todayPrice = @data[1];
$todayWorth = $quantity * $todayPrice;
$todayChange = @data[4];

# Gain in stock price
$gain = $todayWorth - $firstWorth;
$todayGain = $quantity * $todayChange;

#Portfolio Info
$portfolioDayGain = $portfolioDayGain + $todayGain;
$portfolioGain = $portfolioGain + $gain;
$portfolioChange = $portfolioChange + $todayChange;
$portfolioFirstWorth = $portfolioFirstWorth + $firstWorth;
$portfolioCurrentWorth = $portfolioCurrentWorth + $todayWorth;

#Convert Numbers to 2 decimals
$gain = sprintf "%.2f", $gain;
$todayChange = sprintf "%.2f", $todayChange;
$todayPrice = sprintf "%.2f" , $todayPrice;
print "$Stock\t \$$todayPrice \t$todayChange\n";
#print "currWorth: $todayWorth\n";
#print "\tOverall: \$$gain \t$todayGain\n";
}

#print color 'bold';
$portfolioDayGain = sprintf "%.2f", $portfolioDayGain;
$portfolioGain = sprintf "%.2f", $portfolioGain;
$portfolioChange = sprintf "%.2f", $portfolioChange;

# Last Update
print "@data[2] @data[3]\n";
# Overall: TotalGain TodaysGain
print "Ovr: \$$portfolioGain \t$portfolioDayGain\n";

exit 0;



[ Reply to This | # ]