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:
-
$ market
-
Name Price Change Pct Day Lo Day Hi
-
Dow Jones Industr 9258.10 -189.01 -2.00% 9194.78 9628.07
-
NASDAQ COMPOSITE 1740.33 -14.55 -0.83% 1706.86 1806.89
-
S&P 500 INDEX,RTH 984.94 -11.29 -1.13% 970.97 1021.06
or
-
$ market GE XOM
-
Name Price Change Pct Day Lo Day Hi
-
Dow Jones Industr 9258.10 -189.01 -2.00% 9194.78 9628.07
-
NASDAQ COMPOSITE 1740.33 -14.55 -0.83% 1706.86 1806.89
-
S&P 500 INDEX,RTH 984.94 -11.29 -1.13% 970.97 1021.06
-
GEN ELECTRIC CO 20.65 0.35 +1.72% 19.90 21.99
-
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.
-
function market {
-
stocks="^DJI+^IXIC+^GSPC"
-
-
for arg in "$@"
-
do
-
stocks+="+${arg}"
-
done
-
-
# Note the sed commands below do the following:
-
# 1. Strip any html
-
# 2. Replace the '","' delimeter with a #
-
# 3. Replace the ' – ' delimeter with a #
-
# 4. Strip the leading and trailing " characters
-
-
# The awk command does some pretty printing on each record with # used for a delimeter
-
-
curl -s "http://download.finance.yahoo.com/d/quotes.csv?s=$stocks&f=nk1k2m2" | \
-
sed 's/< [^>]*>//g' | \
-
sed 's/","/#/g' | \
-
sed 's/ – /#/g' | \
-
sed 's/"\(.*\)"/\1/' | \
-
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) }'
-
-
}
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.
