2

I want to learn to use the API from Interactive Brokers. I have an account with them, I open the TWS platform and then I execute a code copied from their Youtube channel. It works fine the first time, but after that if I execute again I see this in the console:

"Process finished with exit code 0"

And nothing else.

Sometimes after one minute it works again, no idea what the hell is going on.

-I tried using "terminate" in the console and running again but sometimes it works and sometimes it doesn't.

-I was receiving some messages from Windows Defender but I updated it and that should not be the origin of the problem. EDIT (I get the same error from windows defender now, I am going to update again)

-I tried closing Pycharm and starting it again and it works the first time only, the second is empty, but if I wait a few minutes it works again, but not always. I know there are some limitations like a max number of requests per second, but in this particular case I am just asking for contract details so I guess that is not the issue.

    from ibapi.client import EClient
    from ibapi.wrapper import EWrapper
    from ibapi.contract import Contract


    class TestApp (EWrapper, EClient):
        def __init__(self):
            EClient.__init__(self, self)

        def error(self, reqId, errorCode, errorString):
           print ("Error: ", reqId, " ", errorCode, " ", errorString)

        def contractDetails(self, reqId, contractDetails):
            print ("contract details", reqId, " ", contractDetails)


    def main ():

        app = TestApp ()
        app.connect("127.0.0.1", 7497, 0)

        contract = Contract()
        contract.symbol = "EUR"
        contract.secType = "CASH"
        contract.exchange = "IDEALPRO"
        contract.currency = "USD"


        app.reqContractDetails(1, contract)
        app.run()
        app.disconnect()

    if __name__ == "__main__":
        main()

EDIT:

Let me add that this code always works fine, just checks the connection:

    from ibapi.client import EClient
    from ibapi.wrapper import EWrapper

    class IBapi(EWrapper, EClient):
         def __init__(self):
             EClient.__init__(self, self)

    app = IBapi()
    app.connect('127.0.0.1', 7497, 123)
    app.run()

EDIT 2:

This is really frustrating, now nothing works, the console output is nothing, it is empty. I still get the windows defender message even after excluding the folders.

EDIT 3:

I reinstalled Pycharm 2019 and now I have 2020 and I get the output. Part of the problem of my code is the lack of a call to the function "disconnect()" to stop the run loop. Be careful with that folks. Brian's posts were very useful.

  • That can't be the example code, app.disconnect() will never be called. app.run() starts an infinite loop. Check this code, https://stackoverflow.com/a/61604561/2855515 – brian Oct 21 '20 at 23:16
  • Thank you very much for your answer.I checked and I copied the code properly, in other words, their tutorial does not use the disconnect function. I tried to add that line after run (not sure if that is where it should go) and I got the same problem, empty output. You can check the video here: https://www.youtube.com/watch?v=dzOilFBDmJI&list=PL71vNXrERKUpPreMb3z1WGx6fOTCzMaH1&index=4 I am going to read your link. – pedro rodriguez Oct 22 '20 at 06:48
  • 1
    The example is bad code. It's counting on killing the python program to disconnect the socket from TWS. I suggest you open the data window in TWS (click on DATA) and see your connections. Watch for the api client disconnecting and at that time you can reconnect. A better way is in my code shown in the link. It also shows how to get usable contract info. Here is another link which I think is a duplicate of your problem https://stackoverflow.com/a/56917618/2855515 . – brian Oct 22 '20 at 18:09
  • I reinstalled Pycharm and everything looks good now. You are right, I have to kill a scratch before runing other one because it is still connected. At least I get the output and I can learn, it was horrible. Thanks for your help, I really appreciate. I don't find how to mark this as "solved". Best Regards. – pedro rodriguez Oct 22 '20 at 21:45

0 Answers0