• C#
  • Java
  • VB
  • C++
  • Python
Contact us
Placing Orders

The Next Valid Identifier

Perhaps the most important event received after successfully connecting to the TWS is the IBApi.EWrapper.nextValidId, which is also triggered after invoking the IBApi.EClient.reqIds method. As its name indicates, the nextValidId event provides the next valid identifier needed to place an order. This identifier is nothing more than the next number in the sequence. This means that if there is a single client application submitting orders to an account, it does not have to obtain a new valid identifier every time it needs to submit a new order. It is enough to increase the last value received from the nextValidId method by one. For example, if the valid identifier for your first API order is 1, the next valid identifier would be 2 and so on.

However if there are multiple client applications connected to one account, it is necessary to use an order ID with new orders which is greater than all previous order IDs returned to the client application in openOrder or orderStatus callbacks. For instance, if the client is set as the Master client, it will automatically receive order status and trade callbacks from orders placed from other clients. In such a case, any orderID used in placeOrder must be greater than the orderIDs returned in these status callbacks. Alternatively if the function reqAllOpenOrders is used by a client, subsequent orders placed by that client must have order IDs greater than the order IDs of all orders returned because of that function call. You can always use the IBApi.EClient.reqIds method in the event that your client application loses track of the sequence.

  • //The parameter is always ignored.
    client.reqIds(-1);
  • //The parameter is always ignored.
    client.reqIds(-1);
  • 'The parameter Is always ignored.
    client.reqIds(-1)
  • //The parameter is always ignored.
    m_pClient->reqIds(-1);
  • 1  # The parameter is always ignored.
    2  self.reqIds(-1)

