简体   繁体   中英

asyncio to download option quotes from finance yahoo

I am working on an option quotes downloader from finance yahoo using Python 3.9 with asyncio, aiohttp:

  • there is a need to develop a chained download sequence because the 1st request for each option ticker gets all the expiration dates and the 2nd request gets the option quotes per ticker and expiration date;
  • when downloading only 2 tickers everything is working but when downloading 300-400 tickers then the data are not correct;
  • I assume not all requests are processed.

Could you advise how to find this bottleneck? Here is the code:

def get_tasks(session, tickers):
    tasks = []
    for ticker in tickers:
        print(ticker)
        url_options1 = f"https://query2.finance.yahoo.com/v7/finance/options/{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/finance/options/{ticker}?&date={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[i]['optionChain']['result'][0]['underlyingSymbol']
        tickers.append(symbol)
        if datas[i]['optionChain']['result'][0]['expirationDates']:
            expiries.append(datas[i]['optionChain']['result'][0]['expirationDates'])

    except IndexError as e:
        print(f"{i} {e}")

for i in range(len(tickers)):

    datas2 = asyncio.run(get_optiondata2(tickers[i], expiries[i]), debug=True)

您可以修改该端点的查询参数以检索代码的所有到期日期:

url = 'https://query2.finance.yahoo.com/v7/finance/options/{ticker}?getAllData=true'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM