| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Example stock-ticker program. Can look up stocks from multiple |
| 4 |
# markets and return the results in local currency. |
| 5 |
# |
| 6 |
# Revision: 1.1 |
| 7 |
|
| 8 |
use strict; |
| 9 |
use Finance::Quote; |
| 10 |
|
| 11 |
my $CURRENCY = "AUD"; # Set preferred currency here, or empty string for |
| 12 |
# no conversion. |
| 13 |
|
| 14 |
# The stocks array contains a set of array-references. Each reference |
| 15 |
# has the market as the first element, and a set of stocks thereafter. |
| 16 |
|
| 17 |
my @STOCKS = ([qw/australia CML ITE BHP/], |
| 18 |
[qw/usa MSFT RHAT LNUX/] |
| 19 |
); |
| 20 |
|
| 21 |
# These define the format. The first item in each pair is the label, |
| 22 |
# the second is the printf-style formatting, the third is the width |
| 23 |
# of the field (used in printing headers). |
| 24 |
|
| 25 |
my @labels = (["name", "%12s", 15], |
| 26 |
["date", "%11s", 11], |
| 27 |
["time", "%10s", 11], |
| 28 |
["last", "%8.2f", 8], |
| 29 |
["high", "%8.2f", 8], |
| 30 |
["low", "%8.2f", 8], |
| 31 |
["close", "%8.2f", 8], |
| 32 |
["volume","%10d", 10]); |
| 33 |
|
| 34 |
my $REFRESH = 120; # Seconds between refresh. |
| 35 |
|
| 36 |
# --- END CONFIG SECTION --- |
| 37 |
|
| 38 |
my $quoter = Finance::Quote->new(); |
| 39 |
my $clear = `clear`; # So we can clear the screen. |
| 40 |
|
| 41 |
# Build our header. |
| 42 |
|
| 43 |
my $header = "\t\t\t\tSTOCK REPORT" .($CURRENCY ? " ($CURRENCY)" : "") ."\n\n"; |
| 44 |
|
| 45 |
foreach my $tuple (@labels) { |
| 46 |
my ($name, undef, $width) = @$tuple; |
| 47 |
$header .= sprintf("%".$width."s",uc($name)); |
| 48 |
} |
| 49 |
|
| 50 |
$header .= "\n".("-"x79)."\n"; |
| 51 |
|
| 52 |
# Header is all built. Looks beautiful. |
| 53 |
|
| 54 |
$quoter->set_currency($CURRENCY) if $CURRENCY; # Set default currency. |
| 55 |
|
| 56 |
for (;;) { # For ever. |
| 57 |
print $clear,$header; |
| 58 |
|
| 59 |
foreach my $stockset (@STOCKS) { |
| 60 |
my ($exchange, @symbols) = @$stockset; |
| 61 |
my %info = $quoter->fetch($exchange,@symbols); |
| 62 |
|
| 63 |
foreach my $symbol (@symbols) { |
| 64 |
next unless $info{$symbol,"success"}; # Skip failures. |
| 65 |
foreach my $tuple (@labels) { |
| 66 |
my ($label,$format) = @$tuple; |
| 67 |
printf $format,$info{$symbol,$label}; |
| 68 |
} |
| 69 |
print "\n"; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
sleep($REFRESH); |
| 74 |
} |
| 75 |
|
| 76 |
__END__ |