Partial OrderClose()

 
Hi All,

In MQL4 language, if we use OrderClose() function to close just a part of the lots of the entire position, the EA will close the actual position ticket(number), and create another position ticket(number) with the remaining lots.

OrderClose(OrderTicket(),(OrderLots()/2),Bid,3,Pink);

I need to catch the new ticket number that is generated!


If there aren't any other positions created, between the creation of the order and the partial close, I can use

OrderTicket()+1

to catch the following position ticket number.


But in case, there are an aleatory number of open positions / partial closes, between the creation of a position and the partial close, I cannot catch the ticket number.

Does anyone has ever come across this situation?

Can someone help me?

Thanks in advance.

 
You can use specific Magic Number for your order, it won't change after partial close.
 
I do not know if every broker has it same, but I have the counterpart order number in a Comment field, replacing the original Comment.
 
nunonuk: I need to catch the new ticket number that is generated!

Best_Solutions I can think of involves Saving Ticket_Numbers[Array] to File. If all you need is just getting this number then, Populate an Array of Open_Order_Numbers before the OrderClose(). Then Loop again for any New_Order_Number generated. Comments are not reliable for Partial_Close Experts because Broker Overwrite is not Consistent. As mention above, in most cases, the Magic# is enough [Unless] you need to Label/Separate Partial_Tickets from the normal ones. 

 

Thank you all for trying to help!

ubzen, your solution seems suitable, but I found another solution.

Having in consideration that the ticket numbers always start on 1 and are increment from that point, and that only the EA is going to open orders, I created a Global Variable that counts the tickets and increment evey time I create an order or close one partially. In that way, after an order is partial closed I can access to the global variable and get the ticket number.

It's working :)

 
nunonuk:

Thank you all for trying to help!

ubzen, your solution seems suitable, but I found another solution.

Having in consideration that the ticket numbers always start on 1 and are increment from that point, and that only the EA is going to open orders, I created a Global Variable that counts the tickets and increment evey time I create an order or close one partially. In that way, after an order is partial closed I can access to the global variable and get the ticket number.

It's working :)

Yeah..Good for Strategy_Tester ONLY. In Live trading the Ticket#'s have no Reliable Sequence.
 

ubzen,


In that case, I'll apply your suggestion! 


Thanks for helping.

 
Ovo: I do not know if every broker has it same, but I have the counterpart order number in a Comment field, replacing the original Comment. 
The OrderComment() method is the easiest [Coding-Wise]. For some really good programmers on this forum this is enough. For others, it's un_reliable. It just depends on the programmer :)
 

I am going to share the function I have developed for this purpose!

First I thought about finding the ticket number from the "OrderOpenTime()", when I realized that when "OrderClose()" is executed, a new ticket number is assigned, but the "OrderOpenTime()" is equal to the first order before the partial close!

Then I thought about the "OrderOpenPrice()", this one alone, would work, but who knows..., so combining ""OrderOpenTime()" and "OrderOpenPrice()" gives me for sure the ticket I am looking for.

Here is the function:

int getLastTicketNumber(double openPrice)
{
   datetime temp = 0;
   int lastTicketNumber;
   
   for(i=1; i<=OrdersTotal(); i++)
      {
         if ( OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES) == true )
         {
            if ( OrderOpenTime() > temp && openPrice == OrderOpenPrice() ) 
            {
               temp = OrderOpenTime();
               lastTicketNumber = OrderTicket();
            }
         }
      }
   return(lastTicketNumber);
}
 

What about something like this (sorry for the rough code...need to sleep soon):

Compiled but not completely tested

int PartialOrderClose(int TicketID, double ClosePrice, double LotSize, int EA_MagicNumber) {
   int before_close[], i = 0, j = 0, array_size = 0;
   bool new_order = false;
   GetLastError();   // clear error flag
   
   for (i = OrdersTotal() - 1; i >= 0; i--)
      if (OrderSelect(i, SELECT_BY_POS))
         if (OrderSymbol() == Symbol())                                   // should also include: OrderMagicNumber() == EA_MagicNumber
            if (OrderType() <= 5) {
               array_size = ArrayRange(before_close, 0) + 1;
               ArrayResize(before_close, array_size);
               before_close[array_size - 1] = OrderTicket();
            }
   if (OrderClose(TicketID, LotSize, ClosePrice, 10)) {
      for (i = OrdersTotal() - 1; i >=0; i--)
         if (OrderSelect(i, SELECT_BY_POS))
            if (OrderSymbol() == Symbol())                                // should also include: OrderMagicNumber() == EA_MagicNumber
               if (OrderType() <= 1) {                                    // only open OP_BUY and OP_SELL orders
                  for (j = 0; j < ArrayRange(before_close, 0); j++) {
                     if (before_close[j] != OrderTicket())
                        new_order = true;
                     else
                        { new_order = false; break; }
                  }
                  if (new_order)
                     return (OrderTicket());
               }
   }
   else {
      Print ("An error occurred (Code: ", GetLastError(), ").");
      return (-1);
   }
                
   return (0);
}

The above code makes a list of all of the pending/open orders, and then attempts to partially close the desired order.  Afterward, assuming no error, it tries to find the ticket number for the market order that does not exist in the list which was previously generated, and when the unlisted ticket number is found, it returns that ticket number.

 

 How does your system open new trades. 

What lotsize do you choose opening trades. Is that a fixed lotsize ??

Do you change the Stoploss level the moment you close partly ??  

Reason: