2525from pylab import gca
2626
2727
28+
29+ def parse_yahoo_historical (fh ):
30+ """
31+ Parse the historical data in file handle fh from yahoo finance and return
32+ results as a list of
33+
34+ d, open, close, high, low, volume
35+
36+ where d is a floating poing representation of date, as returned by date2num
37+
38+ """
39+ results = []
40+
41+
42+
43+ lines = fh .readlines ()
44+ for line in lines [1 :]:
45+
46+ vals = line .split (',' )
47+ if len (vals )!= 7 : continue
48+ datestr = vals [0 ]
49+ dt = datetime .date (* time .strptime (datestr , '%d-%b-%y' )[:3 ])
50+ d = date2num (dt )
51+ open , high , low , close = [float (val ) for val in vals [1 :5 ]]
52+ volume = int (vals [5 ])
53+
54+ results .append ((d , open , close , high , low , volume ))
55+ results .reverse ()
56+ return results
57+
2858def quotes_historical_yahoo (ticker , date1 , date2 ):
2959
3060 """
@@ -49,26 +79,12 @@ def quotes_historical_yahoo(ticker, date1, date2):
4979
5080 ticker = ticker .upper ()
5181
52- results = []
53- try :
54- lines = urlopen (url ).readlines ()
82+ try : ret = parse_yahoo_historical (urlopen (url ))
5583 except IOError , exc :
5684 warnings .warn ('urlopen() failure\n ' + url + '\n ' + exc .strerror [1 ])
5785 return None
58- for line in lines [1 :]:
59-
60- vals = line .split (',' )
61- if len (vals )!= 7 : continue
62- datestr = vals [0 ]
63- dt = datetime .date (* time .strptime (datestr , '%d-%b-%y' )[:3 ])
64- d = date2num (dt )
65- open , high , low , close = [float (val ) for val in vals [1 :5 ]]
66- volume = int (vals [5 ])
67-
68- results .append ((d , open , close , high , low , volume ))
69- results .reverse ()
70- return results
7186
87+ return ret
7288
7389
7490def plot_day_summary (ax , quotes , ticksize = 3 ,
0 commit comments