Python code works first time, then it does not show any output, and after that it works again. Interactive Brokers API

593 views Asked by At

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.

0

There are 0 answers