0

On the Interactive Brokers Java API, I am trying to submit an order parent, with a trailing stop child. Right now I can place one or the other, but not both. How can I submit them both together, at the same time? Am I supposed to create an array and then place order?

public void sendOrder(String orderSide, int parentOrderId) {
    System.out.println("sendOrder is called");
    Order order = new Order();
    parentOrderId = nextOrderID + 1;
    order.orderId(nextOrderID + 1);
    //order.m_action = "BUY";
    order.action(orderSide);
    //order.m_totalQuantity = 1;
    order.totalQuantity(20000);
    //order.m_lmtPrice = 0.01;
    System.out.println("sendOrder, this.high = " + this.high);

    DecimalFormat df = new DecimalFormat("0.00000"); // TODO get digit according
    String formatedEntryPrice = df.format(this.high);

    System.out.println("formatedEntryPrice = " + formatedEntryPrice);

    order.lmtPrice(Double.valueOf("1.00")); // TODO stop limit at prior day high
    //order.auxPrice(this.high);
    //order.auxPrice(Double.valueOf(formatedEntryPrice));

    //order.m_orderType = "LMT";
    order.orderType("LMT");
    //order.m_account = "U1836253";
    order.account("U1836253");
  order.transmit(false);
    System.out.println("after creating order");


     if (orderSide  == "BUY"){
         Order stopLoss = new Order();
          order.action ("SELL");
        stopLoss.orderId(nextOrderID + 1);
        order.orderType ("TRAIL");
        order.totalQuantity (20000);
        order.trailingPercent (1);
        order.trailStopPrice(low);
        stopLoss.parentId(nextOrderID) ;
         order.transmit(true);
     }


     if (orderSide  == "SELL"){
         Order stopLoss = new Order();
          order.action ("BUY");
        stopLoss.orderId(nextOrderID + 1);
        order.orderType ("TRAIL");
        order.totalQuantity (20000);
        order.trailingPercent (1);
        order.trailStopPrice(low);
        stopLoss.parentId(nextOrderID) ;
         order.transmit(true);
     }





    m_s.placeOrder(nextOrderID + 1, contract, order);

    System.out.println("sendOrder is finished");
    nextOrderID = nextOrderID +1;
    client.reqIds(Integer.valueOf("1"));

    System.out.println("sendOrder is finished 2");
}
PlacePrint App
  • 101
  • 1
  • 12

1 Answers1

1

You need to place each order with a separate call to placeOrder. By setting transmit to false on the parent order, IB will hold this order until you submit a child order with transmit = true. IB has an example of submitting a bracket order in their API docs.

Note that your code also appears to be wrong in that you are creating a child order called stoploss but then you're setting its order attributes on order, which is your parent order. I assume you actually want to be setting those attributes on the stop loss order.

Brian from QuantRocket
  • 5,268
  • 1
  • 21
  • 18