• C#
  • Java
  • VB
  • C++
  • Python
Contact us
One Cancels All

The One-Cancels All (OCA) order type allows an investor to place multiple and possibly unrelated orders assigned to a group. The aim is to complete just one of the orders, which in turn will cause TWS to cancel the remaining orders. The investor may submit several orders aimed at taking advantage of the most desirable price within the group. Completion of one piece of the group order causes cancellation of the remaining group orders while partial completion causes the group to re-balance. An investor might desire to sell 1000 shares of only ONE of three positions held above prevailing market prices. The OCA order group allows the investor to enter prices at specified target levels and if one is completed, the other two will automatically cancel. Alternatively, an investor may wish to take a LONG position in eMini S&P stock index futures in a falling market or else SELL US treasury futures at a more favorable price. Grouping the two orders using an OCA order type offers the investor two chances to enter a similar position, while only running the risk of taking on a single position.

  • public static List<Order> OneCancelsAll(string ocaGroup, List<Order> ocaOrders, int ocaType)
    {
    foreach (Order o in ocaOrders)
    {
    o.OcaGroup = ocaGroup;
    o.OcaType = ocaType;
    }
    return ocaOrders;
    }
    ...
    List<Order> ocaOrders = new List<Order>();
    ocaOrders.Add(OrderSamples.LimitOrder("BUY", 1, 10));
    ocaOrders.Add(OrderSamples.LimitOrder("BUY", 1, 11));
    ocaOrders.Add(OrderSamples.LimitOrder("BUY", 1, 12));
    OrderSamples.OneCancelsAll("TestOCA_" + nextOrderId, ocaOrders, 2);
    foreach (Order o in ocaOrders)
    client.placeOrder(nextOrderId++, ContractSamples.USStock(), o);
  • public static List<Order> OneCancelsAll(String ocaGroup, List<Order> ocaOrders, int ocaType) {
    for (Order o : ocaOrders) {
    o.ocaGroup(ocaGroup);
    o.ocaType(ocaType);
    }
    return ocaOrders;
    }
    ...
    List<Order> OcaOrders = new ArrayList<>();
    OcaOrders.add(OrderSamples.LimitOrder("BUY", 1, 10));
    OcaOrders.add(OrderSamples.LimitOrder("BUY", 1, 11));
    OcaOrders.add(OrderSamples.LimitOrder("BUY", 1, 12));
    OcaOrders = OrderSamples.OneCancelsAll("TestOCA_" + nextOrderId, OcaOrders, 2);
    for (Order o : OcaOrders) {
    client.placeOrder(nextOrderId++, ContractSamples.USStock(), o);
    }
  • For Each o As Order In ocaOrders
    o.OcaGroup = ocaGroup
    o.OcaType = ocaType
    'Same as with Bracket orders. To prevent accidental executions, set all orders' transmit flag to false.
    'This will tell the TWS Not to send the orders, allowing your program to send them all first.
    o.Transmit = False
    Next o
    'Telling the TWS to transmit the last order in the OCA will also cause the transmission of its predecessors.
    ocaOrders.Item(ocaOrders.Count - 1).Transmit = True
    ...
    Dim ocaOrders As List(Of Order) = New List(Of Order)
    ocaOrders.Add(OrderSamples.LimitOrder("BUY", 1, 10))
    ocaOrders.Add(OrderSamples.LimitOrder("BUY", 1, 11))
    ocaOrders.Add(OrderSamples.LimitOrder("BUY", 1, 12))
    OrderSamples.OneCancelsAll("TestOCA_" + nextOrderId, ocaOrders, 2)
    For Each o As Order In ocaOrders
    client.placeOrder(increment(nextOrderId), ContractSamples.USStock(), o)
    Next
  • void OrderSamples::OneCancelsAll(std::string ocaGroup, Order& ocaOrder, int ocaType){
    ocaOrder.ocaGroup = ocaGroup;
    ocaOrder.ocaType = ocaType;
    }
    ...
    std::vector<Order> ocaOrders;
    ocaOrders.push_back(OrderSamples::LimitOrder("BUY", 1, 10));
    ocaOrders.push_back(OrderSamples::LimitOrder("BUY", 1, 11));
    ocaOrders.push_back(OrderSamples::LimitOrder("BUY", 1, 12));
    for(unsigned int i = 0; i < ocaOrders.size(); i++){
    OrderSamples::OneCancelsAll("TestOca", ocaOrders[i], 2);
    m_pClient->placeOrder(m_orderId++, ContractSamples::USStock(), ocaOrders[i]);
    }
  • 1  @staticmethod
    2  def OneCancelsAll(ocaGroup:str, ocaOrders:ListOfOrder, ocaType:int):
    3 
    4  for o in ocaOrders:
    5 
    6  o.ocaGroup = ocaGroup
    7  o.ocaType = ocaType
    8 
    9  return ocaOrders
    10 
    ...
    1  ocaOrders = [OrderSamples.LimitOrder("BUY", 1, 10), OrderSamples.LimitOrder("BUY", 1, 11),
    2  OrderSamples.LimitOrder("BUY", 1, 12)]
    3  OrderSamples.OneCancelsAll("TestOCA_" + str(self.nextValidOrderId), ocaOrders, 2)
    4  for o in ocaOrders:
    5  self.placeOrder(self.nextOrderId(), ContractSamples.USStockAtSmart(), o)

OCA Types

Via the IBApi.Order.OcaType attribute, the way in which remaining orders should be handled after an execution can be configured as indicated in the table below:

ValueDescription
1Cancel all remaining orders with block.*
2Remaining orders are proportionately reduced in size with block.*
3Remaining orders are proportionately reduced in size with no block.

Note*: if you use a value "with block" it gives the order overfill protection. This means that only one order in the group will be routed at a time to remove the possibility of an overfill. Click here for further discussion of OCA orders.