代码在带有ib insync版本 0.9.34的 Ubuntu 中运行良好
我想将我的代码切换到 Windows,并再次下载所有包,包括 ib insync 版本到最新版本。
当我运行代码时,我得到了RuntimeWarning: coroutine 'dailyPrice' was never awaited
我不确定是否是版本更改导致了问题。我在 Ubuntu 机器上运行一切正常。
但是,错误指向 asyncio 部分。
async def dailyPrice(collection, symbol, exchange, currency, primary, semaphore):
async with semaphore:
TF = ifExist(collection, symbol, primary)
if TF == False:
stock = Stock(symbol, exchange, currency, primaryExchange= primary)
#ib.qualifyContractsAsync(stock)
assert await ib.reqContractDetailsAsync(stock)
data = await ib.reqHistoricalDataAsync(
contract=stock, endDateTime='', durationStr='2 D',
barSizeSetting='1 day', whatToShow='TRADES',useRTH=True,
formatDate=2)
df = util.df(data)
df['symbol'] = symbol
try:
data = pushToDB(df, primary)
except:
errors.append(symbol)
return df
semaphore = asyncio.BoundedSemaphore(20)
async def main():
return await asyncio.gather(*coros, return_exceptions=True)
if __name__ == '__main__':
retry = 3
while (countTicker(collection) < len(stockList)) and (retry > 0):
ib = IB()
#ib.setCallback('error', onError)
ib.connect('127.0.0.1', 4002, clientId=2)
stockList = findMissing(collection, stockList) #replace the stockList with missing Ticker
print("Missing {} stocks' data".format(len(stockList)))
coros = []
for symbol, exchange in zip(stockList['IB Symbol'], stockList['Exchange']):
coros.append(dailyPrice(collection, symbol, 'SMART', 'USD', exchange, semaphore))
try:
with timeout(3600, exception=RuntimeError):
loop = asyncio.get_event_loop()
lst_result = loop.run_until_complete(main())
except Exception as e:
print('logging')
retry += 1
ib.disconnect()
retry -= 1
print("Try remaining {}".format(retry))
#restore full stockList
stockList = pd.read_csv('stockList.csv')
stockList.drop_duplicates(subset=['IB Symbol'], inplace=True)
end = time.time()
print(end - start)