Auto Close Script (Close small lots 1st)

Hello,

I am writing a code to close trades when Account Equity is greater that Account Balance.
My target is to reduce risk for another martingale EA. so I exit at neutral point (No Loss / Profit) after the EA open 5 trades or more.
My question is: is there is any way to close orders based on the open time or lot size? … I want to close the smaller lots or older trade first then go to the next and then to the next …
This is basically to avoid interacting with the main Martingale EA, where the EA will automatically open another trade if the account is in loss.

Example:

Martingale EA open 5 Buy trades on EURUSD

  • 0.01 @ 1.2150 @ 10:15

  • 0.02 @ 1.2140 @ 11:20

  • 0.03 @ 1.2130 @ 11:55

  • 0.04 @ 1.2120 @ 13:50

  • 0.05 @ 1.2110 @ 15:10

if the Auto close EA I implemented close 0.05 1st then the Martingale EA will Open another trade, so I do like to close 0.01 > 0.02 > 0.03 > 0.04 > 0.05 to avoid such interaction.

if (OrdersTotal() > 4 && (AccountEquity() > AccountBalance()))
{

SendNotification(“Auto Close”);
Print (“Auto Close”);
CloseOrdersAll();
}

}

//±-----------------------------------------------------------------+

void CloseOrdersAll()
{
for(int i=OrdersTotal()-1;i>=0;i–)
{
OrderSelect(i, SELECT_BY_POS);
bool result = false;
if ( OrderType() == OP_BUY ) result = OrderClose( OrderTicket(), OrderLots(), NormalizeDouble(Bid,Digits), 30 );
if ( OrderType() == OP_SELL ) result = OrderClose( OrderTicket(), OrderLots(), NormalizeDouble(Ask,Digits), 30 );

}
return;
}