OrderClose Error 4108 - unknown ticket for OrderCloseFunction

 

Good afternnon everybody.

I need your kind help . Sometimes I get this error running the EA : sometimes but not always , that's strange. It runs normally and open/close the trading but sometimes it seems it is getting confused and close only a part of the order at target and the others keep on working. I get 0 errors and 0 warning when compiling it.  Very strange. Anybody can help me ?

for(cnt=0;cnt<OrdersTotal();cnt++){
     if (OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES)){
     if(OrderSymbol()==Symbol() && OrderMagicNumber () == MagicBuy){
       ticketbuy = OrderTicket();//OrderSelect(ticketbuy,SELECT_BY_TICKET, MODE_TRADES);
       profitbuy = profitbuy+OrderProfit() ;
       openpricebuy = OrderOpenPrice();}
     }
  }
  tpb = (OrdersTotalMagicBuy(MagicBuy)*AutomatedTPFactor*Point)+openpricebuy;
  double bid = MarketInfo(Symbol(),MODE_BID);
  if(UseAutomatedTP==true&&profitbuy>0){
    if(Bid>=tpb) orderclosebuy(ticketbuy);
  }
  for(cnt=0;cnt<OrdersTotal();cnt++){   
     if (OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES)){
     if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicSell){
       ticketsell = OrderTicket();//OrderSelect(ticketsell,SELECT_BY_TICKET, MODE_TRADES);
       profitsell = profitsell+OrderProfit();
       openpricesell = OrderOpenPrice();}
     }
  }
  tps = openpricesell-(OrdersTotalMagicSell(MagicSell)*AutomatedTPFactor*Point);
  double ask = MarketInfo(Symbol(),MODE_ASK);    
  if(UseAutomatedTP==true&&profitsell>0){
    if(Ask<=tps)orderclosesell(ticketsell);    
  }
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions (from zero).
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

  3. You don't need two Magic numbers; one with OrderType() is sufficent.

    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: