erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.8k stars 743 forks source link

nest_asyncio==1.3.0 does not work anymore with util.patchAsyncio #226

Closed idanre1 closed 4 years ago

idanre1 commented 4 years ago

Hi I am using ib_insync version 0.9.59 and nest_asyncio==1.3.0

This is a toy example that was working for me on ib_insync version 0.9.58 and nest_asyncio==1.0.0 and stopped working on latest version:

import pandas as pd
from ib_insync import IB, Forex, Contract, Index, util
import os.path
import datetime
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import asyncio

util.patchAsyncio()
util.logToFile("df.log")

async def tick_10(c):
    print('Tick! The time for the 10 second job is: {}'.format(datetime.datetime.now()))
    if not ib.isConnected():
        pass
    else:
        bars = ib.reqHistoricalData(c,
                                endDateTime=pd.to_datetime("2018-04-01"),
                                durationStr='1 D',
                                barSizeSetting='1 min',
                                whatToShow='MIDPOINT',
                                formatDate=2,
                                useRTH=True)
        df = util.df(bars)
        if not df is None:
            print(df.tail().loc[:,["date","close"]])

scheduler = AsyncIOScheduler()
ib = IB()
ib.connect('127.0.0.1', 7496, clientId=1)

# define a contract
c = ib.qualifyContracts(Forex('EURUSD'))[0]

# define a scheduled task
scheduler.add_job(tick_10, args=[c],trigger='cron', minute='*',second='*/10')
scheduler.start()
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
# Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed.
try:
    asyncio.get_event_loop().run_forever()
except (KeyboardInterrupt, SystemExit):
    pass

Could you please have a look?

Thanks! Idan

erdewit commented 4 years ago

Thanks for the bug report.

This is an issue with loop.run_forever() from Python 3.8. It has been addressed in nest_asyncio v1.3.2.