Ordermodify mt4 code issue

Jajaofopobo

Trader
Sep 6, 2021
70
7
24
i have an issue with this function code. Please any help will be greatly appreciated.
No. 1: it doesnt close breakeven when it partially closes the trade. states "OrderModify error 4108 unknown ticket 1 for OrderModify function" and i have tried my best but still dont get/understand it.
No. 2: it tries to partially close a trade it has partially close cos of the pips, is there a way i can mark those trade it already partially closed it? or another way out of it....Thanks so much
MQL4:
//+------------------------------------------------------------------+
//| Function to manage partial close and adjust stop loss to breakeven |
//+------------------------------------------------------------------+
void ManagePartialClose()
{
   for (int i = 0; i < OrdersTotal(); i++)
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
            double orderOpenPrice = OrderOpenPrice();
            double currentPrice = (OrderType() == OP_BUY) ? Bid : Ask;
            double distance = (OrderType() == OP_BUY) ? currentPrice - orderOpenPrice : orderOpenPrice - currentPrice;
 
            if (distance >= partialClosePips * Point())
            {
               double lotSizeToClose = OrderLots() * (partialClosePercent / 100.0);
               double lotSizeRemaining = OrderLots() - lotSizeToClose;
               int closeType = (OrderType() == OP_BUY) ? OP_SELL : OP_BUY;
               double closePrice = (OrderType() == OP_BUY) ? Bid : Ask;
 
               // Perform partial close
               if (OrderClose(OrderTicket(), lotSizeToClose, closePrice, slippage, clrNONE))
               {
                  // Adjust stop loss to breakeven
                  double newSL = (OrderType() == OP_BUY) ? orderOpenPrice : orderOpenPrice;
                  double newTP = (OrderType() == OP_BUY) ? (orderOpenPrice + takeprice * Point()) : (orderOpenPrice - takeprice * Point());
                  // Modify the remaining order to update stop loss to breakeven
                  OrderModify(OrderTicket(), orderOpenPrice, newSL, newTP, 0, clrNONE);
               }
            }
         }
      }
   }
}
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,210
1,504
144
Odesa
www.earnforex.com
No. 1: it doesnt close breakeven when it partially closes the trade. states "OrderModify error 4108 unknown ticket 1 for OrderModify function" and i have tried my best but still dont get/understand it.
When you partially close a trade, its ticket number changes. You can no longer use the same ticket number to modify it. You need to select the order one more time.

No. 2: it tries to partially close a trade it has partially close cos of the pips, is there a way i can mark those trade it already partially closed it? or another way out of it....Thanks so much
If you cannot tell by the volume (if you aren't cutting it in fixed steps and it varies), your only solution would be to use a dynamic array of orders to store those that have already been partially closed and checking order tickets against that array. Please don't forget that the ticket number changes upon a partial close.
 
  • 👍
Reactions: Jajaofopobo

Jajaofopobo

Trader
Sep 6, 2021
70
7
24
Thanks so much for this.. i now see my mistake...i will use orderopenprice as the unique identifier and modify the trade first before partial close.