<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dabbler &#187; exchange</title>
	<atom:link href="http://sethjust.com/tag/exchange/feed/" rel="self" type="application/rss+xml" />
	<link>http://sethjust.com</link>
	<description>If it ain&#039;t broke, fix it!</description>
	<lastBuildDate>Thu, 31 Dec 2009 21:40:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fast and Simple Stock Quotes Using Perl</title>
		<link>http://sethjust.com/2008/10/23/fast-and-simple-stock-quotes-using-perl/</link>
		<comments>http://sethjust.com/2008/10/23/fast-and-simple-stock-quotes-using-perl/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 06:18:05 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[CPAN]]></category>
		<category><![CDATA[exchange]]></category>
		<category><![CDATA[finance]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[stocks]]></category>
		<category><![CDATA[ticker]]></category>

		<guid isPermaLink="false">http://sethjust.wordpress.com/?p=21</guid>
		<description><![CDATA[One of the things that makes perl so powerful and fascinating is the huge number of modules that are available online, especially through the CPAN repository. Today I stumbled upon one called Finance::Quote, which does one thing, very simply: it retrieves stock (or mutual fund) quotes. You feed it a ticker symbol and it gives [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that makes perl so powerful and fascinating is the huge number of modules that are available online, especially through the CPAN repository. Today I stumbled upon one called Finance::Quote, which does one thing, very simply: it retrieves stock (or mutual fund) quotes. You feed it a ticker symbol and it gives back a hash with all sorts of information, but most importantly, the price. I&#8217;m going to show how to use this to create a command line tool that will grab an up-to-the minute stock quote for any ticker symbol you give it.</p>
<p><span id="more-21"></span><br />
To start, installing Finance::Quote is a breeze. As long as you have CPAN installed, just type at the command line:</p>
<pre>$perl -MCPAN -e shell
&gt;install Finance::Quote</pre>
<p>From there it&#8217;s easy to get start, it&#8217;s just a matter of importing the module and creating and instance. This means the beginning of our script will look like:<br />
[sourcecode language='php']#!/usr/bin/perl -w<br />
use strict;</p>
<p># import module<br />
use Finance::Quote;<br />
# create object<br />
my $q = Finance::Quote->new();[/sourcecode]<br />
The instance that we created has one main method: fetch(). It takes a list of arguments, with the first being a the exchange to look for quotes on, and the remaining being a list or array of ticker symbols. The <a href="http://search.cpan.org/~ecocode/Finance-Quote-1.14/lib/Finance/Quote.pm">Finance::Quote documentation</a> provides the following list of valid exchanges:</p>
<pre>australia           Australan Stock Exchange
dwsfunds            Deutsche Bank Gruppe funds
fidelity            Fidelity Investments
tiaacref            TIAA-CREF
troweprice          T. Rowe Price
europe              European Markets
canada              Canadian Markets
usa                 USA Markets
nyse                New York Stock Exchange
nasdaq              NASDAQ
uk_unit_trusts      UK Unit Trusts
vanguard            Vanguard Investments
vwd                 Vereinigte Wirtschaftsdienste GmbH</pre>
<p>For the purposes of this script, I&#8217;m going to stick to &#8220;usa&#8221; because it covers both NYSE and NASDAQ, and those are the stocks I&#8217;m interested in.<br />
Fetch() returns a two-dimensional hash of results. The first index (or dimension) is the ticker symbol of the stock and the second is the label for the specific piece of information. For now, the labels that we&#8217;re interested are the name of the company (&#8216;name&#8217;) and the price (&#8216;price&#8217;). A full listing of labels is available in the <a href="http://search.cpan.org/~ecocode/Finance-Quote-1.14/lib/Finance/Quote.pm#LABELS">Finance::Quote documentation</a>.<br />
Using this information we can now build a simple script to fetch a stock quote:<br />
[sourcecode language='php']#!/usr/bin/perl -w<br />
use strict;</p>
<p># import module<br />
use Finance::Quote;</p>
<p># create object<br />
my $q = Finance::Quote->new();</p>
<p># retrieve stock quote<br />
my %data = $q->fetch(&#8216;usa&#8217;, &#8216;GOOG&#8217;);</p>
<p># print price<br />
print $data{&#8216;GOOG&#8217;, &#8216;price&#8217;} . &#8220;n&#8221;;[/sourcecode]<br />
This script, although it does what it&#8217;s supposed to, isn&#8217;t very pretty. By adding a little code to read ticker symbols from the command line we can check on multiple stocks at the same time, as well as remove the need to hard-code the ticker symbols. We can also make it produce much prettier results by adding more information about the company and add some basic error checking, which yields the following script:<br />
[sourcecode language='php']#!/usr/bin/perl -w<br />
use strict;</p>
<p># import module<br />
use Finance::Quote;</p>
<p># create object<br />
my $q = Finance::Quote->new();</p>
<p>#print usage information<br />
if (length(@ARGV) == 0) {<br />
	print &#8220;Usage: $0 ticker1, ticker2, &#8230; , tickerNn&#8221;;<br />
	exit;<br />
}</p>
<p># get stock symbols from the command line and<br />
# format them correctly (uppercase)<br />
for (@ARGV){<br />
	$_ = uc();<br />
}</p>
<p># retrieve stock quote<br />
my %data = $q->fetch(&#8216;usa&#8217;, @ARGV);</p>
<p># print result for each stock<br />
for (@ARGV){<br />
	if ($data{$_, &#8217;success&#8217;}) {		# if getting the quote succeeded<br />
		my $name = $data{$_, &#8216;name&#8217;};	# build a report<br />
		my $price = $data{$_, &#8216;price&#8217;};<br />
		my $message = &#8221;;<br />
		$message .= $name . &#8216; (&#8216; . $_ . &#8216;)&#8217;;<br />
		$message .= &#8216; &#8216; x(25 &#8211; length($message));<br />
		$message .= &#8220;$$pricen&#8221;;<br />
		print $message;<br />
	}<br />
	else { print &#8220;Failed to retrieve quote for $_: $data{$_, &#8216;errormsg&#8217;}n&#8221;; }<br />
}[/sourcecode]<br />
This is very simple, but it does its job well:</p>
<pre>$ quote goog msft aapl dell java
GOOGLE (GOOG)            $352.32
MICROSOFT CP (MSFT)      $22.32
APPLE INC (AAPL)         $98.23
DELL INC (DELL)          $11.99
SUN MICROSYSTEMS (JAVA)  $4.54</pre>
<p><a href="http://people.reed.edu/~justs/quote.pl">Download the script here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2008/10/23/fast-and-simple-stock-quotes-using-perl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
