How to delete the Last Position in MT5??

 

Hi,

What would be the right code if I have 5 buy open positions and I want to delete only the last buy position the 5th one only?

Thanking you in advance!

 
Dilchan:

Hi,

What would be the right code if I have 5 buy open positions and I want to delete only the last buy position the 5th one only?

Thanking you in advance!

#include <Trade\Trade.mqh>
void OnStart()
{
   CPositionInfo p;
   CTrade t;
   ulong ticket=0;
   datetime time=0;
   for(int i=PositionsTotal()-1; i>=0; --i){
      if(p.SelectByIndex(i) 
         && p.Symbol() == _Symbol
         && p.Time() > time
      ){
         time = p.Time();
         ticket = p.Ticket();
      }
   }
   if(t.PositionClose(ticket))
      Print("position closed ");   
}
 
nicholi shen:

Thank you very very much!