Optionsdaten download von finance yahoo api mittels asyncio, aiohttp
Verfasst: Samstag 16. Oktober 2021, 04:15
Hallo
ich arbeite an einem Optionsdownloader fuer finance.yahoo.com mittels asyncio, aiohttp.
Das Problem ist, bei wenigen Tickern funktioniert der Download korrekt; jedoch bei mehreren hundert werden die requests nicht korrekt abgearbeitet
- um Optionsdaten per Ticker herunterzuladen muss man zuerst die "expiration dates" per Ticker herunterladen
- danach kann man alle Optionsdaten per Ticker und expiration date herunterladen
Wo ist hier der Flaschenhals - Wer kann mir einen Tipp hier geben? Hier ist der Code:
def get_tasks(session, tickers):
tasks = []
for ticker in tickers:
print(ticker)
url_options1 = f"https://query2.finance.yahoo.com/v7/fin ... ns/{ticker}?"
tasks.append(asyncio.create_task(session.get(url_options1, headers={'User-Agent': ua}, ssl=True)))
return tasks
def get_tasks2(session, ticker, expiries):
tasks2 = []
for expiry in expiries:
url_options2 = f"https://query2.finance.yahoo.com/v7/fin ... te={expiry}"
tasks2.append(asyncio.create_task(session.get(url_options2, headers={'User-Agent': ua}, ssl=True)))
return tasks2
async def get_optiondata(tickers):
results1 = []
async with aiohttp.ClientSession() as session:
tasks = get_tasks(session, tickers)
responses = await asyncio.gather(*tasks)
for response in responses:
results1.append(await response.json())
return results1
async def get_optiondata2(ticker, expiries):
results2 = []
async with aiohttp.ClientSession() as session:
tasks2 = get_tasks2(session, ticker, expiries)
responses = await asyncio.gather(*tasks2)
for response in responses:
results2.append(await response.json())
return results2
datas = asyncio.run(get_optiondata(usTickers))
results = []
tickers = []
expiries = []
for i in range(len(datas)):
try:
symbol = datas['optionChain']['result'][0]['underlyingSymbol']
tickers.append(symbol)
if datas['optionChain']['result'][0]['expirationDates']:
expiries.append(datas['optionChain']['result'][0]['expirationDates'])
except IndexError as e:
print(f"{i} {e}")
for i in range(len(tickers)):
datas2 = asyncio.run(get_optiondata2(tickers, expiries), debug=True)
results = []
volumeCalls, volumePuts, volumeCallsTotal, volumePutsTotal = 0, 0, 0, 0
for j in range(len(datas2)):
if len(datas2) > 1:
try:
volumeCalls = get_option2(tickers, datas2[j]['optionChain']['result'][0]['options'][0]['calls'], 'Call')
print(volumeCallsTotal)
volumeCallsTotal += volumeCalls
except IndexError:
print(f"{tickers} Index Error")
ich arbeite an einem Optionsdownloader fuer finance.yahoo.com mittels asyncio, aiohttp.
Das Problem ist, bei wenigen Tickern funktioniert der Download korrekt; jedoch bei mehreren hundert werden die requests nicht korrekt abgearbeitet
- um Optionsdaten per Ticker herunterzuladen muss man zuerst die "expiration dates" per Ticker herunterladen
- danach kann man alle Optionsdaten per Ticker und expiration date herunterladen
Wo ist hier der Flaschenhals - Wer kann mir einen Tipp hier geben? Hier ist der Code:
def get_tasks(session, tickers):
tasks = []
for ticker in tickers:
print(ticker)
url_options1 = f"https://query2.finance.yahoo.com/v7/fin ... ns/{ticker}?"
tasks.append(asyncio.create_task(session.get(url_options1, headers={'User-Agent': ua}, ssl=True)))
return tasks
def get_tasks2(session, ticker, expiries):
tasks2 = []
for expiry in expiries:
url_options2 = f"https://query2.finance.yahoo.com/v7/fin ... te={expiry}"
tasks2.append(asyncio.create_task(session.get(url_options2, headers={'User-Agent': ua}, ssl=True)))
return tasks2
async def get_optiondata(tickers):
results1 = []
async with aiohttp.ClientSession() as session:
tasks = get_tasks(session, tickers)
responses = await asyncio.gather(*tasks)
for response in responses:
results1.append(await response.json())
return results1
async def get_optiondata2(ticker, expiries):
results2 = []
async with aiohttp.ClientSession() as session:
tasks2 = get_tasks2(session, ticker, expiries)
responses = await asyncio.gather(*tasks2)
for response in responses:
results2.append(await response.json())
return results2
datas = asyncio.run(get_optiondata(usTickers))
results = []
tickers = []
expiries = []
for i in range(len(datas)):
try:
symbol = datas['optionChain']['result'][0]['underlyingSymbol']
tickers.append(symbol)
if datas['optionChain']['result'][0]['expirationDates']:
expiries.append(datas['optionChain']['result'][0]['expirationDates'])
except IndexError as e:
print(f"{i} {e}")
for i in range(len(tickers)):
datas2 = asyncio.run(get_optiondata2(tickers, expiries), debug=True)
results = []
volumeCalls, volumePuts, volumeCallsTotal, volumePutsTotal = 0, 0, 0, 0
for j in range(len(datas2)):
if len(datas2) > 1:
try:
volumeCalls = get_option2(tickers, datas2[j]['optionChain']['result'][0]['options'][0]['calls'], 'Call')
print(volumeCallsTotal)
volumeCallsTotal += volumeCalls
except IndexError:
print(f"{tickers} Index Error")