step to open more than one order and close all order

 

I want to make an EA that can open trade more than one and at certain point I want to close all the trade. This is my first time dealing with this situation. I made a "step" to do it, does this step to do it correct?

int limitOpenOrder=3;
//open order
for(int i=0;i<limitOpenOrder;i++) //loop to check if ther's open order and if it's under limit open order
   {
    if(OpenCondition)
        BUY;
    if(SellCondition)
        SELL;
   }
//close order
for (int i = (OrdersTotal() - 1); i >= 0; i--) //loop to check all open order to find open order type
   {
    if (OrderType() == OP_BUY)
        if(CloseBuyCondition)
           res = OrderClose(OrderTicket(), OrderLots(), BidPrice, Slippage);
    
    else if (OrderType() == OP_SELL
        if(CloseSellCOndition)
           res = OrderClose(OrderTicket(), OrderLots(), AskPrice, Slippage);
   }
 
copy the whole code