nitinsy
nitinsy

Reputation: 51

IBKR API - How to modify bracket order?

I am using IBKR API to place bracket order using the sample code provided. New bracket order is placed correctly but when I use the same function to modify bracket order, I am getting error "Order being modified does not match original order".

    public static List<Order> BracketOrder(int parentOrderId, string action, decimal quantity, double limitPrice, 
        double takeProfitLimitPrice, double stopLossPrice)
    {
        //This will be our main or "parent" order
        Order parent = new Order();
        parent.OrderId = parentOrderId;
        parent.Action = action;
        parent.OrderType = "LMT";
        parent.TotalQuantity = quantity;
        parent.LmtPrice = limitPrice;
        //The parent and children orders will need this attribute set to false to prevent accidental executions.
        //The LAST CHILD will have it set to true, 
        parent.Transmit = false;
        Order takeProfit = new Order();
        takeProfit.OrderId = parent.OrderId + 1;
        takeProfit.Action = action.Equals("BUY") ? "SELL" : "BUY";
        takeProfit.OrderType = "LMT";
        takeProfit.TotalQuantity = quantity;
        takeProfit.LmtPrice = takeProfitLimitPrice;
        takeProfit.ParentId = parentOrderId;
        takeProfit.Transmit = false;
        Order stopLoss = new Order();
        stopLoss.OrderId = parent.OrderId + 2;
        stopLoss.Action = action.Equals("BUY") ? "SELL" : "BUY";
        stopLoss.OrderType = "STP";
        //Stop trigger price
        stopLoss.AuxPrice = stopLossPrice;
        stopLoss.TotalQuantity = quantity;
        stopLoss.ParentId = parentOrderId;
        //In this case, the low side order will be the last child being sent. Therefore, it needs to set this attribute to true 
        //to activate all its predecessors
        stopLoss.Transmit = true;
        List<Order> bracketOrder = new List<Order>();
        bracketOrder.Add(parent);
        bracketOrder.Add(takeProfit);
        bracketOrder.Add(stopLoss);
        return bracketOrder;
    }

    int ParentOrderId = EWI.NextOrderId;
    int OrderSize = 1;
    List<Order> bracket = BracketOrder(ParentOrderId, "BUY", 100, 30, 40, 20);
    foreach (Order o in bracket)
       client.placeOrder(o.OrderId, Contract, o);
    // Modify bracket order to change TakeProfit
    bracket = BracketOrder(ParentOrderId, "BUY", 100, 30, 50, 20);
    foreach (Order o in bracket)
       client.placeOrder(o.OrderId, Contract, o);

Upvotes: 1

Views: 681

Answers (0)

Related Questions