The above will result in IBApi.EWrapper.nextValidId callback being invoked:

  • public class EWrapperImpl : EWrapper
    {
    ...
    public virtual void nextValidId(int orderId)
    {
    Console.WriteLine("Next Valid Id: "+orderId);
    NextOrderId = orderId;
    }
  • public class EWrapperImpl implements EWrapper {
    ...
    @Override
    public void nextValidId(int orderId) {
    System.out.println("Next Valid Id: ["+orderId+"]");
    currentOrderId = orderId;
    }
  • Public Class EWrapperImpl
    Implements EWrapper
    ...
    Public Sub nextValidId(orderId As Integer) Implements IBApi.EWrapper.nextValidId
    Console.WriteLine("NextValidId - OrderId [" & orderId & "]")
    nextOrderId = orderId
    End Sub
  • class TestCppClient : public EWrapper
    {
    ...
    void TestCppClient::nextValidId( OrderId orderId)
    {
    printf("Next Valid Id: %ld\n", orderId);
    m_orderId = orderId;
  • 1 class TestWrapper(wrapper.EWrapper):
    ...
    1  def nextValidId(self, orderId: int):
    2  super().nextValidId(orderId)
    3 
    4  logging.debug("setting nextValidOrderId: %d", orderId)
    5  self.nextValidOrderId = orderId

The next valid identifier is persistent between TWS sessions.

If necessary, you can reset the order ID sequence within the API Settings dialogue. Note however that the order sequence Id can only be reset if there are no active API orders.

order_id_reset.png

Placing Orders

Orders are submitted via the IBApi.EClient.placeOrder method. From the snippet below, note how a variable holding the nextValidId is incremented automatically:

  • client.placeOrder(nextOrderId++, ContractSamples.USStock(), OrderSamples.TrailingStopLimit("BUY", 1, 5, 5, 110));
  • client.placeOrder(nextOrderId++, ContractSamples.USStock(), OrderSamples.LimitOrder("SELL", 1, 50));
  • client.placeOrder(increment(nextOrderId), ContractSamples.USStock(), OrderSamples.LimitOrder("SELL", 1, 50))
  • m_pClient->placeOrder(m_orderId++, ContractSamples::USStock(), OrderSamples::LimitOrder("SELL", 1, 50));
  • 1  self.simplePlaceOid = self.nextOrderId()
    2  self.placeOrder(self.simplePlaceOid, ContractSamples.USStock(),
    3  OrderSamples.LimitOrder("SELL", 1, 50))

Immediately after the order was submitted correctly, the TWS will start sending events concerning the order's activity via IBApi.EWrapper.openOrder and IBApi.EWrapper.orderStatus

  • An order can be sent to TWS but not transmitted to the IB server by setting the IBApi.Order.Transmit flag in the order class to False. Untransmitted orders will only be available within that TWS session (not for other usernames) and will be cleared on restart. Also, they can be cancelled or transmitted from the API but not viewed while they remain in the "untransmitted" state.

The openOrder callback

The IBApi.EWrapper.openOrder method delivers an IBApi.Order object representing the open order within the system. Additionally, an IBApi.OrderState object containing margin and commission information about this particular order.

  • public class EWrapperImpl : EWrapper
    {
    ...
    public virtual void openOrder(int orderId, Contract contract, Order order, OrderState orderState)
    {
    Console.WriteLine("OpenOrder. ID: "+orderId+", "+contract.Symbol+", "+contract.SecType+" @ "+contract.Exchange+": "+order.Action+", "+order.OrderType+" "+order.TotalQuantity+", "+orderState.Status);
    if (order.WhatIf)
    {
    Console.WriteLine("What-If. ID: " + orderId +
    ", InitMarginBefore: " + Util.formatDoubleString(orderState.InitMarginBefore) + ", MaintMarginBefore: " + Util.formatDoubleString(orderState.MaintMarginBefore) + " EquityWithLoanBefore: " + Util.formatDoubleString(orderState.EquityWithLoanBefore) +
    ", InitMarginChange: " + Util.formatDoubleString(orderState.InitMarginChange) + ", MaintMarginChange: " + Util.formatDoubleString(orderState.MaintMarginChange) + " EquityWithLoanChange: " + Util.formatDoubleString(orderState.EquityWithLoanChange) +
    ", InitMarginAfter: " + Util.formatDoubleString(orderState.InitMarginAfter) + ", MaintMarginAfter: " + Util.formatDoubleString(orderState.MaintMarginAfter) + " EquityWithLoanAfter: " + Util.formatDoubleString(orderState.EquityWithLoanAfter));
    }
    }
  • 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));
    }
  • 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. ID: " & orderId & ", " & contract.Symbol & ", " & contract.SecType & " @ " & contract.Exchange &
    ": " & order.Action & ", " & order.OrderType & " " & order.TotalQuantity & ", " & orderState.Status)
    If order.WhatIf = True Then
    Console.WriteLine("What-If. ID: " & orderId &
    ", InitMarginBefore: " & Util.formatDoubleString(orderState.InitMarginBefore) & ", MaintMarginBefore: " & Util.formatDoubleString(orderState.MaintMarginBefore) & " EquityWithLoanBefore: " & Util.formatDoubleString(orderState.EquityWithLoanBefore) &
    ", InitMarginChange: " & Util.formatDoubleString(orderState.InitMarginChange) & ", MaintMarginChange: " & Util.formatDoubleString(orderState.MaintMarginChange) & " EquityWithLoanChange: " & Util.formatDoubleString(orderState.EquityWithLoanChange) &
    ", InitMarginAfter: " & Util.formatDoubleString(orderState.InitMarginAfter) & ", MaintMarginAfter: " & Util.formatDoubleString(orderState.MaintMarginAfter) & " EquityWithLoanAfter: " & Util.formatDoubleString(orderState.EquityWithLoanAfter))
    End If
    End Sub
  • class TestCppClient : public EWrapper
    {
    ...
    void TestCppClient::openOrder( OrderId orderId, const Contract& contract, const Order& order, const OrderState& ostate) {
    printf( "OpenOrder. ID: %ld, %s, %s @ %s: %s, %s, %g, %g, %s, %s\n", orderId, 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, ostate.status.c_str(), order.dontUseAutoPriceForHedge ? "true" : "false");
    if (order.whatIf) {
    printf( "What-If. ID: %ld, InitMarginBefore: %s, MaintMarginBefore: %s, EquityWithLoanBefore: %s, InitMarginChange: %s, MaintMarginChange: %s, EquityWithLoanChange: %s, InitMarginAfter: %s, MaintMarginAfter: %s, EquityWithLoanAfter: %s\n",
    orderId, Utils::formatDoubleString(ostate.initMarginBefore).c_str(), Utils::formatDoubleString(ostate.maintMarginBefore).c_str(), Utils::formatDoubleString(ostate.equityWithLoanBefore).c_str(),
    Utils::formatDoubleString(ostate.initMarginChange).c_str(), Utils::formatDoubleString(ostate.maintMarginChange).c_str(), Utils::formatDoubleString(ostate.equityWithLoanChange).c_str(),
    Utils::formatDoubleString(ostate.initMarginAfter).c_str(), Utils::formatDoubleString(ostate.maintMarginAfter).c_str(), Utils::formatDoubleString(ostate.equityWithLoanAfter).c_str());
    }
    }
  • 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. ID:", orderId, contract.symbol, contract.secType,
    5  "@", contract.exchange, ":", order.action, order.orderType,
    6  order.totalQuantity, orderState.status)

The orderStatus callback

The IBApi.EWrapper.orderStatus method contains all relevant information on the current status of the order execution-wise (i.e. amount filled and pending, filling price, etc.).

  • public class EWrapperImpl : EWrapper
    {
    ...
    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 class EWrapperImpl implements EWrapper {
    ...
    @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);
    }
  • Public Class EWrapperImpl
    Implements EWrapper
    ...
    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
  • class TestCppClient : public EWrapper
    {
    ...
    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);
    }
  • 1 class TestWrapper(wrapper.EWrapper):
    ...
    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)
    12 

