partial close trades

 

I am trying to close partial orders, see snippet of my code...

bool PartyClose(int tik, double lot, double price)
  {
   for(count=zero_int; count<=zero_int; count++)
      if(!OrderClose(tik, lot, price, iSlippage, clrRed))
         Print("OrderClose Error ", GetLastError());
      else
         return true;

   return false;
  }

PROBLEM AND ISSUE

when i close single part of a trade, i often get an error "ticket is invalid".

how can i fix this? eg if i have an open trade that is size of 0.10, and i want to close 0.01 of the trade, i get the error, not always, but maybe 1 out of 4 times that i attempt to do partial closure.

 
Revo Trades:

I am trying to close partial orders, see snippet of my code...

PROBLEM AND ISSUE

when i close single part of a trade, i often get an error "ticket is invalid".

how can i fix this? eg if i have an open trade that is size of 0.10, and i want to close 0.01 of the trade, i get the error, not always, but maybe 1 out of 4 times that i attempt to do partial closure.

have you checked that it is using the right ticket number on those occasions?

 
jornal


yep, as you can see: that ticket was already closed.

 
   if(b >= 2)
      if(AvgBuyPrice > 0 && Bid >= AvgBuyPrice)
        {
         //            if(!OrderClose(BuyMaxTic, iMinLots,Bid, iSlippage, clrRed))
         //            if(!OrderClose(BuyMinTic, BuyMinLot,Bid, iSlippage, clrRed))
         done = false;
         if(BuyMaxTic > thr_int && BuyMinTic > thr_int)
            done = PartyClose(BuyMaxTic,BuyMaxLot <= iStartLots ? iMinLots : (double)MathCeil(BuyMaxLot / iMinLots) * iMinLots,Bid);
         if(done)
            PartyClose(BuyMinTic,BuyMinLot,Bid);
        }

This is bit of code i use to send the order. as you can see, the OrderSend command is only sent 1 time, so i do not understand why it appears that mt4 has tried to closed the 1 trade a 2nd time.

 
Revo Trades:

This is bit of code i use to send the order. as you can see, the OrderSend command is only sent 1 time, so i do not understand why it appears that mt4 has tried to closed the 1 trade a 2nd time.

If the order is already closed, and you are getting this error your code must still be using the old ticket number and submitting it again suggesting that the close logic of your program is still being activated despite the order having been closed.

So make sure that you are clearing out the variable that stores the ticket number as soon as it is closed... and only attempting to close if there is an order to be closed...


and debugging....

https://www.mql5.com/en/articles/654

https://www.metatrader5.com/en/metaeditor/help/development/debug

Debugging MQL5 Programs
Debugging MQL5 Programs
  • www.mql5.com
This article is intended primarily for the programmers who have already learned the language but have not fully mastered the program development yet. It highlights the key issues that every developer deals with when debugging a program. So, what is debugging? Debugging is a stage in program development meant for detecting and removing program...
 
Paul Anscombe:

If the order is already closed, and you are getting this error your code must still be using the old ticket number and submitting it again suggesting that the close logic of your program is still being activated despite the order having been closed.

So make sure that you are clearing out the variable that stores the ticket number as soon as it is closed... and only attempting to close if there is an order to be closed...

ok. thanks for your help. i have not made changes to the code yet, but the reading you posted looked promising: i have not cleared the ticket number after the first successful close, as you suggested might be the cause. I clear it upone the next new tic, but not after the successful closing of the ticket. Thanks again.