Code Request: Auto-Delete Sub-Orders When Main Order Closes

 

Hello fellow traders,

I’m looking for assistance with a specific code for my trading robot. In my setup, whenever a new pending order is placed, the robot assigns a magic number that is incremented by 1 from the previous order’s magic number. Additionally, along with each new pending order, a secondary order is automatically placed. The entry point for this secondary order is set to the stop loss level of the main order and is part of a martingale strategy. The secondary order shares the same magic number as its corresponding main order.

What I need is a script or function that automatically deletes the secondary (pending) order when the main order reaches its take profit (TP) and closes. The condition is that both orders are tied by the same magic number.

I’d greatly appreciate any guidance or code snippets that could help me implement this feature.

Thanks in advance!