Automatic Order Status Messages (without invoking reqOpenOrders or reqAllOpenOrders)

  • Clients with the ID of the client submitting the order will receive order status messages indicating changes in the order status.
  • The client with Master Client ID (set in TWS/IBG) will receive order status messages for all clients.
  • Client ID 0 will receive order status messages for its own (client ID 0) orders and also for orders submitted manually from TWS.

Possible Order States

  • ApiPending - indicates order has not yet been sent to IB server, for instance if there is a delay in receiving the security definition. Uncommonly received.
  • PendingSubmit - indicates the order was sent from TWS, but confirmation has not been received that it has been received by the destination. Most commonly because exchange is closed.
  • PendingCancel - indicates that a request has been sent to cancel an order but confirmation has not been received of its cancellation.
  • PreSubmitted - indicates that a simulated order type has been accepted by the IB system and that this order has yet to be elected. The order is held in the IB system until the election criteria are met. At that time the order is transmitted to the order destination as specified.
  • Submitted - indicates that your order has been accepted at the order destination and is working.
  • ApiCancelled - after an order has been submitted and before it has been acknowledged, an API client client can request its cancellation, producing this state.
  • Cancelled - indicates that the balance of your order has been confirmed cancelled by the IB system. This could occur unexpectedly when IB or the destination has rejected your order.
  • Filled - indicates that the order has been completely filled.
  • Inactive - indicates an order is not working, possible reasons include:
    • it is invalid or triggered an error. A corresponding error code is expected to the error() function.
    • the order is to short shares but the order is being held while shares are being located.
    • an order is placed manually in TWS while the exchange is closed.
    • an order is blocked by TWS due to a precautionary setting and appears there in an untransmitted state

Important notes concerning IBApi.EWrapper.orderStatus :

  • Typically there are duplicate orderStatus messages with the same information that will be received by a client. This corresponds to messages sent back from TWS, the IB server, or the exchange.
  • There are not guaranteed to be orderStatus callbacks for every change in order status. For example with market orders when the order is accepted and executes immediately, there commonly will not be any corresponding orderStatus callbacks. For that reason it is recommended to monitor the IBApi.EWrapper.execDetails function in addition to IBApi.EWrapper.orderStatus.
  • Beginning in API v973.04, a parameter mktCapPrice is included in the orderStatus callback. If an order has been price-capped, mktCapPrice will indicate the price at which it has been capped.

Attaching Orders

Advanced orders such as Bracket Orders or Hedging involve attaching child orders to a parent. This can be easily done via the IBApi.Order.ParentId attribute by assigning a child order's IBApi.Order.ParentId to an existing order's IBApi.Order.OrderId. When an order is attached to another, the system will keep the child order 'on hold' until its parent fills. Once the parent order is completely filled, its children will automatically become active.

Important: When attaching orders and to prevent accidental executions it is a very good idea to make use of the IBApi.Order.Transmit flag as demonstrated in Bracket Orders