Closing Pending Orders in Grid Trading Robot

I created a Grid strategy that opens several limit orders.

When the trade is opened, it sets a target and stop loss and keeps the other limit orders pending on the screen, which can be filled if the price reaches them.

The idea is that if the price moves against it, it increases the position, averaging down better.

Everything is working.

If there is no open position, the grid orders are repositioned with each bar on the chart.

Problem: I want that when the target is triggered, in addition to closing the trade, the strategy cancels the pending orders, clearing the grid (as if the close button were pressed in ChartTrader), and restarts everything on the next bar refresh.

But I can’t get the pending orders to be canceled when the target is reached.

Keep a reference to every grid order when you submit it (the Order object returned by EnterLongLimit, or captured in OnOrderUpdate by name), stored in a list. Then in OnExecutionUpdate, when the filled order is your profit target (execution.Order.Name == “Profit target” with SetProfitTarget) and Position.MarketPosition is Flat, loop through the list and call CancelOrder() on each order that is still working, then clear the list and set a flag so the grid is rebuilt on the next bar. If the grid uses isLiveUntilCancelled = true, those orders stay alive until you cancel them, so this step is needed. TraceOrders shows each cancel as it happens. Happy to help further, feel free to DM me.