| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Run perl over this document to get Jon Orwant preferred formatting. :) |
| 4 |
# |
| 5 |
|
| 6 |
use strict; |
| 7 |
my $code_indent = -1; # -1 means we're not in code. |
| 8 |
while (<DATA>) { |
| 9 |
(/(\t*)#!\/\S+\/perl/) and $code_indent = length($1); |
| 10 |
$code_indent = -1 if (/__END__/); |
| 11 |
if ($code_indent >= 0) { s/^\t{0,$code_indent}//; } else { s/^\t*//; } |
| 12 |
print; |
| 13 |
} |
| 14 |
|
| 15 |
__END__ |
| 16 |
|
| 17 |
Finance::Quote (for TPJ) |
| 18 |
Paul Fenwick <pjf at cpan dot org>, Aug 2000 |
| 19 |
$Revision: 1.1 $ |
| 20 |
|
| 21 |
Introduction |
| 22 |
============ |
| 23 |
If you have a reason to watch the world's financial |
| 24 |
markets, and you know a little about perl, then you |
| 25 |
may find that the Finance::Quote package comes in handy. |
| 26 |
I personally use it to remind myself that I should never |
| 27 |
buy shares, as I have a good history of losing money |
| 28 |
on the stock exchange. However, you can use Finance::Quote |
| 29 |
to help track those tasty stock options you've been offered, |
| 30 |
or even to help you build dynamic artwork driven by |
| 31 |
fluctuations in the world markets[1]. |
| 32 |
|
| 33 |
Finance::Quote is a perl module that makes accessing financial |
| 34 |
information easy. You can look up stocks and shares, managed |
| 35 |
funds, currency rates, and all this from a variety of countries |
| 36 |
and markets. It's even easy to add your own specialised sources |
| 37 |
if you find something the module does not do. |
| 38 |
|
| 39 |
[1] Yes, this actually exists and uses Finance::Quote to |
| 40 |
help drive the back-end. See |
| 41 |
<http://www.terrestrialmap.org/StockPuppets.htm>. |
| 42 |
|
| 43 |
History (or credit where credit is due). |
| 44 |
======================================== |
| 45 |
|
| 46 |
Finance::Quote started as the Quote module which was |
| 47 |
distributed as part of Gnucash <http://www.gnucash.org>. |
| 48 |
This module (simple called "Quote") was a modified version |
| 49 |
of Finance::YahooQuote written by DJ Padzensky |
| 50 |
<http://www.padz.net/~djpadz/YahooQuote/>. Linas Vepstas |
| 51 |
and Yannick LE NY had added extra sources for GnuCash, |
| 52 |
at which point I asked if anyone had any objections to me |
| 53 |
breaking it off into a separate project with a range of |
| 54 |
uses outside of GnuCash. |
| 55 |
|
| 56 |
Since that time, many people have contributed to Finance::Quote, |
| 57 |
including Xose Manoel Ramos (for inheritable Yahoo! methods), |
| 58 |
Brent Neal, Keith Refson, and Volker Stuerzl for writing |
| 59 |
pluggable modules, and Peter Thatcher, Jacinta Richardson |
| 60 |
and Steven Murdoch for various bugfixes and improvements. |
| 61 |
Ben Hemming was kind enough to save the world from my poor |
| 62 |
web-design skills and supply a web-page look-and-feel. |
| 63 |
|
| 64 |
|
| 65 |
Legality (Or what can I use it for?) |
| 66 |
==================================== |
| 67 |
|
| 68 |
When discussing Finance::Quote, one question which often |
| 69 |
arises is "how legal is all this"? Finance::Quote obtains |
| 70 |
quotes of various organisations who make this information |
| 71 |
available via the web. Actually fetching the data is |
| 72 |
not illegal[2], web-browsers do it all the time, and |
| 73 |
Finance::Quote is just a rather specialised web-browser. |
| 74 |
The main problems occur with what you do with that data. |
| 75 |
|
| 76 |
Each organisation has a different set of restrictions |
| 77 |
and conditions pertaining to the data you obtain from |
| 78 |
them. The most common restriction is that forbidding |
| 79 |
redistribution, although some of them are as odd to |
| 80 |
limit the number of copies you can have in memory at |
| 81 |
any one time! The Finance::Quote manual pages provide |
| 82 |
some pointers as to where these licenses can be found, |
| 83 |
and it would be wise to check those licenses if you |
| 84 |
wish to be in safe legal waters. |
| 85 |
|
| 86 |
In general, obtaining information and displaying it or |
| 87 |
processing it into some useful form for personal |
| 88 |
use should be okay -- that's what your web-browser does. |
| 89 |
Obtaining the information and re-badging it as your |
| 90 |
own is probably a no-no. If in doubt, check with your |
| 91 |
lawyer. |
| 92 |
|
| 93 |
[2] Although there have been a couple of court cases where |
| 94 |
"deep linking" into web-page has been cast into legal |
| 95 |
shadow. |
| 96 |
|
| 97 |
Terminology |
| 98 |
=========== |
| 99 |
|
| 100 |
Before we begin, it's worth taking the time to explain a |
| 101 |
few pieces of terminology that may be otherwise a little |
| 102 |
confusing. For simplicity, a stock, mutual fund, index, |
| 103 |
or other parcel of information which can be fetched using |
| 104 |
Finance::Quote we will refer to as a "stock". |
| 105 |
|
| 106 |
All stocks have a unique identifier or "symbol" which we |
| 107 |
can use to look them up, and an "exchange" (or locality) |
| 108 |
in which they exist. For example, VA Linux Systems have |
| 109 |
the symbol "LNUX" on the Nasdaq exchange. Symbols are |
| 110 |
traditionally all upper-case, but there exist some symbols |
| 111 |
(such as the pseudo-symbols used in the TIAA-CREF module) |
| 112 |
that are mixed case. |
| 113 |
|
| 114 |
Finance::Quote essentially provides a way of taking a |
| 115 |
list of symbols for a given exchange, and returning |
| 116 |
information about those symbols. Each bit of information |
| 117 |
has a "label" (such as volume, close, high, low, etc), |
| 118 |
which identifies what that information is. All making |
| 119 |
sense? Good. Let's do something fun then. |
| 120 |
|
| 121 |
Using Finance::Quote |
| 122 |
==================== |
| 123 |
|
| 124 |
The Basics |
| 125 |
---------- |
| 126 |
I'll demonstrate the usage of Finance::Quote by way of |
| 127 |
a useful example, which will be expanded upon as we |
| 128 |
go. For starters, let's just say you're interested in |
| 129 |
stocks from a single market, and wish to print their |
| 130 |
current value and volume traded. |
| 131 |
|
| 132 |
#!/usr/bin/perl -w |
| 133 |
use strict; |
| 134 |
use Finance::Quote; |
| 135 |
|
| 136 |
@ARGV >= 2 or die "Usage: $0 exchange symbol symbol symbol ...\n"; |
| 137 |
|
| 138 |
my $exchange = shift; # Where do we fetch our stocks from. |
| 139 |
my @symbols = @ARGV; # Which stocks are we interested in. |
| 140 |
|
| 141 |
my $quoter = Finance::Quote->new; # Create the F::Q object. |
| 142 |
|
| 143 |
$quoter->timeout(30); # Cancel fetch operation if it takes |
| 144 |
# longer than 30 seconds. |
| 145 |
|
| 146 |
# Grab our information and place it into %info. |
| 147 |
my %info = $quoter->fetch($exchange,@symbols); |
| 148 |
|
| 149 |
foreach my $stock (@symbols) { |
| 150 |
unless ($info{$stock,"success"}) { |
| 151 |
warn "Lookup of $stock failed - ".$info{$stock,"errormsg"}. |
| 152 |
"\n"; |
| 153 |
next; |
| 154 |
} |
| 155 |
print "$stock:\t\t", |
| 156 |
"Volume: ",$info{$stock,"volume"},"\t", |
| 157 |
"Price: " ,$info{$stock,"price"},"\n"; |
| 158 |
} |
| 159 |
__END__ |
| 160 |
|
| 161 |
If our script was called "showstocks", and you were |
| 162 |
interested in Australian supermarkets, you could call |
| 163 |
it like this: |
| 164 |
|
| 165 |
showstocks australia CML WOW |
| 166 |
|
| 167 |
This will provide you with information about Coles-Myer |
| 168 |
and Woolworths. |
| 169 |
|
| 170 |
Alternatively, if you were interested some US |
| 171 |
technology stocks, you could try this: |
| 172 |
|
| 173 |
showstocks usa LNUX RHAT MSFT IBM |
| 174 |
|
| 175 |
which will provide you with information about VA Linux |
| 176 |
Systems, Red Hat, Microsoft and International Business |
| 177 |
Machines. |
| 178 |
|
| 179 |
This script demonstrates a number of capabilities of |
| 180 |
Finance::Quote. In particular: |
| 181 |
|
| 182 |
* Finance::Quote is object-oriented, |
| 183 |
and a Finance::Quote object can be |
| 184 |
generated using Finance::Quote->new(). |
| 185 |
|
| 186 |
* The fetch() method can be used for |
| 187 |
retrieving information. This method |
| 188 |
is very powerful, and will be explained |
| 189 |
in some depth later in this article. |
| 190 |
|
| 191 |
* The fetch method returns a two-dimensional |
| 192 |
hash. This is the topic of our next |
| 193 |
section. |
| 194 |
|
| 195 |
The Return Hash |
| 196 |
--------------- |
| 197 |
Finance::Quote's most useful function, fetch(), |
| 198 |
returns a two-dimensional hash. This contains |
| 199 |
a variety of information about the stocks you |
| 200 |
requested, including volume, price, highs and lows, |
| 201 |
percentage changes, and other information. Each |
| 202 |
key in the hash has two parts: |
| 203 |
|
| 204 |
$info{$symbol,$label} |
| 205 |
|
| 206 |
The symbol is the symbol that you've requested. In |
| 207 |
the examples above, "RHAT" and "LNUX" are examples |
| 208 |
of symbols. The label refers to a specific type |
| 209 |
of information about that stock, such as "volume", |
| 210 |
"price", "close", "p_change" or "name". Labels are |
| 211 |
always lower-case. |
| 212 |
|
| 213 |
There exists some very special labels that you should |
| 214 |
be aware of. The label "success" is used to |
| 215 |
indicate if the information could be successfully |
| 216 |
retrieved. If the value of the success label for a |
| 217 |
given stock is false, then no useful information could |
| 218 |
be gained about that symbol at this time. If a failure |
| 219 |
did occur, the reason for that failure will be in the |
| 220 |
label "errormsg". |
| 221 |
|
| 222 |
The special label "price" is used to indicate the |
| 223 |
worth or value of the given stock. This varies a |
| 224 |
little depending upon what you're fetching information |
| 225 |
on. For stocks, it's usually the last price the stock |
| 226 |
was traded at. For some investments, it's the current |
| 227 |
yield (a percentage per-annum). For currencies |
| 228 |
it's the exchange rate, and for indexes it's the |
| 229 |
last value of that index (in points). |
| 230 |
|
| 231 |
The reason the price label is important is that it |
| 232 |
allows for applications to track the movement of |
| 233 |
information without having to know if we're dealing |
| 234 |
with a stock or a managed fund or an indicator -- |
| 235 |
the price label will provide us with the information |
| 236 |
that we (usually) want. This is particularly |
| 237 |
useful for things such as stock-tickers, allowing |
| 238 |
them to track things other than just stocks. |
| 239 |
|
| 240 |
For applications that care about the details, there |
| 241 |
are a wide range of labels that can be returned. |
| 242 |
These include the highest and lowest prices for the |
| 243 |
day, dividend yields and dates, the time and date |
| 244 |
that the information is current for, the volume |
| 245 |
traded, the name of the stock, and many others. |
| 246 |
The standard labels are listed in the side-bar. |
| 247 |
|
| 248 |
It's important to remember that the information fetched |
| 249 |
by Finance::Quote is usually delayed, and so is often 30 |
| 250 |
minutes or more behind what's really happening. If you're |
| 251 |
doing currency conversions as well, then the currency |
| 252 |
conversion rate may also be delayed. You can use the |
| 253 |
labels "date" and "time" to determine when the data was |
| 254 |
valid -- it's not unknown for some sources to provide data |
| 255 |
that is a week old or more. |
| 256 |
|
| 257 |
Advanced Usage |
| 258 |
============== |
| 259 |
|
| 260 |
Now, the basics of Finance::Quote are pretty simple. You |
| 261 |
create yourself a F::Q object, you ask it for some information, |
| 262 |
and you get that information back in a hash. For most |
| 263 |
applications, this is all you really need. However, |
| 264 |
Finance::Quote provides a wide range of extra features to |
| 265 |
help make your life easier. |
| 266 |
|
| 267 |
Currency Conversion |
| 268 |
------------------- |
| 269 |
Finance::Quote has the ability to look up currency |
| 270 |
rates, and can even automatically convert foreign |
| 271 |
stocks into local currencies. Let's say that |
| 272 |
you live in Australia, but you have some stocks |
| 273 |
in the USA. You may be interested in knowing |
| 274 |
the value of those stocks in Australian dollars, |
| 275 |
which have more meaning to you than US dollars. |
| 276 |
Here's how to do it: |
| 277 |
|
| 278 |
#!/usr/bin/perl -w |
| 279 |
use strict; |
| 280 |
use Finance::Quote; |
| 281 |
|
| 282 |
my $market = shift; |
| 283 |
my @stocks = @ARGV; |
| 284 |
|
| 285 |
my $quoter = Finance::Quote->new(); |
| 286 |
$quoter->set_currency("AUD"); # Aussie dollars, thanks. |
| 287 |
my %info = $quoter->fetch($market,@stocks); |
| 288 |
|
| 289 |
# Print the info here. |
| 290 |
|
| 291 |
__END__ |
| 292 |
|
| 293 |
The set_currency() method asks Finance::Quote to |
| 294 |
convert all values into the given currency before |
| 295 |
returning them to you. Finance::Quote knows which |
| 296 |
things it can convert (like prices and ranges), |
| 297 |
and which things it cannot (like percentage changes |
| 298 |
and volumes). It's even smart enough to (usually) |
| 299 |
not touch indexes and other abstract indicators |
| 300 |
that don't have currencies attached to them. |
| 301 |
|
| 302 |
Be aware that set_currency() can significantly increase |
| 303 |
the time of a query, as currency lookup information |
| 304 |
has to be fetched as well as the stock information. |
| 305 |
|
| 306 |
It's also possible to fetch currency exchange rates |
| 307 |
directly. This is done using the currency() method. |
| 308 |
Eg: |
| 309 |
|
| 310 |
my $exchange_rate = $quoter->currency("USD","AUD"); |
| 311 |
print "1 US dollar is $exchange_rate Australian dollars.\n"; |
| 312 |
|
| 313 |
The currency() method can also do clever things like |
| 314 |
take a prefix to the FROM currency. Hence the following |
| 315 |
script is a quick'n'dirty command-line utility to |
| 316 |
convert between currencies: |
| 317 |
|
| 318 |
#!/usr/bin/perl -w |
| 319 |
use strict; |
| 320 |
use Finance::Quote; |
| 321 |
|
| 322 |
# Command-line currency conversion. |
| 323 |
|
| 324 |
die "Usage: $0 FROM TO\n" unless defined($ARGV[1]); |
| 325 |
|
| 326 |
my $quoter = Finance::Quote->new(); |
| 327 |
my $exchange_rate = $quoter->currency($ARGV[0],$ARGV[1]); |
| 328 |
|
| 329 |
die "Don't know how to convert $ARGV[0] to $ARGV[1]\n" unless $exchange_rate; |
| 330 |
|
| 331 |
print "$ARGV[0] -> $ARGV[1] = $exchange_rate\n"; |
| 332 |
__END__ |
| 333 |
|
| 334 |
If this script were to be called "currency-lookup", you |
| 335 |
could show the going rate between Australian and American |
| 336 |
dollars like this: |
| 337 |
|
| 338 |
currency-lookup AUD USD |
| 339 |
|
| 340 |
If you wanted to know how much 95 Australian Dollars were |
| 341 |
in French Francs, you could do this: |
| 342 |
|
| 343 |
currency-lookup "95 AUD" FRF |
| 344 |
|
| 345 |
|
| 346 |
Fail-over Support and Custom Modules |
| 347 |
----------------------------------- |
| 348 |
Finance::Quote provides automatic fail-over support |
| 349 |
if you specify the market that you're interested |
| 350 |
in and not the actual source from which you want |
| 351 |
to fetch it. This means that if you use "nasdaq" instead |
| 352 |
of "yahoo" as your source, Finance::Quote will |
| 353 |
automatically try all sources of nasdaq data in |
| 354 |
case the first one failed. Fail-over support is |
| 355 |
on by default in all newly create Finance::Quote |
| 356 |
objects. |
| 357 |
|
| 358 |
Fail-over support can increase the time of a query, |
| 359 |
especially if you're searching for a non-existent |
| 360 |
stock. It's possible to (un)set fail-over support |
| 361 |
explicitly like this: |
| 362 |
|
| 363 |
$quoter->failover(0); # Disable failover support. |
| 364 |
|
| 365 |
Likewise, when you create your Finance::Quote object, it's |
| 366 |
possible to state which modules you'd like to be |
| 367 |
able to fetch information from. For example: |
| 368 |
|
| 369 |
my $quoter = Finance::Quote->new("Yahoo::Australia"); |
| 370 |
|
| 371 |
will only use the Finance::Quote::Yahoo::Australia |
| 372 |
module for queries. This is particularly useful |
| 373 |
if you have a deal with a particular information |
| 374 |
supplier, or otherwise wish to restrict where |
| 375 |
Finance::Quote can search for information. Specifying modules |
| 376 |
to load at creation time can also let you load |
| 377 |
custom modules that are not part of the standard |
| 378 |
Finance::Quote distribution. |
| 379 |
|
| 380 |
my $quoter = Finance::Quote->new("-defaults", "MyBank"); |
| 381 |
|
| 382 |
Here we would load the Finance::Quote::MyBank module, |
| 383 |
as well as all the default modules that are packaged |
| 384 |
with Finance::Quote. Note that "-defaults" as an |
| 385 |
argument to new is only magical when passed as the |
| 386 |
first argument (although in the future it MAY be |
| 387 |
legal to pass it anywhere in the arguments list). |
| 388 |
|
| 389 |
If you're interested in writing your own modules for |
| 390 |
Finance::Quote, then you should read the Hacker's Guide |
| 391 |
that comes with Finance::Quote, or which can be found |
| 392 |
in the Finance::Quote documentation manager at |
| 393 |
<http://sourceforge.net/docman/index.php?group_id=4232>. |
| 394 |
|
| 395 |
Required Labels |
| 396 |
--------------- |
| 397 |
Fail-over support is a wonderful thing -- the source you're |
| 398 |
fetching data from may have fallen over and you might never |
| 399 |
have to worry. Unfortunately, not all sources provide all |
| 400 |
the information you're looking for, and having bits of |
| 401 |
information disappear when you're used to them being there |
| 402 |
can be a little surprising. |
| 403 |
|
| 404 |
Rather than having to worry about your fail-over sources not |
| 405 |
providing the information you're looking for, Finance::Quote |
| 406 |
allows you to define a list of things that are important |
| 407 |
to you, using the require_labels() method. Say that your |
| 408 |
program relies upon the labels price, date, high, low and |
| 409 |
volume. You can express this to your Finance::Quote object |
| 410 |
like so: |
| 411 |
|
| 412 |
$quoter->require_labels(qw/price date high low volume/); |
| 413 |
|
| 414 |
If you now use the $quoter->fetch() to obtain information, you |
| 415 |
can be guaranteed that those fields will be available if they |
| 416 |
apply to what you've requested. This means you can use |
| 417 |
fail-over methods safe in the knowledge that the information |
| 418 |
you actually care about will not disappear. |
| 419 |
|
| 420 |
Tricks with the user agent |
| 421 |
-------------------------- |
| 422 |
|
| 423 |
If you've ever used LWP::UserAgent before, then you'll be |
| 424 |
pleased to know that you can customise the underlying |
| 425 |
LWP::UserAgent object in Finance::Quote. For example: |
| 426 |
|
| 427 |
$quoter->user_agent->agent("MyTicker/0.1"); |
| 428 |
|
| 429 |
will cause the $quoter object to identify itself as |
| 430 |
"MyTicker/0.1" in HTTP sessions. Likewise, this lets |
| 431 |
you set your proxy explicitly: |
| 432 |
|
| 433 |
$quoter->user_agent->proxy('http',$MY_PROXY); |
| 434 |
|
| 435 |
Note that the UserAgent that Finance::Quote uses |
| 436 |
automatically respects proxy environment variables |
| 437 |
(such as "http_proxy") at creation time. |
| 438 |
|
| 439 |
For more information on what you can and cannot do |
| 440 |
here, check out the documentation for LWP::UserAgent. |
| 441 |
|
| 442 |
|
| 443 |
Rolling your own Finance::Quote module |
| 444 |
======================================= |
| 445 |
As well as using the standard Finance::Quote modules, it's |
| 446 |
also possible to write your own module that you can ask |
| 447 |
Finance::Quote to use. Writing such a module can be a tricky |
| 448 |
task, and is beyond the scope of this article. |
| 449 |
|
| 450 |
The Finance::Quote package does come with its own Hacker's Guide |
| 451 |
explaining how to write a Finance::Quote module. This can be |
| 452 |
found with other documentation at |
| 453 |
<http://finance-quote.sourceforge.net/documentation.html> |
| 454 |
|
| 455 |
|
| 456 |
Example program -- Stock ticker |
| 457 |
=============================== |
| 458 |
|
| 459 |
We'll finish off with an example of a useful, real-life program. |
| 460 |
In this case it's a text-based stock ticker. The code is |
| 461 |
intentionally simple to display the features of Finance::Quote, |
| 462 |
but it provides a very clear and useful display for stocks that |
| 463 |
you may have your eye on. |
| 464 |
|
| 465 |
[Example supplied in separate file] |