How to close lowest position

 

Hi everybody,

I'm a newby and i work with MT5.

I'l developping my own EA and i need in somme situations to close only the lowest position.

That mean i have to identify and select it and i don't know how to do that.

Actually to close a position i use this :

 for(int i=PositionsTotal()-1; i>=0; i--)                  //---Look at all positions
   {
   if(m_position.SelectByIndex(i))                          // selects the position by index for further access to its properties
      {
      if(m_position.Symbol()==Symbol())
         {
         if(m_position.Profit()>=Take_Profit_Target || PositionSwap<=-Maximum_swap)
            {     
            m_trade.PositionClose(m_position.Ticket());        // close a position by the specified symbol
            }
           }
          }
         }

and i wonder if with the same logical i can close only the lowest positions.

Thank's for the help.


Regards
 
What is the "lowest position" ? The one with highest loss ?
 
Alain Verleyen:
What is the "lowest position" ? The one with highest loss ?
Yes the lowest position in my case is the one with the most negative profit
 
So what's the problem? Go through the list and remember the ticket with the largest loss. On loop exit, close the ticket.
 

This is the problem, i don't know how to do exactly that...

How to identify the one with le largest lost.

If you can post an example of code it would be great.

 
chpeller: i don't know how to do exactly that...
Not compiled, not tested.
int ticket=0; double largestLoss=0;

for(int i=PositionsTotal()-1; i>=0; i--){  // \  Go 
   if(m_position.SelectByIndex(i)          //  > through 
   && m_position.Symbol()==Symbol()        // /  the list.

   && m_position.Profit() < largestLoss    // \  remember 
   ){                                      //  \ the ticket 
      largestLoss = m_position.Profit();   //  / with the 
      ticket      = m_position.Ticket();   // /  largest loss.
   }
}                                          // \ On loop exit, 
if(ticket)  m_trade.PositionClose(ticket); // / close the ticket
Not compiled, not tested.
Is that so hard? Coded exactly as described.
 

I will try this as soon as possible. Many thanks.

It seem to be simple for you, but for me, four months ago i had never coded anything neither done any trade.
As it is not so obvious for me, i am still in training and sometimes i need a help from a more senior coder.

 
William Roeder:
Not compiled, not tested. Not compiled, not tested.
Is that so hard? Coded exactly as described.

Thank you for your response but i think this won't do the job.

Your code seem to select all the positions with profit under 0.

I can have up to 100 positions with profit under 0. For example if have 4 negative profit positions :

pos1 = -10, pos2 = -20, pos3 = -5, pos 4 = -15. I want to close only "pos 2" and keep opened all the others.

My need is to select "pos2" in a large group of negative profit positions, i know how to close only this one if i can select it.

 
  1. There is no need to select profitable positions as you aren't going to close them.
    chpeller: i need in somme situations to close only the lowest position.
  2. Yes, it selects all losing positions. There is no way to find the biggest looser unless you look at all of them.
  3. Once the loop exits, then and only then, do you know the biggest. Think!
 
Alain Verleyen:
What is the "lowest position" ? The one with highest loss ?

It is the worst profitable buy or sell position.

The one with the highest loss

 
Create 2 arrays one that will hold positions tickets of long type and another one of double type that will hold positions profit , you just need to loop through all positions and insert tkt and position profit into the respective array in every iteration . Then using ArrayMin function pass the profits array to it it will return the index of lowest profit in the array . Use this index and select the item in the tickets array using the same index then close the position that has this index
Or you can use 2dimensions array with 1st dimension will hold profits and 2nd will hold positions tickets then apply the ArrayMin function just like previously explained
Reason: