I wrote a simple script to use with Conky for displaying stock quotes (or any thing you can have a symbol to refer to) and charts from Yahoo! Finance . I got to mention one thing, normally Google has good (just good, not great) support for developers to retrieve data from its products, but for financial information, it isnt so convenient to share with us. At least, the (nearly real-time) quotes part. Yahoo provides a CSV example and you can choose what data you want, just edit the link with options you need.
A sample result looked like:
Lets take a look of this script, stock-conky.sh:
#!/bin/bash # stock-conky.sh by livibetter 2010-08-17 # Usage: stock-conky.sh <chart_small|chart_large|header|quote> [ticker symbol] [x y] case "$1" in chart_small) wget -q "http://ichart.finance.yahoo.com/t?s=$2&lang=en-US®ion=US" -O "/tmp/yahoo.finance.$2.png" echo "\${image /tmp/yahoo.finance.$2.png -p $3,$4 -s 192x96 -f 60}" ;; chart_large) wget -q "http://ichart.finance.yahoo.com/b?s=$2&lang=en-US®ion=US" -O "/tmp/yahoo.finance.$2.png" echo "\${image /tmp/yahoo.finance.$2.png -p $3,$4 -s 512x288 -f 60}" ;; header) echo 'Symbol Last Change Open High Low Volume Date Time' ;; quote) wget -q "http://download.finance.yahoo.com/d/quotes.csv?s=$2&f=sl1d1t1c1ohgvp2&e=.csv" -O - | awk -F "\"*,\"*" '{ ticker = substr(substr($1, 2, length($1) - 1), 1, 8); printf("%-8s %7.2f ", ticker, $2); if ($5 > 0) printf("${color green}") else if ($5 < 0) printf("${color red}") else printf("${color black}"); if ($5 == "N/A") printf(" ${color}N/A ( N/A) ") else printf("%7.2f (%5.2f%%)$color ", $5, $10) if ($6 == "N/A") printf(" N/A ") else printf("%7.2f ", $6); if ($7 == "N/A") printf(" N/A ") else printf("%7.2f ", $7); if ($8 == "N/A") printf(" N/A ") else printf("%7.2f ", $8); if ($9 == "N/A") printf("%10s ", "N/A") else printf("%10d ", $9); printf("%10s %7s", $3, $4); printf("\n"); }' ;; esac
It should be called like:
$ stock-conky.sh <chart_small|chart_large|header|quote> [ticker symbol] [x y]
First argument is the type, you can have chart_small for small chart (192x96), chart_large for larger chart (512x288), header for header title, and quote for the quotes. The second argument is the tickers symbol. Third and fourth arguments are the position of chart image in your Conky output. The ticker symbol maximal length is 8 characters, it should be enough. Change is rendered with color, its easy to read. I found that some field could be N/A, so it has to be dealt with or awk would give out 0.00, thats not the correct value.
This script downloads chart images to /tmp/yahoo.finance.<ticker symbol>.png, then it outputs Conky TEXT for being parsed. For quote, it uses awk to layout the quotes. For both types, you should run it using ${execpi 60 stock-conky.sh arguments...}. Parse and run with one minute interval, there is no need to run it more frequent. Here is a sample configuration:
# by livibetter 2010-08-17 TEXT ${voffset 288}${execpi 60 stock-conky.sh header} ${execpi 60 stock-conky.sh quote GOOG} ${execpi 60 stock-conky.sh quote YHOO} ${execpi 60 stock-conky.sh quote MSFT} ${execpi 60 stock-conky.sh quote CADUSD=X} ${execpi 60 stock-conky.sh chart_large GOOG 0 0}${execpi 60 stock-conky.sh chart_small YHOO 520 0}${execpi 60 stock-conky.sh chart_small MSFT 520 104}
The first voffset is to move the starting text below chart images, the chart images are pulled at the end of configuration because image in Conky is not being arranged as text is. You will need to tell Conky where you want to put this image. So, we just put those command at the end.
Hope this would help you keep an eye on your investments on your desktop!
Hey there!
ReplyDeleteThank you so much for this! Ive been looking for something to deal with my stocks and conky. There are plenty other variations of such out there on the net, but this one fits my needs just perfectly.
Theres one little thing Id like to ask for though.
What would I need to modify in the code to get the share prices with not just 2 decimals, but with four? Im also dealing with penny stocks and it would be nice to see the actual 0.0004 than just 0.00 for example.
Could you help me out with that? Thank you so much in advance!!!! Ill be checking back here frequently.
You can change "5.2" or "7.2" to "5.4" and "7.4" then you will have 4 decimal places. However, the source only provides 2 decimal places except Change. (I guess thats why I use ".2" at first place)
ReplyDeleteI dont know if there is any way I can make Yahoo to supply more precise data. I am not going to find the answer, because I am no longer using Conky. If you find any solution or other good sources (via API or feed or CSV, not scrapping), I may help you with those.
Thank you very much for the quick response.
ReplyDeleteSimply changing those .2 to .4 digits did it like you suggested. I can finally see the prices for all my penny stocks. Thats all I really needed! Thanks again so much!
About getting more detailed info from yahoo, I am already totally amazed at what you cooked up with your work and I dont think Ill be of any help to you at this point. My bad. If youre not using Conky anymore, may I ask what type of system monitoring tool youre currently using?
I am using dzen with C + Bash (written when I still used Bash as main code. I later ported the main Bash to C code. You may want to check out all posts labeled with dzen.
ReplyDelete