How to select the largest lot in the pool?

 

Hi everyone, 

I just try to select the order ticket of largest lot order from my current trading pool, buying and selling.

From some of sample just write like following, but as my understanding, it just return the random ticket rather than largest one. 

Is anyone could advise me this is right or not? If not, is anyone could give some advice to correct this code?

Thanks

int TicketOfLargestBuyOrder()
{
   double maxLots=0;
   int    orderTicketNr=0;

   for (int i=0;i < OrdersTotal();i++)
   {
      if ( OrderSelect(i,SELECT_BY_POS)) 
      {
         if( OrderType()==OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber()==magicbuy)
         {
            
            double orderLots = OrderLots();  
            if (orderLots >= maxLots) //double maxLots = MarketInfo(Symbol(),MODE_MAXLOT) ;
            {
               maxLots       = orderLots; 
               orderTicketNum = OrderTicket();
            }   
         } 
      } 
   }
   return orderTicketNum;
}
 
kentlee5566:

Hi everyone, 

I just try to select the order ticket of largest lot order from my current trading pool, buying and selling.

From some of sample just write like following, but as my understanding, it just return the random ticket rather than largest one. 

Is anyone could advise me this is right or not? If not, is anyone could give some advice to correct this code?

Thanks

you need a (max) lot. why return ticket?

 

Hi Mohamad,

Because I want to make some modify through Orderselect(OrderTicket(),,) for some dedicated orders.

So do you mean I should use MathMax()/fmax()??

 
kentlee5566:

Hi Mohamad,

Because I want to make some modify through Orderselect(OrderTicket(),,) for some dedicated orders.

So do you mean I should use MathMax()/fmax()??

  1. Loop order pool to get largest lot first.
  2. loop again order pool, and find ticket that matching order lots.
  3. Use that ticket to do the modifications etc..

int HighestOrderLotTicket(){
   int ticket = 0;
   double lot = 0;
   for(int order = OrdersTotal()-1; order >= 0; order--){
      bool select = OrderSelect(order,SELECT_BY_POS);   
      if(select && OrderSymbol()==_Symbol) {
         if(OrderLots()>lot) lot = OrderLots();
      }
   }
   
   if(lot>0) {
      for(int order = OrdersTotal()-1; order >= 0; order--){
         bool select = OrderSelect(order,SELECT_BY_POS);   
         if(select && OrderLots()==lot) {
            ticket = OrderTicket();
            break;
         }
      }
   }
   
   return ticket;
}
 
int TicketOfLargestBuyOrder()
{
   double maxLots=0;
   int    orderTicketNum=0;

   for (int i=0;i < OrdersTotal();i++)
   {
      if( !OrderSelect(i,SELECT_BY_POS) ) continue;
      if( OrderType()!=OP_BUY )           continue;
      if( OrderSymbol() != Symbol() )     continue;
      if( OrderMagicNumber()!=magicbuy )  continue;
      if( OrderLots() <= maxLots )        continue;
      maxLots        = OrderLots();
      orderTicketNum = OrderTicket();
   }
   return orderTicketNum;
}
 
kentlee5566:

Hi everyone, 

I just try to select the order ticket of largest lot order from my current trading pool, buying and selling.

From some of sample just write like following, but as my understanding, it just return the random ticket rather than largest one. 

Is anyone could advise me this is right or not? If not, is anyone could give some advice to correct this code?

Thanks

I can see nothing wrong with your code. It does what you want.

Just remember that if there are more than 1 order with the same lot size, your function will only return 1 of them.

 
Mohamad Zulhairi Baba:

  1. Loop order pool to get largest lot first.
  2. loop again order pool, and find ticket that matching order lots.
  3. Use that ticket to do the modifications etc..

Sorry I just feel confuse at 1st step, get the largest one. For instance, if I have five order with lots, 0.01 / 0.02 / 0.04 / 0.08 / 0.1.

