My latest polyglot excursion has landed my in the world of snakes. I've started playing around with Python a few weeks ago and have to report that I'm very pleased with how well this programming language performs. I decided to take it out for a little run on Yahoo stock data and came up with a little script that returns the current state of your stock in relation to the current day's trading range.
To run this script from command line, you first need to give it execution permission. This is time to call the oft-run bash command:
$ chmod +x yahoo.py
Of course I'm getting a little ahead of things. We haven't defined the yahoo.py program. It utilizes three modules: re (regex), sys (defining argv) and urllib (for opening an url). These modules provide the functions we need. Some more preliminaries. We start our program with the shebang line so the chmod invocation actually works. Next, I have used a main() function here when you really don't need it in its current implementation. The last two lines make reference to the main() function sentinel but it's virtually useless for our purposes. The reason to include it is if we decide to grow this program to do other stuff and import it into another script at some later time. Okay, here it is.
#!/usr/bin/python
import re
import urllib
import sys
site = "http://finance.yahoo.com/d/quotes.csv?s="
sym = sys.argv[1]
h = urllib.urlopen(site + sym + "&f=shgl1")
j = h.read()
h.close()
k = re.findall('(\w+)",(\d+.\d\d),(\d+.\d\d),(\d+.\d\d)',j)
stock = k[0][0]
hi = float(k[0][1])
lo = float(k[0][2])
last = float(k[0][3])
z = (last-lo)/(hi-lo)
def main():
if z >= 0.5:
print "%s is trading in the top half of it's range" % stock
else:
print "%s is trading in the bottom half of it's range" % stock
if __name__ == '__main__':
main()
There really isn't much to it, as you can plainly see. I've used the main() function to print two options based on the trading price of your stock. The way to input the stock you're interested in is to pass it as a command-line argument like this:
$ ./yahoo.py SPY
The sys.argv[1] is the first argument from command-line (that's not totally true, but this isn't a computer science course either) and that's all this little script can currently handle. The url address is a little cryptic, so I've chunked it out into three sections. First the http:// part, which is always the same. Then the stock you entered in command-line. And finally a bizarre looking string that has Yahoo values that you're interested in. In our case, we want the stock name, high, low and last price. This gets converted into a string that our regex engine begins to work on. It returns a list of tuples (or single tuple in our case), where we can subset the values we're interested in.
One can see how this program can easily be expanded to include much more information. You could also offer the option to pass flags to the script which tells it what path you want to take to get your desired result. I'm thinking it may be useful to pass a set of stocks to the script and have it tell what percent of the group is doing well or not that day.
This program is obviously a toy program as written, but it's a good beginning for something a little more useful. I'm setting up camp in this snake pit to check it out. I'm not scared of snakes. At least not yet.
Good idea.
ReplyDeleteI still like "simpler is better." I write scripts in Bourne unless there's a good reason not to.
11:45pm [~] 13132[0]> cat bin/toprange.sh
#! /bin/sh
SYM=$( echo "$1" | tr '[:lower:]' '[:upper:]' )
OUT=$( lynx -dump "http://finance.yahoo.com/d/quotes.csv?s=$1&f=shgl1" |
tr -d . )
HI=$( echo "$OUT" | awk -F, '{print $2}' )
LO=$( echo "$OUT" | awk -F, '{print $3}' )
LA=$( echo "$OUT" | awk -F, '{print $4}' )
MI=$(( ($HI + $LO) / 2 ))
[ $LA -ge $MI ] && NOT="" || NOT="not"
echo $SYM is $NOT in top half of the intraday range.
Nice, thanks for the code. Now I just need a Ruby and Perl version.
ReplyDeleteHmm: if ...bottom else .. bottom
ReplyDeleteThanks Anon, no wonder I've been so bearish lately.
ReplyDelete