Need help identifying last BUY and last SELL order ticket

 

Hello!

I want to identify both last BUY and SELL order ticket to calculate their current profit and add more positions after X profit.

I have tried several ways but I don't know why it's not working. I have tried printing the ticket variable out but the ticket number is always 0

Here is what I have tried so far

if( OrdersTotal() > 0 )
{  
   
   int   All_Ticket[] ;    ArrayResize(All_Ticket,420) ;
   int   BUY_Ticket[] ;    ArrayResize(BUY_Ticket,420) ;
   int   SELL_Ticket[];    ArrayResize(SELL_Ticket,420);
   
   //++++++++++++++++++++++++++++++++++++++++++++++//

   for(i = OrdersTotal() - 1; i >= 0; i--)
   {
      SelectOrder =  OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
            All_Ticket[i]  =  OrderTicket();
         
            /*
            switch( OrderType() )
            {
               case OP_BUY    :  if( OrderTicket() > Last_BUY_Ticket  )  Last_BUY_Ticket  = OrderTicket()   ;
               case OP_SELL   :  if( OrderTicket() > Last_SELL_Ticket )  Last_SELL_Ticket = OrderTicket()   ; 
            }
            */
            
         }
   }
   
   //++++++++++++++++++++++++++++++++++++++++++++++//
   
   for( i = OrdersTotal() - 1; i >= 0; i-- )
   {
      SelectOrder = OrderSelect(All_Ticket[i],SELECT_BY_TICKET,MODE_TRADES);
      
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
            
            if( OrderType() == OP_BUY )   
            {
               BUY_Ticket[i]  =  All_Ticket[i];
            }
            else {
                     if( OrderType() == OP_SELL )
                     {
                        SELL_Ticket[i] =  All_Ticket[i];
                     }
                 }
         }
   }
   
   //++++++++++++++++++++++++++++++++++++++++++++++//
/*   
   ArraySort(BUY_Ticket,0,0,MODE_DESCEND);
   ArraySort(SELL_Ticket,0,0,MODE_DESCEND);
*/   
   int   Last_BUY_Ticket   =  ArrayMaximum(BUY_Ticket ,0,0);
   int   Last_SELL_Ticket  =  ArrayMaximum(SELL_Ticket,0,0);
   
   Print("Last_BUY_Ticket = #",Last_BUY_Ticket);
   Print("Last_SELL_Ticket = #",Last_SELL_Ticket);

   //++++++++++++++++++++++++++++++++++++++++++++++//

   
//-- Last BUY Order --//

   SelectOrder =  OrderSelect(Last_BUY_Ticket,SELECT_BY_TICKET,MODE_TRADES)   ;
   
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
      {
   
         double      Last_BUY_Profit   =  subP_Profit( OrderType(), OrderOpenPrice() );
         
            if( Last_BUY_Profit > AddPositionPoint )
            {
                 subADD_BUY(B_SL,B_TP)   ;
            }
            
      }         
         
         
//-- Last SELL Order --//

   SelectOrder =  OrderSelect(Last_SELL_Ticket,SELECT_BY_TICKET,MODE_TRADES)  ;
   
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
      {   
   
         double      Last_SELL_Profit   =  subP_Profit( OrderType(), OrderOpenPrice() );
         
            if( Last_SELL_Profit > AddPositionPoint )
            {
                  subADD_SELL(S_SL,S_TP)  ;            
            }
            
      }
      
      
}               


Anyone know why it's not working?

Also, I'm new to coding

Appreciate any ideas/suggestions

 
Yutthasak Wisidsin:

Hello!

I want to identify both last BUY and SELL order ticket to calculate their current profit and add more positions after X profit.

I have tried several ways but I don't know why it's not working. I have tried printing the ticket variable out but the ticket number is always 0

Here is what I have tried so far


Anyone know why it's not working?

Also, I'm new to coding

Appreciate any ideas/suggestions

Welcome to the wonderful world of programming. It's often the case that you will ask for constructive criticism, and in the interest of succinctness the response can seem as if they are attacking you, but take it as a sign of respect that someone would take their time to help you.   With that being said your code could use a lot of work. You should study some C++ style guides and clean up your indentation and variable naming conventions. For example, CapWords are usually reserved for class type defs. Regarding your logic I think that you may be over-complicating it. In order to get the most recent orders you just need to loop through the order pool and assign the ticket number to a single variable if the time is greater than all previous orders evaluated before it. This is an example of how one might approach the challenge. 


void OnStart()
{
   if(OrderSelect(most_recently_opened(OP_BUY, 0, MODE_HISTORY), SELECT_BY_TICKET))
      printf("Most recent: Ticket=%d, Time=%s", 
         OrderTicket(), TimeToString(OrderOpenTime())
      );
}


int most_recently_opened(int order_type, int magic=0, int pool=MODE_TRADES)
{
   int total = pool == MODE_TRADES ? OrdersTotal() : OrdersHistoryTotal();
   int ticket = -1;
   datetime order_time = 0;
   for(int i = total - 1; i >= 0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, pool)
         && OrderSymbol() == _Symbol
         && (magic == 0 || OrderMagicNumber() == magic)
         && OrderType() == order_type
         && OrderOpenTime() > order_time
      ){
         order_time = OrderOpenTime();
         ticket = OrderTicket();
      }
   }
   return ticket;
}
 

Thank you for your advice @nicholi shen  I will surely try to get into programming as it's seems fun and challenging for me!

As for my problem, you even take the time to give me an example, really appreciate your help!

Reason: