-2

I'm getting started with the interactive brokers api, and starting off the examples do seem to work, but the one thing that's not is that the connection hangs pretty much on every instantiation of the IBapi class. Here's a basic example:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper 
import time 
 
class IBapi(EWrapper, EClient):
     def __init__(self):
         EClient.__init__(self, self) 
     
     print('testing')
 
app = IBapi()
app.connect('127.0.0.1', 7497, 0)
app.run()
print("TEST successful: The app will stop running in 5 seconds...")
time.sleep(5)
app.disconnect() <--- suppose to disconnect

The connection works, as I'm able to place an order (with additional code) in my paper account, but the connection stays up, and I'd have to go a different terminal and just kill the python process. Any ideas of how can I run this code and disconnect when done? TIA

craftytech
  • 37
  • 7

1 Answers1

0

app.run() Starts an infinite loop socket reader so no interaction after that is possible.

You can run this command in it's own thread and then continue send commands or plan your program to respond to socket reader events.

Here is an answer where I show both methods.

https://stackoverflow.com/a/54423878/2855515

brian
  • 10,619
  • 4
  • 21
  • 79