Close Position By Ticket

 
How can i to close position by ticket? this code close position by Symbol()

#include <Trade\Trade.mqh>
CTrade            m_trade;


onticket()
{

m_trade.PositionClose(Symbol());

}
 
Farhad1:
How can i to close position by ticket? this code close position by Symbol()

Code Close all positions

***
//+------------------------------------------------------------------+
//| Close all positions                                              |
//+------------------------------------------------------------------+
void CloseAllPositions()
  {
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current positions
      if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
         m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
  }
***
Close all positions
Close all positions
  • www.mql5.com
Closing positions when reaching a profit level
 

I only open one trade at a time, so this works for me.


I have the tradeID stored globally (so at the top, "int tradeID;") so it means all functions can access it.

So open a trade as normal:

trade.Sell(0.1);

Then, after opening a trade:

tradeID = trade.ResultOrder();


So to close the only open trade:

trade.PositionClose(tradeID);