Return highest buy or lowest sell ticket number

 

Hello Fellow coders,

I'm getting myself into a bit of a flap. I am trying to write a function so that it will return the ticket number of my highest buy trade or lowest sell trade, so they can be isolated from the basket and closed. 

I know I need to find the correct ticket number of the above trades so they can be returned into the function to close that ticket. I'm just not sure how to write it correctly any help or pointing into the right direction would be much appreciated.

 
Adam Woods :

Hello Fellow coders,

I'm getting myself into a bit of a flap. I am trying to write a function so that it will return the ticket number of my highest buy trade or lowest sell trade, so they can be isolated from the basket and closed. 

I know I need to find the correct ticket number of the above trades so they can be returned into the function to close that ticket. I'm just not sure how to write it correctly any help or pointing into the right direction would be much appreciated.

Code:


//+------------------------------------------------------------------+
//| Calculate all positions                                          |
//+------------------------------------------------------------------+
void CalculateAllPositions(ulong &ticket_highest_buy,ulong &ticket_lowest_sell)
  {
   ticket_highest_buy   = ULONG_MAX;
   ticket_lowest_sell   = ULONG_MAX;
//--- auxiliary variables
   double price_highest_buy   = DBL_MIN;
   double price_lowest_sell   = DBL_MAX;
//---
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               if(m_position.PriceOpen()>price_highest_buy) // the highest position of "BUY" is found
                  ticket_highest_buy=m_position.Ticket();
               continue;
              }
            else
               if(m_position.PositionType()==POSITION_TYPE_SELL)
                 {
                  if(m_position.PriceOpen()<price_lowest_sell) // the lowest position of "SELL" is found
                     ticket_lowest_sell=m_position.Ticket();
                 }
           }
  }
 
Vladimir Karputov:

Code:


Perfect that gives me everything I need to put into my own code.. thank you

 
               if(m_position.PriceOpen()>price_highest_buy) { // the highest position of "BUY" is found
                  price_highest_buy=m_position.PriceOpen();
                  ticket_highest_buy=m_position.Ticket();
               }

same for price_lowest_sell

 
lippmaje #:

same for price_lowest_sell

Can you write same code for mql4, please
 
Jagdish Vasave - # :
Can you write same code for mql4, please

You are on the MQL5 forum - all questions about MQL5 are discussed here.

There is one special section for the old terminal: MQL4 and MetaTrader 4 - please ask your question there.

Reason: