OrderModify error 4108 after partial profit taking

 

Hello, 

I am trying to modify stoploss using the ordermodify function after partial profit taking in MQL4, but I keep receiving error 4108. All the variables have been declared without any errors.

Thanks in advance for your help.

else //else if you already have open orders, update your take profit
   {
      if (OrderSelect(openOrderID,SELECT_BY_TICKET) == true)
      {
         int orderType = OrderType(); //0 if long, 1 if short
         double op = OrderOpenPrice();
         double lots = OrderLots();
                  
         if (lots == lotSize)
         {
            if (orderType == 0) //long position
            {  
               if (Bid - op > (firstTakeProfit*GetPipValue()))
               {
                  OrderClose(openOrderID, lots/2, Bid, slippage, clrRed);
                  
                  double optimalStopLoss = medium_moving_ema;
                  OrderModify(openOrderID, op, optimalStopLoss, takeProfitLong, 0);
               }
  
            }
            
            else if (orderType == 1) //short position
            {
               if (op - Ask >(firstTakeProfit*GetPipValue()))
               {
                  OrderClose(openOrderID, lots/2, Ask, slippage, clrRed);
                  
                  double optimalStopLoss = medium_moving_ema;
                  OrderModify(openOrderID, op, optimalStopLoss, takeProfitLong, 0);    
               }
            }
         
         }
    
         
      }
   }
 

Error ...

4108

ERR_INVALID_TICKET

Invalid ticket

When you do a partial close, the ticket number of the remaining position is different to the initial one.

For simplicity, I suggest you do the modification first, then do the partial close, otherwise you will have to write extra code to track and search for the ticket number of new order after the partial close.

 

As a followup to my previous post, in the following GIF animation, you can see how when I do a partial close of an open order, the ticket number changes from #490272795 to #490273079.

Click on the image below to see the animated video...


 
kanyi_maina:

Hello, 

I am trying to modify stoploss using the ordermodify function after partial profit taking in MQL4, but I keep receiving error 4108. All the variables have been declared without any errors.

Thanks in advance for your help.

Do not double post!

I have deleted your duplicated topic.

 
             OrderClose(openOrderID, lots/2, Ask, slippage, clrRed);

You can't just use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot.

You also must check if you have already done it, to avoid repeated closing. Alternatives:

  • Move SL to Break Even+1 before the partial close. That way, you know that you already did it.
  • Set a flag in persistent storage (files, global variables w/flush)
  • Open two orders initially, and close one (manually or by TP.)
 
kanyi_maina:

Hello, 

I am trying to modify stoploss using the ordermodify function after partial profit taking in MQL4, but I keep receiving error 4108. All the variables have been declared without any errors.

Thanks in advance for your help.

Or you can just trace your orders with magic number. Magic Number will be the same for remaining of the opened order after partial has been taken. 
 
Daniel Cioca #:
Or you can just trace your orders with magic number. Magic Number will be the same for remaining of the opened order after partial has been taken. 

And if there are more than 1 open orders with the same magic number?

 
Keith Watford #:

And if there are more than 1 open orders with the same magic number?

Magic Number has to be unique… otherwise why bother using it …
 
Daniel Cioca #:
Magic Number has to be unique… otherwise why bother using it …

Maybe unique for the EA, but each order placed by that EA will use the same magic number that is assigned to it in the EA properties.

 
Keith Watford #:

Maybe unique for the EA, but each order placed by that EA will use the same magic number that is assigned to it in the EA properties.

Not necessary… should be unique for the order… MN should be computed in the EA code … like for example use instrument name to compute MN … 
 
Daniel Cioca #: MN should be computed in the EA code … like for example use instrument name to compute MN … 

It does not need to be computed.

Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
          PositionClose is not working - MQL5 programming forum (2020)
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
          Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
          Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

Reason: