Crooner
Crooner

Reputation: 61

How do I retrieve the status of open orders using IBpy?

I am using a paper trading IB-account where trades are processed just fine. I have a number of unfilled orders. The "updateAccountValue" performs as expected, while "con.register(acct_update, message.reqAllOpenOrders())" does nothing, neither does "con.reqOpenOrders()'. Neither raises an error either. The IB documentation tells me that all three methods are 'void' which I understand to mean that no value is supposed to be returned. Yet, as stated, the "updateAccountValue" method works perfectly fine, supplying the proper output.

Question 1: how do I retrieve data regarding (unfilled) open orders?

I have also noticed that the code does not always run, but it always runs properly right after (re-)starting the TWS workstation application.

Question 2: why does this code not run every time it is launched?

from ib.opt import ibConnection, message
import sys

def acct_update(msg):
    print msg   

con = ibConnection(clientId=100)
con.register(acct_update,
        message.updateAccountValue)
con.register(acct_update,
        message.reqAllOpenOrders())

con.connect()
con.reqAccountUpdates(True,'DU000000')
con.reqAllOpenOrders()
con.reqOpenOrders()

sys.exit()

Upvotes: 0

Views: 1597

Answers (2)

Vicky
Vicky

Reputation: 31

I was trying to figure out how to print out all open orders. Here is what I found that may help you with your first question.

  1. Add a print function to the original IBpy documents of Order.py and Contract.py.

In the Order.py add:

def __str__(self):
    return "Action: " + self.m_action + ", Quantity: " + str(self.m_totalQuantity) + ", at price: " + str(self.m_lmtPrice)

In the Contract.py add:

def __str__(self):
    return "secType: " + self.m_secType + ", symbol: " + self.m_symbol + "; expiry: " + self.m_expiry

You can modify the fields to show what you would like to view.

  1. In your own python file:

``

from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order

def print_open_order_messege(msg):
    print ("open_order: " + str(msg.orderId) + "::" + str(msg.contract) + "::" + str(msg.order) + "::"+str(msg.orderState))

def print_order_status_messege(msg):
    print ("order_status: " + str(msg.orderId) + "::" + "Status: " + msg.status + ", Filled: " + str(msg.filled) + ", Remaining: " + str(msg.remaining) + ", avgFillPrice: " + str(msg.avgFillPrice))

con.register(print_open_order_messege, message.openOrder)
con.register(print_order_status_messege, message.orderStatus)
con.reqAllOpenOrders()

It prints out my test order as below:

... open_order: 2::secType: FUT, symbol: NQ; expiry: 20161216::action: BUY, quantity: 1, at price: 4500.0::Status: PendingCancel order_status: 2::Status: PendingCancel Filled: 0 Remaining: 1 avgFillPrice: 0.0

Upvotes: 3

brian
brian

Reputation: 10989

Notice the difference ?

con.register(acct_update,
        message.updateAccountValue)
con.register(acct_update,
        message.reqAllOpenOrders())

You should use message.openOrder

Also, you're sending it to the acct_update callback but since it just prints, it's no big deal. However if you want information from the callback, here is the format it arrives in.

<openOrder orderId=123469, contract=<ib.ext.Contract.Contract object at 0x7f68daeff6a0>, order=<ib.ext.Order.Order object at 0x7f68e80d2668>, orderState=<ib.ext.OrderState.OrderState object at 0x7f68daf39240>>

You also call exit() before the program probably has a chance to finish. It's asynchronous, that means you have to wait for a reply.

Upvotes: 1

Related Questions