• C#
  • Java
  • VB
  • C++
  • Python
Contact us
Retrieving currently active orders

As long as an order is active, it is possible to retrieve it using the TWS API. Orders submitted via the TWS API will always be bound to the client application (i.e. client Id) they were submitted from meaning only the submitting client will be able to modify the placed order. Three different methods are provided to allow for maximum flexibility. Active orders will be returned via the IBApi.EWrapper.openOrder and IBApi.EWrapper.orderStatus methods as already described in The openOrder callback and The orderStatus callback sections

Note: it is not possible to obtain cancelled or fully filled orders.

API client's orders

The IBApi.EClient.reqOpenOrders method allows to obtain all active orders submitted by the client application connected with the exact same client Id with which the order was sent to the TWS. If client 0 invokes reqOpenOrders, it will cause currently open orders placed from TWS manually to be 'bound', i.e. assigned an order ID so that they can be modified or cancelled by the API client 0. In API versions after 973.07 there will be IBApi.EWrapper.orderBound callback in response to newly bound orders that indicates the mapping between the permID (unique account-wide) and API Order ID (specific to an API client). In the API settings in Global Configuration, is a setting checked by default "Use negative numbers to bind automatic orders" which will specify how manual TWS orders are assigned an API order ID.

  • client.reqOpenOrders();
  • client.reqOpenOrders();
  • client.reqOpenOrders()
  • m_pClient->reqOpenOrders();
  • 1  self.reqOpenOrders()

All submitted orders

To obtain those orders created via the TWS API regardless of the submitting client application, make use of the IBApi.EClient.reqAllOpenOrders function.

  • client.reqAllOpenOrders();
  • client.reqAllOpenOrders();
  • client.reqAllOpenOrders()
  • m_pClient->reqAllOpenOrders();
  • 1  self.reqAllOpenOrders()

Manually submitted orders

Finally, IBApi.EClient.reqAutoOpenOrders can only be invoked by client with ID 0. It will cause future orders placed from TWS to be 'bound', i.e. assigned an order ID such that they can be accessed by the cancelOrder or placeOrder (for modification) functions by client ID 0.

  • client.reqAutoOpenOrders(true);
  • client.reqAutoOpenOrders(true);
  • client.reqAutoOpenOrders(True)
  • m_pClient->reqAutoOpenOrders(true);
  • 1  self.reqAutoOpenOrders(True)

Important: only those applications connecting with client Id 0 will be able to take over manually submitted orders

Through the TWS' API settings it is possible to configure this method's behaviour to some extent. As shown in the image below, manually placed orders can be given a negative order Id which can serve to easily tell manual from API submitted orders. The TWS' tooltip elaborates further:

tws_autobind.png

Receiving Order Information

