Close all trades code

 
I have this code which should  close all trades when  a certain condition is met. Normally 3 open trades of the same symbol are open when the condition is met. However, the code closes one trade and leaves 2 others instead of closing all the three trades how do I amend the code to close all the three trades?
 while(PositionsTotal()>0...........
     {
      for(int i=PositionsTotal()-1;i>=0; i--)

        {

         PositionSelect(PositionGetSymbol(i));

         while(false==m_trade.PositionClose(PositionGetInteger(POSITION_TICKET),3))
           {
            m_trade.PositionClose(PositionGetInteger(POSITION_TICKET),3);
           }

        }
     }
     
 

There are plenty of code doing that in the Codebase, did you check ?

Because in your 5 lines of code I can see a lot of problems, not sure it's worth to explain them.

 

#include <Trade\Trade.mqh> //Instatiate Trades Execution Library
#include <Trade\OrderInfo.mqh> //Instatiate Library for Orders Information
#include <Trade\PositionInfo.mqh> //Instatiate Library for Positions Information
void Close_all()
  {
   CTrade         m_trade; // Trades Info and Executions library
   COrderInfo     m_order; //Library for Orders information
   CPositionInfo  m_position; // Library for all position features and information
//--Đóng Positions
   for(int i = PositionsTotal() - 1; i >= 0; i--) // loop all Open Positions
      if(m_position.SelectByIndex(i))  // select a position
        {
         m_trade.PositionClose(m_position.Ticket()); // then close it --period
         Sleep(100); // Relax for 100 ms
        }
//--End  Positions

//-- Orders
   for(int i = OrdersTotal() - 1; i >= 0; i--) // loop all Orders
      if(m_order.SelectByIndex(i))  // select an order
        {
         m_trade.OrderDelete(m_order.Ticket()); // then delete it --period
         Sleep(100); // Relax for 100 ms
        }
//--End 
//-- Positions
   for(int i = PositionsTotal() - 1; i >= 0; i--) // loop all Open Positions
      if(m_position.SelectByIndex(i))  // select a position
        {
         m_trade.PositionClose(m_position.Ticket()); // then close it --period
         Sleep(100); // Relax for 100 ms
        }
//--End 
  }// End func Close_all
//+------------------------------------------------------------------+

"I'm sharing with you a function that I often use, which can close all open trades. Note that I perform it twice to ensure certainty."

 
Nguyen Trong Thanh #:

"I'm sharing with you a function that I often use, which can close all open trades. Note that I perform it twice to ensure certaint

Having issues with this code on a netting account

 
Peter Kaiza #: Having issues with this code on a netting account
On netting account there is always only a maximum of one open position per symbol. Adjust your code accordingly.
 
Mistake, the  account is a hedging type so don't need to amend.I have 3 trades of the same symbol  opened, code semss to close only one  and leaves the other 2.Why?
 
Peter Kaiza #: Mistake, the  account is a hedging type so don't need to amend.I have 3 trades of the same symbol  opened, code semss to close only one  and leaves the other 2.Why?

Because the PositionSelect() function is mostly for "netting" accounts. Search the CodeBase for publications for closing all trades and study how they work.

 
Peter Kaiza:
I have this code which should  close all trades when  a certain condition is met. Normally 3 open trades of the same symbol are open when the condition is met. However, the code closes one trade and leaves 2 others instead of closing all the three trades how do I amend the code to close all the three trades?

Take your time to study this code: https://www.mql5.com/en/code/25650

Close-All
Close-All
  • www.mql5.com
A script to close all market positions and/or pending orders.
Reason: