fun with stock quotes
Robin Becker
robin at jessikat.demon.co.uk
Sat Jan 1 01:08:55 EST 2000
In article <Fnn296.A3w at world.std.com>, Will Ware <wware at world.std.com> writes
>I recently discovered that Yahoo's stock-quote-serving web
>page is now offering historical stock data in machine-friendly
>form. The following scripts will pull in a bunch of stock data
>and format it as a C module. Then you can back-test any silly
>investment algorithms you come up with. Nerdy greedy fun for
>the whole family.
>
here's my take on that which just returns a class with all the values
######################################################
import urllib, string, normalDate, math, regex, sys, os
class _YahooData:
def __init__(self,ticker,start,finish,Open,Low,High,Close,Volume):
self.ticker=ticker
self.start=start
self.finish=finish
self.Open=Open
self.Low=Low
self.High=High
self.Close=Close
self.Volume=Volume
def fetchRangeAsCSV(ticker, first, last):
'''go to Yahoo and fetch a range of historical stock
data, where 'first' and 'last' are normalDates'''
key = (first, last)
url = 'http://chart.yahoo.com/table.csv?s=%s' \
'&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=d&q=q&y=0&z=%s' \
'&x=.csv' % (ticker,
first.month(), first.day(), first.year(),
last.month(), last.day(), last.year(),
ticker)
return urllib.URLopener().open(url).readlines()
def _yahoo_to_normalDate(date):
date = string.split(date, '-')
date[0] = eval(date[0])
date[2] = eval(date[2])
if date[2] > 90: year = date[2] + 1900
else: year = date[2] + 2000
month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep',
'Oct','Nov','Dec'].index(date[1]) + 1
return normalDate.ND(year * 10000 + month * 100 + date[0])
def fetchRange(ticker, first, last):
Open = []
Low = []
High = []
Close = []
Volume = []
finish=None
for x in fetchRangeAsCSV(ticker,first,last)[1:]:
F = string.split(x, ',')
Open.append(eval(F[1]))
Low.append(eval(F[2]))
High.append(eval(F[3]))
Close.append(eval(F[4]))
Volume.append(eval(F[5]))
if finish is None:
finish=_yahoo_to_normalDate(F[0])
start = _yahoo_to_normalDate(F[0])
# Yahoo gives reverse time order
Open.reverse()
Low.reverse()
High.reverse()
Close.reverse()
Volume.reverse()
if len(data) == 0:
raise 'ouch'
return _YahooData(ticker,start,finish,Open,Low,High,Close,Volume)
if __name__=='__main__':
# just do a quick test for now
masterList = [
# tech stocks
'LNUX', 'MSFT', 'AMZN', 'INTC', 'RHAT', 'DELL', 'GTW',
'CSCO', 'AAPL', 'AOL', 'COMS', 'CPQ', 'LCOS', 'LU',
'ORCL', 'SUNW', 'TXN', 'YHOO',
# single-letters and blue chips
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'J', 'K', 'L', 'N', 'O', 'P', 'R',
'U', 'W', 'X', 'Y', 'Z',
# Dow Jones stocks
'AA', 'AXP', 'T', 'BA', 'CAT', 'CHV', 'KO',
'DIS', 'DD', 'EK', 'GE', 'GM', 'GT', 'HWP',
'IBM', 'IP', 'JNJ', 'MCD', 'MRK', 'MMM', 'JPM',
'MO', 'PG', 'S', 'TRV', 'UK', 'WMT',
# biotech
'AFFX', 'BIOM', 'CRA', 'ENMD', 'HGSI',
'MATX', 'MLNM', 'RGEN', 'SAFS', 'DNA', 'GENE',
'INCY', 'GLGC', 'PFE',
'AVXT', 'CTIC', 'CRXA', 'GZMO', 'IDPH',
'ILXO', 'ISIP', 'RZYM', 'SUPG', 'TGEN', 'VICL',
# retailers, general interest
'BBY', 'COST', 'UPS', 'HD',
]
masterList = masterList[:12]
start = normalDate.ND(19950101)
finish = normalDate.ND() # today
d = {}
for t in masterList:
d[t]=fetchRange(t, start, finish)
print "data for %s obtained start=%d(%f) finish=%d(%f)" % \
(t,d[t].start,d[t].Close[0],d[t].finish,d[t].Close[-1])
--
Robin Becker
More information about the Python-list
mailing list