How to Handle Python IB API Connection Problems?

Discussion in 'Automated Trading' started by Cyrix, Feb 20, 2018.

  1. Cyrix

    Cyrix

    I am trying to use Python IB API to do a simple task: at 10 am every day, the program first gets the size of the account, then buys 10 share of a stock.

    Based on the codes presented in IB's webinar, I am able to write this basic program (below), which I want to run 24/7. There is one problem: if the TWS disconnects, the program throws errors and stops.

    Is there a simple code I can add to the program so that when TWS crashes or disconnects, it exits the infinite loop of run() and returns to the while loop to try to do the task again tomorrow?

    Thanks.

    Code:
    class TestApp(EWrapper, EClient):
        def __init__(self):
            EClient.__init__(self,self)
            self.net_Liq = 0
    
        def updateAccountValue(self, key: str, val: str, currency: str, accountName: str):
            super().updateAccountValue(key, val, currency, accountName)
            if key=="NetLiquidation":
                self.net_Liq=val
    
        def accountDownloadEnd(self, accountName: str):
            super().accountDownloadEnd(accountName)
            self.placeOrder(self.nextOrderId(), ContractSamples.USStock(), OrderSamples.MarketOrder("BUY", 10))
    
    
    def main():
        app=TestApp()
        while(1):
            now = datetime.datetime.now()
            if now.hour==10:
                app.connect("127.0.0.1", 7497, 0)
                app.reqAccountUpdates(True, "")
                app.run()
            time.sleep(3600)
    
    if __name__== "__main__":
        main()
     
    Last edited: Feb 20, 2018
  2. 2rosy

    2rosy

    try/except. handle error(), error_0(), error_1()
     
    tommcginnis likes this.
  3. d08

    d08

    You need an error handler. So when errorCode returns 1100 it's disconnected, you can use another variable to then to halt execution until it's restored with either 1101 or 1102.

    This is with IbPy so it might be a bit different but the idea is the same.
     
    tommcginnis likes this.
  4. DaveV

    DaveV

    Instead of using TWS, consider running the IB Gateway and have your program connect to it. The Gateway can run literally run for months without having to be restarted. You will only have to make very minor changes to your API program to connect to the IB Gateway vs TWS, such as the connection port number.

    The big problem with the using the Gateway vs TWS is that all the ancillary functions that TWS provides, such as showing executions, bids and ask, etc. will have to be handled by your program. I have no idea if you can simultaneously run the Gateway and TWS, since I have never tried it.
     
  5. d08

    d08

    There's no huge difference in whether you run TWS or Gateway. If the TWS was disconnected, most likely the gateway was also.
     
  6. DaveV

    DaveV

    I agree that the gateway will probably disconnect also, and the app will have to handle that exception. I was addressing not only the disconnect, but Cyrix's statement that he/she wanted the app to run 24/7. TWS cannot run 24/7, but the Gateway can.
     
    d08 likes this.
  7. When using the IBController tool can also TWS run 24/7.
     
  8. DaveV

    DaveV

    Wow. Very impressive program. Wish I had known about IBController before I converted my system from using TWS to using the IB Gateway. I may have gone in a different direction. I do miss some of the TWS features.
     
  9. If you want to actually trade, you don't handle Python IB API connection problems, unless you want to be a professional programmer. You lease, or purchase a stable C# trading platform, customize how you like, and let the developers worry about the bugs and instabilities.
     
  10. d08

    d08

    I'm curious as to what you had to change. TWS has effectively the gateway built in, they're the same except TWS comes with a GUI.
    I can switch from TWS to Gateway with zero modifications.

    For something relatively straightforward, there's no need for that and you don't turn into a professional programmer with 20 lines of code. And why does it has to be C#?
     
    #10     Feb 21, 2018