2

I want to get the status of my previous orders. I have the following simple code but I only get True/False values. Here is my code:

from ib.opt import Connection, message

tws_conn = Connection.create(port=7497, clientId=999)
tws_conn.connect()

def acct_update(msg):
    print msg  

tws_conn.register(acct_update, message.openOrder)
tws_conn.register(acct_update, message.orderStatus)

here is the output:

>>> from ib.opt import Connection, message
>>> 
>>> tws_conn = Connection.create(port=7497, clientId=999)
>>> tws_conn.connect()
Server Version: 76
TWS Time at connection:20161021 18:53:42 EST
True
>>> 
>>> def acct_update(msg):
...     print msg  
... 
>>> tws_conn.register(acct_update, message.openOrder)
True
>>> tws_conn.register(acct_update, message.orderStatus)
True
>>> 
>>> 

How can I get the list of open Orders? and how can I get the status of an individual order?

Thanks.

user1871528
  • 1,655
  • 3
  • 27
  • 41

1 Answers1

1

You forgot to ask! Add a line tws_conn.reqOpenOrders() to get the orders. You'll see both an openOrder and orderStatus callback. You may want to add a line tws_conn.register(acct_update, message.openOrderEnd) to know when you've finished getting openOrders. I've never tested that, and as usual people ask these questions on Friday night ;)

If you want orders placed by all clients and TWS use tws_conn.reqAllOpenOrders()

Here is a sample from an order just placed in TWS (clientId = 0) but obviously won't fill for a couple days.

<openOrder orderId=0, contract=<ib.ext.Contract.Contract object at 0x00000000041FA0F0>, order=<ib.ext.Order.Order object at 0x00000000043FF080>, orderState=<ib.ext.OrderState.OrderState object at 0x00000000043FF5C0>>

<orderStatus orderId=0, status=PreSubmitted, filled=0, remaining=1, avgFillPrice=0.0, permId=1905611953, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld=None>

<openOrderEnd>

Note there is an API setting "Download open orders on connection" but it is unreliable and it's best if you keep track of orders on your own and ask for executions. See... if an order is filled while you're not connected, then it's no longer an open order and you won't get it.

The true's are just what the command line prints for those commands...means nothing went wrong I guess.

brian
  • 10,619
  • 4
  • 21
  • 79