According to your code, the 1st part is select the order lot for each order, total are five lots. But how to define the max lot?

I mean, how to select the order with max lot, 0.1? It seems loop will return five order, rather than only one.


Please correct me, I stuck at this issue for two days :( 

int HighestOrderLotTicket(){
   int ticket = 0;
   double lot = 0;
   for(int order = OrdersTotal()-1; order >= 0; order--){		// loop for 5 times 
      bool select = OrderSelect(order,SELECT_BY_POS);   		// select the order for each time looping
      if(select && OrderSymbol()==_Symbol) {				// to confirm the right order is selected
         if(OrderLots()>lot) lot = OrderLots();				// to define lot = OrderLots()
      }
   }
   
   if(lot>0) {
      for(int order = OrdersTotal()-1; order >= 0; order--){
         bool select = OrderSelect(order,SELECT_BY_POS);   
         if(select && OrderLots()==lot) {
            ticket = OrderTicket();
            break;
         }
      }
   }
   
   return ticket;							// return each order ticket from above
}
 
kentlee5566:

Sorry I just feel confuse at 1st step, get the largest one. For instance, if I have five order with lots, 0.01 / 0.02 / 0.04 / 0.08 / 0.1.

According to your code, the 1st part is select the order lot for each order, total are five lots. But how to define the max lot?

I mean, how to select the order with max lot, 0.1? It seems loop will return five order, rather than only one.

1st part will evaluate all 5 orders, and determine (return) which one is highest. (Not all 5 orders)
2nd part will find which corresponding ticket matches that highest lot.

Have you tried the code on demo?

int HighestOrderLotTicket(){
   int ticket = 0;
   double lot = 0;
   for(int order = OrdersTotal()-1; order >= 0; order--){               // loop for 5 times 
      bool select = OrderSelect(order,SELECT_BY_POS);                   // select the order for each time looping
      if(select && OrderSymbol()==_Symbol) {                            // to confirm the right order is selected
         if(OrderLots()>lot) lot = OrderLots();                         // return largest lot in the pool
      }
   }
   
   if(lot>0) {                                                          // largest lot exist & > 0
      for(int order = OrdersTotal()-1; order >= 0; order--){
         bool select = OrderSelect(order,SELECT_BY_POS);   
         if(select && OrderLots()==lot) {                               // matching largest lot
            ticket = OrderTicket();                                     // ticket
            break;                                                      // ticket found, exist loop
         }
      }
   }
   
   return ticket;                                                       // return that ticket
}


 
Mohamad Zulhairi Baba:
1st part will evaluate all 5 orders, and determine (return) which one is highest. (Not all 5 orders)
2nd part will find which corresponding ticket matches that highest lot.

Have you tried the code on demo?


Thanks man, I try on it. And it seems work!!

Great

 
kentlee5566:

Sorry I just feel confuse at 1st step, get the largest one. For instance, if I have five order with lots, 0.01 / 0.02 / 0.04 / 0.08 / 0.1.

According to your code, the 1st part is select the order lot for each order, total are five lots. But how to define the max lot?

I mean, how to select the order with max lot, 0.1? It seems loop will return five order, rather than only one.


Please correct me, I stuck at this issue for two days :( 

I don't know why you are using 2 loops where 1 will do

//-----------------------------------------------------------------------------------++
int HighestOrderLotTicket(){
   int ticket = 0;
   double lot = 0;
   for(int order = OrdersTotal()-1; order >= 0; order--){           // loop for n times 
      if(OrderSelect(order,SELECT_BY_POS))                          // select the order for each time looping
      if(OrderSymbol()==_Symbol) {                                  // to confirm the right order is selected
         if(OrderLots()>lot) {
          lot = OrderLots();                                        // to define lot = OrderLots()
          ticket=OrderTicket();                                     // get ticket
         }
      }
   }
   return ticket;                                                   // return each order ticket from above
}
//-----------------------------------------------------------------------------------++
Reason: