• 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: " + Util.IntMaxString(order.PermId) + ", ClientId: " + Util.IntMaxString(order.ClientId) + ", OrderId: " + Util.IntMaxString(orderId) +
    ", Account: " + order.Account + ", Symbol: " + contract.Symbol + ", SecType: " + contract.SecType + " , Exchange: " + contract.Exchange + ", Action: " + order.Action +
    ", OrderType: " + order.OrderType + ", TotalQty: " + Util.DecimalMaxString(order.TotalQuantity) + ", CashQty: " + Util.DoubleMaxString(order.CashQty) +
    ", LmtPrice: " + Util.DoubleMaxString(order.LmtPrice) + ", AuxPrice: " + Util.DoubleMaxString(order.AuxPrice) + ", Status: " + orderState.Status +
    ", MinTradeQty: " + Util.IntMaxString(order.MinTradeQty) + ", MinCompeteSize: " + Util.IntMaxString(order.MinCompeteSize) +
    ", CompeteAgainstBestOffset: " + (order.CompeteAgainstBestOffset == Order.COMPETE_AGAINST_BEST_OFFSET_UP_TO_MID ? "UpToMid" : Util.DoubleMaxString(order.CompeteAgainstBestOffset)) +
    ", MidOffsetAtWhole: " + Util.DoubleMaxString(order.MidOffsetAtWhole) + ", MidOffsetAtHalf: " + Util.DoubleMaxString(order.MidOffsetAtHalf) +
    ", FAGroup: " + order.FaGroup + ", FAMethod: " + order.FaMethod);
    }
    ...
    public virtual void orderStatus(int orderId, string status, decimal filled, decimal remaining, double avgFillPrice, int permId, int parentId, double lastFillPrice, int clientId, string whyHeld, double mktCapPrice)
    {
    Console.WriteLine("OrderStatus. Id: " + orderId + ", Status: " + status + ", Filled: " + Util.DecimalMaxString(filled) + ", Remaining: " + Util.DecimalMaxString(remaining)
    + ", AvgFillPrice: " + Util.DoubleMaxString(avgFillPrice) + ", PermId: " + Util.IntMaxString(permId) + ", ParentId: " + Util.IntMaxString(parentId) +
    ", LastFillPrice: " + Util.DoubleMaxString(lastFillPrice) + ", ClientId: " + Util.IntMaxString(clientId) + ", WhyHeld: " + whyHeld + ", MktCapPrice: " + Util.DoubleMaxString(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, Decimal filled, Decimal remaining, double avgFillPrice, int permId, int parentId,
    double lastFillPrice, int clientId, String whyHeld, double mktCapPrice) {
    System.out.println(EWrapperMsgGenerator.orderStatus( orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice));
    }
    ...
    @Override
    public void openOrderEnd() {
    System.out.println("Open Order End: " + EWrapperMsgGenerator.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: " & Util.IntMaxString(order.PermId) & ", ClientId: " & Util.IntMaxString(order.ClientId) & ", OrderId: " & Util.IntMaxString(orderId) &
    ", Account: " & order.Account & ", Symbol: " & contract.Symbol & ", SecType: " & contract.SecType & " , Exchange: " & contract.Exchange & ", Action: " & order.Action &
    ", OrderType: " & order.OrderType & ", TotalQty: " & Util.DecimalMaxString(order.TotalQuantity) & ", CashQty: " & Util.DoubleMaxString(order.CashQty) & ", LmtPrice: " &
    Util.DoubleMaxString(order.LmtPrice) & ", AuxPrice: " & Util.DoubleMaxString(order.AuxPrice) & ", Status: " & orderState.Status &
    ", MinTradeQty: " & Util.IntMaxString(order.MinTradeQty) & ", MinCompeteSize: " & Util.IntMaxString(order.MinCompeteSize) &
    ", CompeteAgainstBestOffset: " & If(order.CompeteAgainstBestOffset = Order.COMPETE_AGAINST_BEST_OFFSET_UP_TO_MID, "UpToMid", Util.DoubleMaxString(order.CompeteAgainstBestOffset)) &
    ", MidOffsetAtWhole: " & Util.DoubleMaxString(order.MidOffsetAtWhole) & ", MidOffsetAtHalf: " & Util.DoubleMaxString(order.MidOffsetAtHalf) &
    ", FAGroup: " & order.FaGroup & ", FAMethod: " & order.FaMethod)
    End Sub
    ...
    Public Sub orderStatus(orderId As Integer, status As String, filled As Decimal, remaining As Decimal, 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: " & Util.DecimalMaxString(filled) & ", Remaining: " & Util.DecimalMaxString(remaining) &
    ", AvgFillPrice: " & Util.DoubleMaxString(avgFillPrice) & ", PermId: " & Util.IntMaxString(permId) & ", ParentId: " & Util.IntMaxString(parentId) &
    ", LastFillPrice: " & Util.DoubleMaxString(lastFillPrice) & ", ClientId: " & Util.IntMaxString(clientId) & ", WhyHeld: " & whyHeld & ", mktCapPrice: " & Util.DoubleMaxString(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: %s, ClientId: %s, OrderId: %s, Account: %s, Symbol: %s, SecType: %s, Exchange: %s:, Action: %s, OrderType:%s, TotalQty: %s, CashQty: %s, "
    "LmtPrice: %s, AuxPrice: %s, Status: %s, MinTradeQty: %s, MinCompeteSize: %s, CompeteAgainstBestOffset: %s, MidOffsetAtWhole: %s, MidOffsetAtHalf: %s, "
    "FAGroup: %s, FAMethod: %s\n",
    Utils::intMaxString(order.permId).c_str(), Utils::longMaxString(order.clientId).c_str(), Utils::longMaxString(orderId).c_str(), order.account.c_str(), contract.symbol.c_str(),
    contract.secType.c_str(), contract.exchange.c_str(), order.action.c_str(), order.orderType.c_str(), DecimalFunctions::decimalStringToDisplay(order.totalQuantity).c_str(),
    Utils::doubleMaxString(order.cashQty).c_str(), Utils::doubleMaxString(order.lmtPrice).c_str(), Utils::doubleMaxString(order.auxPrice).c_str(), orderState.status.c_str(),
    Utils::intMaxString(order.minTradeQty).c_str(), Utils::intMaxString(order.minCompeteSize).c_str(),
    order.competeAgainstBestOffset == COMPETE_AGAINST_BEST_OFFSET_UP_TO_MID ? "UpToMid" : Utils::doubleMaxString(order.competeAgainstBestOffset).c_str(),
    Utils::doubleMaxString(order.midOffsetAtWhole).c_str(), Utils::doubleMaxString(order.midOffsetAtHalf).c_str(),
    order.faGroup.c_str(), order.faMethod.c_str());
    }
    ...
    void TestCppClient::orderStatus(OrderId orderId, const std::string& status, Decimal filled,
    Decimal remaining, double avgFillPrice, int permId, int parentId,
    double lastFillPrice, int clientId, const std::string& whyHeld, double mktCapPrice){
    printf("OrderStatus. Id: %ld, Status: %s, Filled: %s, Remaining: %s, AvgFillPrice: %s, PermId: %s, LastFillPrice: %s, ClientId: %s, WhyHeld: %s, MktCapPrice: %s\n",
    orderId, status.c_str(), DecimalFunctions::decimalStringToDisplay(filled).c_str(), DecimalFunctions::decimalStringToDisplay(remaining).c_str(), Utils::doubleMaxString(avgFillPrice).c_str(), Utils::intMaxString(permId).c_str(),
    Utils::doubleMaxString(lastFillPrice).c_str(), Utils::intMaxString(clientId).c_str(), whyHeld.c_str(), Utils::doubleMaxString(mktCapPrice).c_str());
    }
    ...
    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:", intMaxString(order.permId), "ClientId:", intMaxString(order.clientId), " OrderId:", intMaxString(orderId),
    5  "Account:", order.account, "Symbol:", contract.symbol, "SecType:", contract.secType,
    6  "Exchange:", contract.exchange, "Action:", order.action, "OrderType:", order.orderType,
    7  "TotalQty:", decimalMaxString(order.totalQuantity), "CashQty:", floatMaxString(order.cashQty),
    8  "LmtPrice:", floatMaxString(order.lmtPrice), "AuxPrice:", floatMaxString(order.auxPrice), "Status:", orderState.status,
    9  "MinTradeQty:", intMaxString(order.minTradeQty), "MinCompeteSize:", intMaxString(order.minCompeteSize),
    10  "competeAgainstBestOffset:", "UpToMid" if order.competeAgainstBestOffset == COMPETE_AGAINST_BEST_OFFSET_UP_TO_MID else floatMaxString(order.competeAgainstBestOffset),
    11  "MidOffsetAtWhole:", floatMaxString(order.midOffsetAtWhole),"MidOffsetAtHalf:" ,floatMaxString(order.midOffsetAtHalf),
    12  "FAGroup:", order.faGroup, "FAMethod:", order.faMethod)
    13 
    14  order.contract = contract
    15  self.permId2ord[order.permId] = order
    ...
    1  def orderStatus(self, orderId: OrderId, status: str, filled: Decimal,
    2  remaining: Decimal, 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:", decimalMaxString(filled),
    8  "Remaining:", decimalMaxString(remaining), "AvgFillPrice:", floatMaxString(avgFillPrice),
    9  "PermId:", intMaxString(permId), "ParentId:", intMaxString(parentId), "LastFillPrice:",
    10  floatMaxString(lastFillPrice), "ClientId:", intMaxString(clientId), "WhyHeld:",
    11  whyHeld, "MktCapPrice:", floatMaxString(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}", Util.LongMaxString(orderId), Util.IntMaxString(apiClientId), Util.IntMaxString(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}", Util.LongMaxString(orderId), Util.IntMaxString(apiClientId), Util.IntMaxString(apiOrderId))
    End Sub
  • void TestCppClient::orderBound(long long orderId, int apiClientId, int apiOrderId) {
    printf("Order bound. OrderId: %s, ApiClientId: %s, ApiOrderId: %s\n", Utils::llongMaxString(orderId).c_str(), Utils::intMaxString(apiClientId).c_str(), Utils::intMaxString(apiOrderId).c_str());
    }
  • 1  def orderBound(self, orderId: int, apiClientId: int, apiOrderId: int):
    2  super().orderBound(orderId, apiClientId, apiOrderId)
    3  print("OrderBound.", "OrderId:", intMaxString(orderId), "ApiClientId:", intMaxString(apiClientId), "ApiOrderId:", intMaxString(apiOrderId))