Active orders will be delivered via The openOrder callback and The orderStatus callback callbacks. When all orders have been sent to the client application you will receive a IBApi.EWrapper.openOrderEnd event:

  • public class EWrapperImpl : EWrapper
    {
    ...
    public virtual void openOrder(int orderId, Contract contract, Order order, OrderState orderState)
    {
    Console.WriteLine("OpenOrder. PermID: " + order.PermId + ", ClientId: " + order.ClientId + ", OrderId: " + orderId + ", Account: " + order.Account +
    ", Symbol: " + contract.Symbol + ", SecType: " + contract.SecType + " , Exchange: " + contract.Exchange + ", Action: " + order.Action + ", OrderType: " + order.OrderType +
    ", TotalQty: " + order.TotalQuantity + ", CashQty: " + order.CashQty + ", LmtPrice: " + order.LmtPrice + ", AuxPrice: " + order.AuxPrice + ", Status: " + orderState.Status);
    }
    ...
    public virtual void orderStatus(int orderId, string status, double filled, double remaining, double avgFillPrice, int permId, int parentId, double lastFillPrice, int clientId, string whyHeld, double mktCapPrice)
    {
    Console.WriteLine("OrderStatus. Id: " + orderId + ", Status: " + status + ", Filled: " + filled + ", Remaining: " + remaining
    + ", AvgFillPrice: " + avgFillPrice + ", PermId: " + permId + ", ParentId: " + parentId + ", LastFillPrice: " + lastFillPrice + ", ClientId: " + clientId + ", WhyHeld: " + whyHeld + ", MktCapPrice: " + mktCapPrice);
    }
    ...
    public virtual void openOrderEnd()
    {
    Console.WriteLine("OpenOrderEnd");
    }
  • public class EWrapperImpl implements EWrapper {
    ...
    @Override
    public void openOrder(int orderId, Contract contract, Order order,
    OrderState orderState) {
    System.out.println(EWrapperMsgGenerator.openOrder(orderId, contract, order, orderState));
    }
    ...
    @Override
    public void orderStatus(int orderId, String status, double filled,
    double remaining, double avgFillPrice, int permId, int parentId,
    double lastFillPrice, int clientId, String whyHeld, double mktCapPrice) {
    System.out.println("OrderStatus. Id: "+orderId+", Status: "+status+", Filled"+filled+", Remaining: "+remaining
    +", AvgFillPrice: "+avgFillPrice+", PermId: "+permId+", ParentId: "+parentId+", LastFillPrice: "+lastFillPrice+
    ", ClientId: "+clientId+", WhyHeld: "+whyHeld+", MktCapPrice: "+mktCapPrice);
    }
    ...
    @Override
    public void openOrderEnd() {
    System.out.println("OpenOrderEnd");
    }
  • Public Class EWrapperImpl
    Implements EWrapper
    ...
    Public Sub openOrder(orderId As Integer, contract As IBApi.Contract, order As IBApi.Order, orderState As IBApi.OrderState) Implements IBApi.EWrapper.openOrder
    Console.WriteLine("OpenOrder. PermID: " & order.PermId & ", ClientId: " & order.ClientId & ", OrderId: " & orderId & ", Account: " & order.Account &
    ", Symbol: " & contract.Symbol & ", SecType: " & contract.SecType & " , Exchange: " & contract.Exchange & ", Action: " & order.Action & ", OrderType: " & order.OrderType &
    ", TotalQty: " & order.TotalQuantity & ", CashQty: " & order.CashQty & ", LmtPrice: " & order.LmtPrice & ", AuxPrice: " & order.AuxPrice & ", Status: " & orderState.Status)
    End Sub
    ...
    Public Sub orderStatus(orderId As Integer, status As String, filled As Double, remaining As Double, avgFillPrice As Double, permId As Integer, parentId As Integer, lastFillPrice As Double, clientId As Integer, whyHeld As String, mktCapPrice As Double) Implements IBApi.EWrapper.orderStatus
    Console.WriteLine("OrderStatus. Id: " & orderId & ", Status: " & status & ", Filled: " & filled & ", Remaining: " & remaining &
    ", AvgFillPrice: " & avgFillPrice & ", PermId: " & permId & ", ParentId: " & parentId & ", LastFillPrice: " & lastFillPrice &
    ", ClientId: " & clientId & ", WhyHeld: " & whyHeld & ", mktCapPrice: " & mktCapPrice)
    End Sub
    ...
    Public Sub openOrderEnd() Implements IBApi.EWrapper.openOrderEnd
    Console.WriteLine("OpenOrderEnd")
    End Sub
  • class TestCppClient : public EWrapper
    {
    ...
    void TestCppClient::openOrder( OrderId orderId, const Contract& contract, const Order& order, const OrderState& orderState) {
    printf( "OpenOrder. PermId: %i, ClientId: %ld, OrderId: %ld, Account: %s, Symbol: %s, SecType: %s, Exchange: %s:, Action: %s, OrderType:%s, TotalQty: %g, CashQty: %g, "
    "LmtPrice: %g, AuxPrice: %g, Status: %s\n",
    order.permId, order.clientId, orderId, order.account.c_str(), contract.symbol.c_str(), contract.secType.c_str(), contract.exchange.c_str(),
    order.action.c_str(), order.orderType.c_str(), order.totalQuantity, order.cashQty == UNSET_DOUBLE ? 0 : order.cashQty, order.lmtPrice, order.auxPrice, orderState.status.c_str());
    }
    ...
    void TestCppClient::orderStatus(OrderId orderId, const std::string& status, double filled,
    double remaining, double avgFillPrice, int permId, int parentId,
    double lastFillPrice, int clientId, const std::string& whyHeld, double mktCapPrice){
    printf("OrderStatus. Id: %ld, Status: %s, Filled: %g, Remaining: %g, AvgFillPrice: %g, PermId: %d, LastFillPrice: %g, ClientId: %d, WhyHeld: %s, MktCapPrice: %g\n", orderId, status.c_str(), filled, remaining, avgFillPrice, permId, lastFillPrice, clientId, whyHeld.c_str(), mktCapPrice);
    }
    ...
    void TestCppClient::openOrderEnd() {
    printf( "OpenOrderEnd\n");
    }
  • 1 class TestWrapper(wrapper.EWrapper):
    ...
    1  def openOrder(self, orderId: OrderId, contract: Contract, order: Order,
    2  orderState: OrderState):
    3  super().openOrder(orderId, contract, order, orderState)
    4  print("OpenOrder. PermId: ", order.permId, "ClientId:", order.clientId, " OrderId:", orderId,
    5  "Account:", order.account, "Symbol:", contract.symbol, "SecType:", contract.secType,
    6  "Exchange:", contract.exchange, "Action:", order.action, "OrderType:", order.orderType,
    7  "TotalQty:", order.totalQuantity, "CashQty:", order.cashQty,
    8  "LmtPrice:", order.lmtPrice, "AuxPrice:", order.auxPrice, "Status:", orderState.status)
    9 
    10  order.contract = contract
    11  self.permId2ord[order.permId] = order
    ...
    1  def orderStatus(self, orderId: OrderId, status: str, filled: float,
    2  remaining: float, avgFillPrice: float, permId: int,
    3  parentId: int, lastFillPrice: float, clientId: int,
    4  whyHeld: str, mktCapPrice: float):
    5  super().orderStatus(orderId, status, filled, remaining,
    6  avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice)
    7  print("OrderStatus. Id:", orderId, "Status:", status, "Filled:", filled,
    8  "Remaining:", remaining, "AvgFillPrice:", avgFillPrice,
    9  "PermId:", permId, "ParentId:", parentId, "LastFillPrice:",
    10  lastFillPrice, "ClientId:", clientId, "WhyHeld:",
    11  whyHeld, "MktCapPrice:", mktCapPrice)
    ...
    1  def openOrderEnd(self):
    2  super().openOrderEnd()
    3  print("OpenOrderEnd")
    4 
    5  logging.debug("Received %d openOrders", len(self.permId2ord))


Order Binding Notification

When an order is bound by API client 0 there will be callback to IBApi::EWrapper::orderBound.
This indicates the mapping between API order ID and permID.
**not yet implemented

  • public void orderBound(long orderId, int apiClientId, int apiOrderId)
    {
    Console.WriteLine("Order bound. Order Id: {0}, Api Client Id: {1}, Api Order Id: {2}", orderId, apiClientId, apiOrderId);
    }
  • @Override
    public void orderBound(long orderId, int apiClientId, int apiOrderId) {
    System.out.println(EWrapperMsgGenerator.orderBound(orderId, apiClientId, apiOrderId));
    }
  • Public Sub orderBound(orderId As Long, apiClientId As Integer, apiOrderId As Integer) Implements EWrapper.orderBound
    Console.WriteLine("Order bound. Order Id: {0}, Api Client Id: {1}, Api Order Id: {2}", orderId, apiClientId, apiOrderId)
    End Sub
  • void TestCppClient::orderBound(long long orderId, int apiClientId, int apiOrderId) {
    printf("Order bound. OrderId: %lld, ApiClientId: %d, ApiOrderId: %d\n", orderId, apiClientId, apiOrderId);
    }
  • 1  def orderBound(self, orderId: int, apiClientId: int, apiOrderId: int):
    2  super().orderBound(orderId, apiClientId, apiOrderId)
    3  print("OrderBound.", "OrderId:", orderId, "ApiClientId:", apiClientId, "ApiOrderId:", apiOrderId)