MT4 EA Close specific order.

 

Hi!
Im running multiple Forex Pairs but i only want to close the order of the Forex Pair that the EA is installed "on".

My try;

void CloseOrders()

   {

   // Update the exchange rates before closing the orders.

   RefreshRates();

   // Log in the terminal the total of orders, current and past.

      

   // Start a loop to scan all the orders.

   // The loop starts from the last order, proceeding backwards; Otherwise it would skip some orders.

   for (int i = (OrdersTotal() - 1); i >= 0; i--)

   {

      // If the order cannot be selected, throw and log an error.

      

      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

      {

      

      

      if (OrderSymbol() == Symbol()) // Comparing Symbols

            { 


      // Create the required variables.

      // Result variable - to check if the operation is successful or not.

      bool res = false;

      

      // Allowed Slippage - the difference between current price and close price.

      int Slippage = 0;

      

      // Bid and Ask prices for the instrument of the order.

      double BidPrice = MarketInfo(OrderSymbol(), MODE_BID);

      double AskPrice = MarketInfo(OrderSymbol(), MODE_ASK);


      // Closing the order using the correct price depending on the type of order.

      if (OrderType() == OP_BUY)

          {

         res = OrderClose(OrderTicket(), OrderLots(), BidPrice, Slippage);

      }

      else if (OrderType() == OP_SELL)

          {

         res = OrderClose(OrderTicket(), OrderLots(), AskPrice, Slippage);

      }

      

      // If there was an error, log it.

      if (res == false) Print("ERROR - Unable to close the order - ", OrderTicket(), " - ", GetLastError());

   }

   }

   }

   }
Unfortunatly the function never goes further than the OrderSelect. Do you guys see my problem? Thank you :)

 
  • Please edit your post and use the "</>" or Alt-S to paste your code properly. Don't just paste in plain text. It makes it difficult to read.
  • To control which orders belong to your EA not others you should filter by Symbol and Magic number. Use a parameter for the user to set the magic number.

Function

Action

OrderMagicNumber

Returns an identifying (magic) number of the currently selected order

OrderSend

The main function used to open an order or place a pending order

OrderSymbol

Returns symbol name of the currently selected order


OrderMagicNumber - Trade Functions - MQL4 Reference
OrderMagicNumber - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderMagicNumber - Trade Functions - MQL4 Reference
 
Unfortunatly the function never goes further than the OrderSelect. Do you guys see my problem? Thank you :)
Why do you think that? There are no prints that would report this.
Reason: