Help for OrderExpiration in a OrderClose-Function

 

Hi everyone,

I need a little Help with my Code. I want close every Trade after, for example, 20 Minutes. No matter if the Trades is profitable or not. It is possible to write this condition in my existing Code?
And which Codelines and Definition of Variables I also need?

I appreciate your help.

This is the OrderClose-Function I have already:

void CloseTrade(bool closeBuys=false, bool closeSells=false)
{
   for(int i = 0;i < OrdersTotal(); i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol() == Symbol()&&OrderMagicNumber()==magic)
            if(OrderType()<2)
            {
            bool Close_BUY = (Bid >= (OrderOpenPrice() + (MyOCTakeprofit*pips)));
            bool Close_SELL = (Ask <= (OrderOpenPrice() - (MyOCTakeprofit*pips)));
            double price = OrderClosePrice();
            int ticket = OrderTicket();
            int Type=OrderType();
            if( !closeBuys && !closeSells )
               if( (Type == OP_BUY && Close_BUY) || (Type == OP_SELL && Close_SELL) )
               {
               if(!OrderClose(ticket,OrderLots(),price,30,clrBlack)) Print(GetLastError());        
               else i--;
               }
            if( (Type == OP_BUY && closeBuys) || (Type == OP_SELL && closeSells) )
               {
               if(!OrderClose(ticket,OrderLots(),price,30,clrTurquoise)) Print(GetLastError());        
               else i--;
               }
            }
   }      
 }

 
dundale: I want close every Trade after, for example, 20 Minutes. It is possible to... I appreciate your help.
  1. When you post code please use the SRC button! Please edit your post.
               General rules and best pratices of the Forum. - General - MQL5 programming forum
  2. Of course it's possible: Find the order, get the open time, get the current time, compare them, and close it.
  3. Help you with what? You haven't stated a problem.
  4. for(int i = 0;i < OrdersTotal(); i++){
       double price = OrderClosePrice();
       :
                   else i--;
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) because while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    2. For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    3. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
Reason: