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.
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: