I am exiting open trades with market orders and am having trouble linking the exit with the entry. In this example I am exiting order 201 with order 208 and linking by way of the m_parentid.
entry_orderid_ = 201
order = IBOrder()
order.m_orderId = 208
order.m_orderType = 'MKT'
order.m_totalQuantity = 1
order.m_action = "SELL"
order.m_transmit = True
order.m_parentid = entry_orderid_
log.info("Placing market exit order. {} {} {}.".format(order.m_action, 1, 'ES'))
print '.....placing order..'
try:
app.con.placeOrder(order.m_orderId, self.contract, order)
except Exception, e:
print e
This executes successfully but in the executions method I have no way of tracking the parent child relationship
ex = msg.execution
parent_order = ex.m_parentid
This results in an exception. It also allows me to exit an entry multiple times. I would think it wouldnt let one close out an already closed trade. I suppose my question is how does one track the parent-child/entry and exits through executions?
You aren't using parentId properly. It's for other types of orders where the parent fill will trigger the child order.
int ParentId [get, set] The order ID of the parent order, used for bracket and auto trailing stop orders.
eg. https://interactivebrokers.github.io/tws-api/bracket_order.html
You also seem to be using ibpy, there is a newer api from IB. http://interactivebrokers.github.io/#
You can't 'link' buy and sell orders like that with the api, you'll have to keep track by yourself. That being said, your satements will show open and close and the commissionReport will show the closed trade profit if possible.