I need help , Mql4. Orderclose and orderdelete

Ümit UYSAL  
hello algo traders.

Consider 10 buystop orders stacked at a certain interval. The stop loss level of each buystop order will be the same as the buystop order level before it. As the price rises, it will take the buystop orders one by one, starting from the bottom, and turn them into a buy transaction.

For example, 4 out of 10 buystop orders turned into buys and the price started to drop from there. When the last buy trade at the top is stop, I want all open buy trades to be closed and undelivered buystop orders to be deleted.

I wrote a lot of code for this but I ran into some problems. For example, buystop deleted some of the orders and some remained.

I do not know if such a study has been done before, but I request you to help me in this regard.