Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Yahoo download data issue #488

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 4 additions & 45 deletions backtrader/feeds/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,27 +225,20 @@ class YahooFinanceData(YahooFinanceCSVData):
Whether to use the dividend/split adjusted close and adjust all values
according to it.

- ``urlhist``

The url of the historical quotes in Yahoo Finance used to gather a
``crumb`` authorization cookie for the download

- ``urldown``

The url of the actual download server

- ``retries``

Number of times (each) to try to get a ``crumb`` cookie and download
the data
Number of times (each) to try to download the data

'''

params = (
('proxies', {}),
('period', 'd'),
('reverse', False),
('urlhist', 'https://finance.yahoo.com/quote/{}/history'),
('urldown', 'https://query1.finance.yahoo.com/v7/finance/download'),
('retries', 3),
)
Expand All @@ -260,47 +253,12 @@ def start_v7(self):
raise Exception(msg)

self.error = None
url = self.p.urlhist.format(self.p.dataname)

sesskwargs = dict()
if self.p.proxies:
sesskwargs['proxies'] = self.p.proxies

crumb = None
sess = requests.Session()
sess.headers['User-Agent'] = 'backtrader'
for i in range(self.p.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

crumb = urlquote(crumb)

# urldown/ticker?period1=posix1&period2=posix2&interval=1d&events=history&crumb=crumb
# urldown/ticker?period1=posix1&period2=posix2&interval=1d&events=history

# Try to download
urld = '{}/{}'.format(self.p.urldown, self.p.dataname)
Expand All @@ -323,10 +281,11 @@ def start_v7(self):

urlargs.append('interval={}'.format(intervals[self.p.timeframe]))
urlargs.append('events=history')
urlargs.append('crumb={}'.format(crumb))

urld = '{}?{}'.format(urld, '&'.join(urlargs))
f = None
sess = requests.Session()
sess.headers['User-Agent'] = 'backtrader'
for i in range(self.p.retries + 1): # at least once
resp = sess.get(urld, **sesskwargs)
if resp.status_code != requests.codes.ok:
Expand Down
2 changes: 1 addition & 1 deletion backtrader/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
unicode_literals)


__version__ = '1.9.78.123'
__version__ = '1.9.78.124'

__btversion__ = tuple(int(x) for x in __version__.split('.'))