Skip to content
Closed
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
10 changes: 8 additions & 2 deletions backtester/dataSource/data_source_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
from time import mktime as mktime
from itertools import groupby

def getHeadersForYahoo():
return {'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}


def getCookieForYahoo(instrumentId):
"""Returns a tuple pair of cookie and crumb used in the request"""
url = 'https://finance.yahoo.com/quote/%s/history' % (instrumentId)
req = requests.get(url)

req = requests.get(url, headers=getHeadersForYahoo())
txt = req.content
cookie = req.cookies['B']
pattern = re.compile('.*"CrumbStore":\{"crumb":"(?P<crumb>[^"]+)"\}')
Expand All @@ -31,7 +37,7 @@ def downloadFileFromYahoo(startDate, endDate, instrumentId, fileName, event='his
start = int(mktime(startDate.timetuple()))
end = int(mktime(endDate.timetuple()))
url = 'https://query1.finance.yahoo.com/v7/finance/download/%s?period1=%s&period2=%s&interval=1d&events=%s&crumb=%s' % (instrumentId, start, end, event, crumb)
data = requests.get(url, cookies={'B': cookie})
data = requests.get(url, cookies={'B': cookie}, headers=getHeadersForYahoo())
with open(fileName, 'wb') as f:
f.write(data.content)
return True
Expand Down