Checking the Stock Market from the Command Line

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&P 500 as well as any stocks you feed the function.

For example:

  1. $ market
  2. Name                  Price     Change   Pct     Day Lo    Day Hi  
  3. Dow Jones Industr      9258.10  -189.01  -2.00%   9194.78   9628.07
  4. NASDAQ COMPOSITE       1740.33   -14.55  -0.83%   1706.86   1806.89
  5. S&P 500 INDEX,RTH       984.94   -11.29  -1.13%    970.97   1021.06

or

  1. $ market GE XOM
  2. Name                  Price     Change   Pct     Day Lo    Day Hi  
  3. Dow Jones Industr      9258.10  -189.01  -2.00%   9194.78   9628.07
  4. NASDAQ COMPOSITE       1740.33   -14.55  -0.83%   1706.86   1806.89
  5. S&P 500 INDEX,RTH       984.94   -11.29  -1.13%    970.97   1021.06
  6. GEN ELECTRIC CO          20.65     0.35  +1.72%     19.90     21.99
  7. EXXON MOBIL CP           77.00     0.93  +1.22%     74.00     79.39

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.

For Example:
http://download.finance.yahoo.com/d/quotes.csv?s=^DJI+GE&f=nk1k2m2

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 Gummy Stuff’s Yahoo Data page.

After some magic with sed and awk you get the above result.

And finally Here is the code, if anyone wants to add it to their bash profile.

  1. function market {
  2.     stocks="^DJI+^IXIC+^GSPC"
  3.  
  4.     for arg in "$@"
  5.     do
  6.         stocks+="+${arg}"
  7.     done
  8.  
  9.     # Note the sed commands below do the following:
  10.     #   1. Strip any html
  11.     #   2. Replace the '","' delimeter with a #
  12.     #   3. Replace the ' – ' delimeter with a #
  13.     #   4. Strip the leading and trailing " characters
  14.  
  15.     # The awk command does some pretty printing on each record with # used for a delimeter
  16.  
  17.     curl -s "http://download.finance.yahoo.com/d/quotes.csv?s=$stocks&f=nk1k2m2" | \
  18.     sed 's/< [^>]*>//g' | \
  19.     sed 's/","/#/g' | \
  20.     sed 's/ – /#/g' | \
  21.     sed 's/"\(.*\)"/\1/' | \
  22.     awk 'BEGIN{FS="#"; printf("%-20s  %-8s  %-7s  %-6s  %-8s  %-9s\n", "Name", "Price", "Change", "Pct", "Day Lo", "Day Hi")} { printf("%-20s  %8s  %7s  %6s  %8s  %9s\n", $1, $3, $4, $5, $6, $7) }'
  23.  
  24. }

I think some of the sed commands could be improved to reduce the number of them, but this is simple and works. So I’m happy for now, or would be if I started see some numbers from the economy that looked better.

  • I didn't write in python, because bash seemed better for the job. Personally, I like the first sed line the best, who knew stripping html could be so simple!
  • tmarthal
    Why didn't you write this in python? urllib == curl++?

    however, that awk line is pretty awesome
blog comments powered by Disqus