forked from mementum/backtrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyahoodownload.py
executable file
·243 lines (193 loc) · 7.24 KB
/
yahoodownload.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
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015, 2016 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
import collections
import datetime
import io
import logging
import sys
PY2 = sys.version_info.major == 2
if PY2:
from urllib2 import urlopen
from urllib import quote as urlquote
else:
from urllib.request import urlopen
from urllib.parse import quote as urlquote
logging.basicConfig(
format='%(levelname)s: %(message)s',
level=logging.INFO)
class YahooDownload(object):
urlhist = 'https://finance.yahoo.com/quote/{}/history'
urldown = 'https://query1.finance.yahoo.com/v7/finance/download'
retries = 3
def __init__(self, ticker, fromdate, todate, period='d', reverse=False):
try:
import requests
except ImportError:
msg = ('The new Yahoo data feed requires to have the requests '
'module installed. Please use pip install requests or '
'the method of your choice')
raise Exception(msg)
url = self.urlhist.format(ticker)
sesskwargs = dict()
if False and self.p.proxies:
sesskwargs['proxies'] = self.p.proxies
crumb = None
sess = requests.Session()
for i in range(self.retries + 1): # at least once
resp = sess.get(url, **sesskwargs)
if resp.status_code != requests.codes.ok:
continue
txt = resp.text
i = txt.find('CrumbStore')
if i == -1:
continue
i = txt.find('crumb', i)
if i == -1:
continue
istart = txt.find('"', i + len('crumb') + 1)
if istart == -1:
continue
istart += 1
iend = txt.find('"', istart)
if iend == -1:
continue
crumb = txt[istart:iend]
crumb = crumb.encode('ascii').decode('unicode-escape')
break
if crumb is None:
self.error = 'Crumb not found'
self.f = None
return
# urldown/ticker?period1=posix1&period2=posix2&interval=1d&events=history&crumb=crumb
# Try to download
urld = '{}/{}'.format(self.urldown, ticker)
urlargs = []
posix = datetime.date(1970, 1, 1)
if todate is not None:
period2 = (todate.date() - posix).total_seconds()
urlargs.append('period2={}'.format(int(period2)))
if todate is not None:
period1 = (fromdate.date() - posix).total_seconds()
urlargs.append('period1={}'.format(int(period1)))
intervals = {
'd': '1d',
'w': '1wk',
'm': '1mo',
}
urlargs.append('interval={}'.format(intervals[period]))
urlargs.append('events=history')
urlargs.append('crumb={}'.format(crumb))
urld = '{}?{}'.format(urld, '&'.join(urlargs))
f = None
for i in range(self.retries + 1): # at least once
resp = sess.get(urld, **sesskwargs)
if resp.status_code != requests.codes.ok:
continue
ctype = resp.headers['Content-Type']
if 'text/csv' not in ctype:
self.error = 'Wrong content type: %s' % ctype
continue # HTML returned? wrong url?
# buffer everything from the socket into a local buffer
try:
# r.encoding = 'UTF-8'
f = io.StringIO(resp.text, newline=None)
except Exception:
continue # try again if possible
break
self.datafile = f
def writetofile(self, filename):
if not self.datafile:
return
if not hasattr(filename, 'read'):
# It's not a file - open it
f = io.open(filename, 'w')
else:
f = filename
self.datafile.seek(0)
for line in self.datafile:
f.write(line)
f.close()
def parse_args():
parser = argparse.ArgumentParser(
description='Download Yahoo CSV Finance Data')
parser.add_argument('--ticker', required=True,
help='Ticker to be downloaded')
parser.add_argument('--reverse', action='store_true', default=False,
help='Do reverse the downloaded files')
parser.add_argument('--timeframe', default='d',
help='Timeframe: d -> day, w -> week, m -> month')
parser.add_argument('--fromdate', required=True,
help='Starting date in YYYY-MM-DD format')
parser.add_argument('--todate', required=True,
help='Ending date in YYYY-MM-DD format')
parser.add_argument('--outfile', required=True,
help='Output file name')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
logging.info('Processing input parameters')
logging.info('Processing fromdate')
try:
fromdate = datetime.datetime.strptime(args.fromdate, '%Y-%m-%d')
except Exception as e:
logging.error('Converting fromdate failed')
logging.error(str(e))
sys.exit(1)
logging.info('Processing todate')
try:
todate = datetime.datetime.strptime(args.todate, '%Y-%m-%d')
except Exception as e:
logging.error('Converting todate failed')
logging.error(str(e))
sys.exit(1)
logging.info('Do Not Reverse flag status')
reverse = args.reverse
logging.info('Downloading from yahoo')
try:
yahoodown = YahooDownload(
ticker=args.ticker,
fromdate=fromdate,
todate=todate,
period=args.timeframe,
reverse=reverse)
except Exception as e:
logging.error('Downloading data from Yahoo failed')
logging.error(str(e))
sys.exit(1)
logging.info('Opening output file')
try:
ofile = io.open(args.outfile, 'w')
except IOError as e:
logging.error('Error opening output file')
logging.error(str(e))
sys.exit(1)
logging.info('Writing downloaded data to output file')
try:
yahoodown.writetofile(ofile)
except Exception as e:
logging.error('Writing to output file failed')
logging.error(str(e))
sys.exit(1)
logging.info('All operations completed successfully')
sys.exit(0)