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; 
}
 
  1. Mohamed Hesham Ahmed: My question is: is there is any way to close orders based on the open time or lot size? ..

    Loop through your orders and find it. Then close it.

    double vMin=DBL_MAX; int iPos=EMPTY;
      for(int i=OrdersTotal()-1;i>=0;i--)
     {
        OrderSelect(i, SELECT_BY_POS);
        double v=OrderLots(); if(v < vMin){
        }
    }
    if(iPos !=EMPTY){
       OrderSelect(iPos, SELECT_BY_POS); …
    }
  2.   for(int i=OrdersTotal()-1;i>=0;i--)
     {
        OrderSelect(i, SELECT_BY_POS);

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

  3.         if ( OrderType() == OP_BUY )   result = OrderClose( OrderTicket(), OrderLots(), NormalizeDouble(Bid,Digits), 30 );
            if ( OrderType() == OP_SELL )  result = OrderClose( OrderTicket(), OrderLots(), NormalizeDouble(Ask,Digits), 30 );

    You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.

  4. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.