<?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>thirstymind.org &#187; stocks</title>
	<atom:link href="http://www.thirstymind.org/tag/stocks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thirstymind.org</link>
	<description>andrew watts' weblog</description>
	<lastBuildDate>Sat, 17 Oct 2009 22:11:16 +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>Checking the Stock Market from the Command Line</title>
		<link>http://www.thirstymind.org/2008/10/08/checking-the-stock-market-from-the-command-line/</link>
		<comments>http://www.thirstymind.org/2008/10/08/checking-the-stock-market-from-the-command-line/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 01:51:57 +0000</pubDate>
		<dc:creator>Andrew Watts</dc:creator>
				<category><![CDATA[how to]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[market]]></category>
		<category><![CDATA[stocks]]></category>

		<guid isPermaLink="false">http://www.thirstymind.org/?p=402</guid>
		<description><![CDATA[With all the financial trouble in the country I have been checking the market a lot lately, and I find it a little annoying to constantly switch to my web browser and refresh or check my dashboard or whatever.  Since I spend a lot of time in a terminal, I put a function together, [...]]]></description>
			<content:encoded><![CDATA[<p>With all the financial trouble in the country I have been checking the market a lot lately, and I find it a little annoying to constantly switch to my web browser and refresh or check my dashboard or whatever.  Since I spend a lot of time in a terminal, I put a function together, which will always display the latest market conditions for the Dow Jones Industrial Average, the Nasdaq and the S&#038;P 500 as well as any stocks you feed the function.</p>
<p>For example:</p>
<div class="geshi no term">
<ol>
<li class="li1">
<div class="de1">$ market
</div>
</li>
<li class="li1">
<div class="de1">Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Price &nbsp; &nbsp; Change &nbsp; Pct &nbsp; &nbsp; Day Lo &nbsp; &nbsp;Day Hi &nbsp;
</div>
</li>
<li class="li1">
<div class="de1">Dow Jones Industr &nbsp; &nbsp; &nbsp;9258.10 &nbsp;-189.01 &nbsp;-2.00% &nbsp; 9194.78 &nbsp; 9628.07
</div>
</li>
<li class="li1">
<div class="de1">NASDAQ COMPOSITE &nbsp; &nbsp; &nbsp; 1740.33 &nbsp; -14.55 &nbsp;-0.83% &nbsp; 1706.86 &nbsp; 1806.89
</div>
</li>
<li class="li1">
<div class="de1">S&amp;P 500 INDEX,RTH &nbsp; &nbsp; &nbsp; 984.94 &nbsp; -11.29 &nbsp;-1.13% &nbsp; &nbsp;970.97 &nbsp; 1021.06</div>
</li>
</ol>
</div>
<p>or</p>
<div class="geshi no term">
<ol>
<li class="li1">
<div class="de1">$ market GE XOM
</div>
</li>
<li class="li1">
<div class="de1">Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Price &nbsp; &nbsp; Change &nbsp; Pct &nbsp; &nbsp; Day Lo &nbsp; &nbsp;Day Hi &nbsp;
</div>
</li>
<li class="li1">
<div class="de1">Dow Jones Industr &nbsp; &nbsp; &nbsp;9258.10 &nbsp;-189.01 &nbsp;-2.00% &nbsp; 9194.78 &nbsp; 9628.07
</div>
</li>
<li class="li1">
<div class="de1">NASDAQ COMPOSITE &nbsp; &nbsp; &nbsp; 1740.33 &nbsp; -14.55 &nbsp;-0.83% &nbsp; 1706.86 &nbsp; 1806.89
</div>
</li>
<li class="li1">
<div class="de1">S&amp;P 500 INDEX,RTH &nbsp; &nbsp; &nbsp; 984.94 &nbsp; -11.29 &nbsp;-1.13% &nbsp; &nbsp;970.97 &nbsp; 1021.06
</div>
</li>
<li class="li1">
<div class="de1">GEN ELECTRIC CO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;20.65 &nbsp; &nbsp; 0.35 &nbsp;+1.72% &nbsp; &nbsp; 19.90 &nbsp; &nbsp; 21.99
</div>
</li>
<li class="li1">
<div class="de1">EXXON MOBIL CP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 77.00 &nbsp; &nbsp; 0.93 &nbsp;+1.22% &nbsp; &nbsp; 74.00 &nbsp; &nbsp; 79.39</div>
</li>
</ol>
</div>
<p>The key to making this work is a csv generated by a http request to Yahoo.  The request takes 2 arguments, the stocks and a string of tags.</p>
<p>For Example:<br />
<a href="http://download.finance.yahoo.com/d/quotes.csv?s=^DJI+GE&#038;f=nk1k2m2">http://download.finance.yahoo.com/d/quotes.csv?s=^DJI+GE&#038;f=nk1k2m2</a></p>
<p>Which says grab the name, last trade price, change percent, and the days range (all real time) for all the stocks defined by s.  There is a whole slew of tags that you can grab, the best resource of this information that I found is located at <a href="http://www.gummy-stuff.org/Yahoo-data.htm">Gummy Stuff&#8217;s Yahoo Data page</a>.</p>
<p>After some magic with sed and awk you get the above result.</p>
<p>And finally Here is the code, if anyone wants to add it to their bash profile.</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">function</span> market <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re2">stocks=</span><span class="st0">&quot;^DJI+^IXIC+^GSPC&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span> arg <span class="kw1">in</span> <span class="st0">&quot;$@&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">do</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; stocks+=<span class="st0">&quot;+${arg}&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">done</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># Note the sed commands below do the following:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># &nbsp; 1. Strip any html</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># &nbsp; 2. Replace the &#39;&quot;,&quot;&#39; delimeter with a #</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># &nbsp; 3. Replace the &#39; &#8211; &#39; delimeter with a #</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># &nbsp; 4. Strip the leading and trailing &quot; characters</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co0"># The awk command does some pretty printing on each record with # used for a delimeter</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; curl -s <span class="st0">&quot;http://download.finance.yahoo.com/d/quotes.csv?s=$stocks&amp;f=nk1k2m2&quot;</span> <span class="sy0">|</span> \</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">sed</span> <span class="st0">&#39;s/&lt; [^&gt;]*&gt;//g&#39;</span> <span class="sy0">|</span> \</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">sed</span> <span class="st0">&#39;s/&quot;,&quot;/#/g&#39;</span> <span class="sy0">|</span> \</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">sed</span> <span class="st0">&#39;s/ &#8211; /#/g&#39;</span> <span class="sy0">|</span> \</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">sed</span> <span class="st0">&#39;s/&quot;<span class="es0">\(</span>.*<span class="es0">\)</span>&quot;/<span class="es0">\1</span>/&#39;</span> <span class="sy0">|</span> \</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">awk</span> <span class="st0">&#39;BEGIN{FS=&quot;#&quot;; printf(&quot;%-20s &nbsp;%-8s &nbsp;%-7s &nbsp;%-6s &nbsp;%-8s &nbsp;%-9s<span class="es0">\n</span>&quot;, &quot;Name&quot;, &quot;Price&quot;, &quot;Change&quot;, &quot;Pct&quot;, &quot;Day Lo&quot;, &quot;Day Hi&quot;)} { printf(&quot;%-20s &nbsp;%8s &nbsp;%7s &nbsp;%6s &nbsp;%8s &nbsp;%9s<span class="es0">\n</span>&quot;, $1, $3, $4, $5, $6, $7) }&#39;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>I think some of the sed commands could be improved to reduce the number of them, but this is simple and works.  So I&#8217;m happy for now, or would be if I started see some numbers from the economy that looked better.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thirstymind.org/2008/10/08/checking-the-stock-market-from-the-command-line/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
