-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhist_data.py
380 lines (318 loc) · 13.6 KB
/
hist_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#! /usr/bin/env python
"""
:Description:
A Python 3 script to download price-volume, dividend, split data for
stocks. By default it downloads data in the following three files:
./hist_data/prc_vol_{ticker}_{start_date}_{end_date}.csv
./hist_data/div_{ticker}_{start_date}_{end_date}.csv
./hist_data/splt_{ticker}_{start_date}_{end_date}.csv
If optional filepath(s) are provided by -p, -d and -s, as shown below,
only those kind(s) of data are downloaded.
:Example:
1. Download all data (since 1900101) till today for ticker:
./hist_data.py C
2. Download all data for ticker, start_date & end_date:
./hist_data.py C 20000101 20110101
3. Download __only price-volume__ data for ticker, start_date &
end_date in the specified filenames:
./hist_data.py C 20000101 20110101 \
-p "/tmp/price_volume_{ticker}.csv"
4. Download all data for ticker, start_date & end_date in the
specified filenames:
./hist_data.py C 20000101 20110101 \
-p "/tmp/price_volume_{ticker}.csv" \
-d "/tmp/dividend_{ticker}.csv" \
-s "/tmp/split_{ticker}.csv"
:See also:
http://search.cpan.org/dist/Finance-QuoteHist/lib/Finance/QuoteHist/Yahoo.pm
http://code.activestate.com/recipes/511444-stock-prices-historical-data-bulk-download-from-in/
http://www.goldb.org/ystockquote.html
"""
__author__ = 'Gopi Goswami <[email protected]>'
__version__ = '1.0'
__all__ = ['StkDataDownload']
#import urllib.request - modified by Ravi for python 2.7
import urllib2
#from html.parser import HTMLParser
from htmllib import HTMLParser
import re
import datetime
import csv
import os
import sys
import optparse
import logging
logging.basicConfig(level = logging.INFO,
format = ('%(levelname)s %(asctime)s '
'%(filename)s:%(lineno)s %(message)s'))
# ----------------------------------------------------------------------------
def _to_date(yyyymmdd):
format = '%Y%m%d'
try:
date = datetime.datetime.strptime(yyyymmdd, format)
except:
raise Exception('Invalid date: ' + yyyymmdd)
return date
def _makedirs(dirpath):
if not os.path.exists(dirpath):
os.makedirs(dirpath)
def _abspath(path):
if path is None:
return None
return os.path.abspath(path)
# ----------------------------------------------------------------------------
_splt_field_names = [ 'Ticker', 'Date', 'Splits' ]
_splt_header = dict(zip(_splt_field_names, _splt_field_names))
_splt_row = dict.fromkeys(_splt_field_names)
_splt_writer = None
# ----------------------------------------------------------------------------
_compiled_reg_exps = \
[('date', \
re.compile(r'(?P<mon>[a-zA-Z]+)\s+(?P<dd>[0-9]+),\s+(?P<yyyy>[0-9]{4,4}).*', re.MULTILINE)), \
('dvd', \
re.compile(r'\s+\$\s+(?P<div>.*)\s+Dividend$', re.MULTILINE)), \
('splt', \
re.compile(r'(?P<denom>.*):\s+(?P<numer>.*)\s+Stock\s+Split$', re.MULTILINE)), \
]
class _Splt(HTMLParser):
def handle_data(self, data):
# Set the reg_exps
tag = self.get_starttag_text( )
if tag is None:
return None
logging.debug('tag={0}, line="{1}"'.format(tag, data))
if not tag.startswith('<td'):
return None
global _splt_row
global _splt_writer
for desc, rec in _compiled_reg_exps:
ss = rec.search(data)
if ss is None:
continue
if desc == 'date':
date = datetime.datetime.strptime(' '.join(ss.groups( )),
'%b %d %Y')
date = datetime.datetime.strftime(date, '%Y-%m-%d')
_splt_row['Date'] = date
elif desc == 'splt':
denom = float(ss.group('denom'))
if denom <= 0.0:
logging.error('Found bad split: line={0}'.format(data))
splt = (float(ss.group('numer')) /
float(ss.group('denom')))
_splt_row['Splits'] = splt
_splt_writer.writerow(_splt_row)
# ----------------------------------------------------------------------------
class StkDataDownload(object):
def __init__(self,
prc_filepath = './hist_data/prc_vol_{ticker}_{start_date}_{end_date}.csv',
dvd_filepath = './hist_data/dvd_{ticker}_{start_date}_{end_date}.csv',
splt_filepath = './hist_data/splt_{ticker}_{start_date}_{end_date}.csv',
gzip = False):
# Set the filepaths
self.prc_filepath = _abspath(prc_filepath)
self.dvd_filepath = _abspath(dvd_filepath)
self.splt_filepath = _abspath(splt_filepath)
self.gzip = gzip
# Set the urls, e.g.,
#
# http://finance.yahoo.com/d/quotes.csv?s=IBM&f=l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7
# http://finance.yahoo.com/q/hp?s=C&a=00&b=2&c=1962&d=10&e=17&f=2010&g=v&z=66&y=0
# http://finance.yahoo.com/q/hp?s=C&a=00&b=2&c=1962&d=10&e=17&f=2010&g=v&z=66&y=66
# http://finance.yahoo.com/q/hp?s=C&a=00&b=2&c=1962&d=10&e=17&f=2010&g=v&z=66&y=132
self.yahoo_ichart_url = ('http://ichart.yahoo.com/table.csv?' +
's={ticker}&' +
'a={start_mm}&' +
'b={start_dd}&' +
'c={start_yyyy}&' +
'd={end_mm}&' +
'e={end_dd}&' +
'f={end_yyyy}&' +
'g={data_type}&' +
'x=.csv')
self.yahoo_finance_url = ('http://finance.yahoo.com/q/hp?' +
's={ticker}&' +
'a={start_mm}&' +
'b={start_dd}&' +
'c={start_yyyy}&' +
'd={end_mm}&' +
'e={end_dd}&' +
'f={end_yyyy}&' +
'g=v&' +
'z=66&' +
'y={count}')
self.out_filepath = None
self.html = None
self.no_data_ids = ['There are no All Markets results for',
'Get Quotes Results for',
'Historical quote data is unavailable for the specified date range',
'Dividend data is unavailable for the specified date range',
'Sorry, Internal Server Error'
]
logging.debug(('prc_filepath={0}, '
'dvd_filepath={1}, '
'splt_filepath={2}').format(self.prc_filepath,
self.dvd_filepath,
self.splt_filepath))
def _set_out_filepath(self, filepath):
self.out_filepath = filepath.format(ticker = self.ticker,
start_date = self.start_yyyymmdd,
end_date = self.end_yyyymmdd)
_makedirs(os.path.dirname(self.out_filepath))
def _no_data(self):
logging.info(('No data available for: '
'ticker={0}, '
'start_date={1}, '
'end_date={2}').format(self.ticker,
self.start_yyyymmdd,
self.end_yyyymmdd))
return False
def _set_url(self,
url,
data_type,
count = 0):
url = url.format(ticker = self.ticker,
#start_mm = self.start_date.month,
start_mm = self.start_date.month - 1,
start_dd = self.start_date.day,
start_yyyy = self.start_date.year,
#end_mm = self.end_date.month,
end_mm = self.end_date.month -1,
end_dd = self.end_date.day,
end_yyyy = self.end_date.year,
data_type = data_type,
count = count)
logging.info('The download url="{0}"'.format(url))
try:
# sock = urllib.request.urlopen(url) # modified by Ravi
sock = urllib2.urlopen(url)
#except urllib.error.HTTPError:
except urllib2.HTTPError:
return self._no_data( )
#except urllib.error.URLError:
except urllib2.URLError:
return self._no_data( )
else:
self.html = sock.read( ).decode('utf-8')
for check in self.no_data_ids:
if self.html.find(check) >= 0:
return self._no_data( )
return True
def _gzip(self, path):
cmd = 'gzip -f {0}'.format(path)
logging.info('Running: cmd="{0}"'.format(cmd))
os.system(cmd)
def _run_xxx(self, filepath, data_type):
self._set_out_filepath(filepath)
if not self._set_url(self.yahoo_ichart_url, data_type):
return False
with open(self.out_filepath, 'w') as outfile:
for count, line in enumerate(self.html.split('\n')):
if not line.strip( ):
continue
if count == 0:
tt = 'Ticker'
else:
tt = self.ticker
outfile.writelines(tt + ',' + line + '\n')
logging.info('Download path={0}'.format(self.out_filepath))
if self.gzip:
self._gzip(self.out_filepath)
return True
def _run_splt(self):
self._set_out_filepath(self.splt_filepath)
global _splt_writer
with open(self.out_filepath, 'w') as outfile:
_splt_writer = csv.DictWriter(outfile, _splt_field_names)
_splt_writer.writerow(_splt_header)
_splt_row['Ticker'] = self.ticker
_splt = _Splt( )
count = 0
while True:
if not self._set_url(self.yahoo_finance_url, 'v', count):
break
_splt.feed(self.html)
count += 66
if count >= 1000:
# count too big, probably getting errors anyway
break
logging.info('Download path={0}'.format(self.out_filepath))
if self.gzip:
self._gzip(self.out_filepath)
return True
def run(self,
ticker,
start_date,
end_date):
self.ticker = ticker
self.start_yyyymmdd = start_date
self.start_date = _to_date(start_date)
self.end_date = _to_date(end_date)
self.end_yyyymmdd = end_date
logging.info(('ticker={0}, '
'start_date={1}, '
'end_date={2}').format(self.ticker,
self.start_yyyymmdd,
self.end_yyyymmdd))
if self.prc_filepath is not None:
if not self._run_xxx(self.prc_filepath, 's'):
return False
if self.dvd_filepath is not None:
if not self._run_xxx(self.dvd_filepath, 'v'):
return False
if self.splt_filepath is not None:
if not self._run_splt( ):
return False
# ----------------------------------------------------------------------------
def _main( ):
parser = optparse.OptionParser(__doc__)
parser.add_option('-p',
'--prc_filepath',
dest = 'prc_filepath',
help = 'Download filepath for price volume data',
default = None)
parser.add_option('-d',
'--dvd_filepath',
dest = 'dvd_filepath',
help = 'Download filepath for dividend data',
default = None)
parser.add_option('-s',
'--splt_filepath',
dest = 'splt_filepath',
help = 'Download filepath for split data',
default = None)
parser.add_option('-q',
'--quiet',
dest = 'quiet',
help = 'Not verbose',
action = 'store_true',
default = False)
options, args = parser.parse_args( )
num_args = len(args)
if num_args == 1:
ticker = args[0]
#start_date = '19000101'
start_date = '20000101'
end_date = datetime.datetime.strftime(datetime.datetime.now( ),
'%Y%m%d')
elif num_args == 3:
ticker, start_date, end_date = tuple(args)
else:
parser.error('Failed to parse args; see usage: '
'num_args={0}'.format(num_args))
if options.quiet:
logging.getLogger('').setLevel(logging.CRITICAL)
if options.prc_filepath is None and \
options.dvd_filepath is None and \
options.splt_filepath is None:
# Default: all of the following are None, so download all
# data
stk = StkDataDownload( )
else:
stk = StkDataDownload(options.prc_filepath,
options.dvd_filepath,
options.splt_filepath)
stk.run(ticker, start_date, end_date)
# ----------------------------------------------------------------------------
if __name__ == '__main__':
_main( )