erdewit / ib_insync

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

ib_insync with IBC watchdog error #113

Closed charlesedwards closed 5 years ago

charlesedwards commented 5 years ago

Hi,

I am new to ib_insync and LOVE your platform, great work!

This may be an issue on my behalf, but trying to use IBC and ib_insync to launch the TWS platform... run some code... then close the tws platform, but I get the below error:

2018-12-09 19:05:58,220 ib_insync.Watchdog INFO Stopping
2018-12-09 19:05:58,220 ib_insync.ib INFO Disconnecting from 127.0.0.1:7496, 182 B sent in 7 messages, 20.1 kB received in 337 messages, session time 75.2 s.
2018-12-09 19:05:58,220 ib_insync.client INFO Disconnected
2018-12-09 19:05:58,220 ib_insync.Watchdog INFO Stopping
2018-12-09 19:05:58,221 asyncio ERROR Exception in callback _ProactorBasePipeTransport._call_connection_lost(None)
handle: <Handle _ProactorBasePipeTransport._call_connection_lost(None)>
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\nest_asyncio.py", line 162, in run
    ctx.run(self._callback, *self._args)
  File "C:\ProgramData\Anaconda3\lib\asyncio\proactor_events.py", line 134, in _call_connection_lost
    self._protocol.connection_lost(exc)
  File "C:\ProgramData\Anaconda3\lib\site-packages\ib_insync\client.py", line 445, in connection_lost
    self.connection.disconnected()
  File "C:\ProgramData\Anaconda3\lib\site-packages\ib_insync\client.py", line 297, in _onSocketDisconnected
    self.apiEnd.emit()
  File "C:\ProgramData\Anaconda3\lib\site-packages\ib_insync\event.py", line 83, in emit
    obj(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\ib_insync\event.py", line 81, in emit
    func(obj, *args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\ib_insync\ibcontroller.py", line 407, in _stop
    self.controller.terminate()
  File "C:\ProgramData\Anaconda3\lib\site-packages\ib_insync\ibcontroller.py", line 127, in terminate
    util.run(self.terminateAsync())
  File "C:\ProgramData\Anaconda3\lib\site-packages\ib_insync\util.py", line 236, in run
    result = loop.run_until_complete(future)
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 555, in run_until_complete
    self.run_forever()
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 510, in run_forever
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
2018-12-09 19:05:58,225 ib_insync.IBC INFO Terminating
2018-12-09 19:05:58,226 ib_insync.IBC INFO Terminating
2018-12-09 19:05:58,237 asyncio ERROR Task was destroyed but it is pending!
task: <Task pending coro=<Watchdog._watchAsync() running at C:\ProgramData\Anaconda3\lib\site-packages\ib_insync\ibcontroller.py:434> wait_for=<Future pending cb=[Task.__wakeup()]>>

If it helps, my code is as follows:


from ib_insync import *
import asyncio
import logging
util.logToConsole(logging.DEBUG)
util.patchAsyncio()

asyncio.set_event_loop(asyncio.ProactorEventLoop())
ibc = IBC(twsVersion=974,
                 gateway=False,
                 tradingMode='live',
                 ibcIni=r'C:\Users\Administrator\Documents\IBC\config.ini',
                 ibcPath='C:\IBC',
                 twsPath='C:\Jts')
ib = IB()
app = Watchdog(ibc,ib, port='7496',clientId=11,appStartupTime=60, appTimeout=10)
app.start()
print(app.ib.accountValues())
print('hello...')
ib.sleep(15)
asyncio.tasks.
app.stop()
erdewit commented 5 years ago
asyncio.set_event_loop(asyncio.ProactorEventLoop())
util.patchAsyncio()

Reordering these two lines around should fix it (then the right event loop will get patched).

charlesedwards commented 5 years ago

Thanks you @erdewit.!

That got rid of the error, but how do I STOP the script from running altogether when I am done? the app.stop() command doesnt seem to work pre my above script, it is still running?

This is the log at the end:

2018-12-10 23:31:19,631 ib_insync.wrapper DEBUG Timeout
2018-12-10 23:31:19,631 ib_insync.Watchdog DEBUG Soft timeout
2018-12-10 23:31:19,631 ib_insync.connection DEBUG >>> 20,43,0,EUR,CASH,,0.0,,,IDEALPRO,,USD,,,0,,5 secs,30 S,0,MIDPOINT,1,0,,
2018-12-10 23:31:21,799 ib_insync.client DEBUG <<< 4,2,-1,2106,HMDS data farm connection is OK:cashhmds
2018-12-10 23:31:21,799 ib_insync.wrapper INFO Warning 2106, reqId -1: HMDS data farm connection is OK:cashhmds
2018-12-10 23:31:22,184 ib_insync.client DEBUG <<< 17,43,20181210  23:30:49,20181210  23:31:19,7,20181210  23:30:45,1.142075,1.142125,1.142015,1.142015,-1,-1.00000,-1,20181210  23:30:50,1.142015,1.142055,1.142005,1.142015,-1,-1.00000,-1,20181210  23:30:55,1.142015,1.142075,1.142015,1.14207,-1,-1.00000,-1,20181210  23:31:00,1.14207,1.14207,1.142015,1.142015,-1,-1.00000,-1,20181210  23:31:05,1.142015,1.142025,1.14199,1.14199,-1,-1.00000,-1,20181210  23:31:10,1.14199,1.142015,1.141945,1.141955,-1,-1.00000,-1,20181210  23:31:15,1.141955,1.141955,1.141945,1.14195,-1,-1.00000,-1
2018-12-10 23:31:24,623 ib_insync.Watchdog INFO Stopping
2018-12-10 23:31:24,624 ib_insync.ib INFO Disconnecting from 127.0.0.1:7496, 182 B sent in 7 messages, 16.3 kB received in 326 messages, session time 135 s.
2018-12-10 23:31:24,624 ib_insync.client INFO Disconnected
2018-12-10 23:31:24,624 ib_insync.Watchdog INFO Stopping
2018-12-10 23:31:24,624 ib_insync.IBC INFO Terminating
2018-12-10 23:31:24,867 asyncio ERROR Task was destroyed but it is pending!
task: <Task pending coro=<Watchdog._watchAsync() running at C:\ProgramData\Anaconda3\lib\site-packages\ib_insync\ibcontroller.py:434> wait_for=<Future pending cb=[Task.__wakeup()]>>
erdewit commented 5 years ago

This only place were app.stop() can block is when it waits for the TWS process to be terminated. I'm not sure what can go wrong there.

charlesedwards commented 5 years ago

@erdewit, I am trying to finish the script; i.e. shutdown TWS and Stop the script.

I thought app.stop() would do this, but it appears not? What should I have at the bottom of the script to close TWS and end the script?

charlesedwards commented 5 years ago

I think this may be a bug associated with app.stop() and ibc.terminate - I have tried both approaches (calling IBC alone and using WATCHDOG) and it appears the process gets stuck in these steps.