EA close order issue - Somebody help pls

 

Hi All!

I'm making my EA as a Copier. Everything is going well until now. EA is now running good in master mode, but in slave mode could only open orders and modify changes, but can not close orders by itself when master closed. Please help me to debug my code below:

P/S: When i close orders in master, nothing happens in slave. Sometimes i got the "Zero divide" in Experts message panel...

Thank you!

      for(int i=0; i<OrdersTotal(); i++)
        {
         if(!OrderSelect(i,SELECT_BY_POS)) continue;

         if(StringSubstr(OrderComment(),0,3)!="Orders") continue;

         if(InList(StrToInteger(StringSubstr(OrderComment(),StringLen("Orders"),0)))==-1)
           {
            if(OrderType()==0)
              {
               if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),slip))
                  Print("Error: ",GetLastError()," during closing the order.");
              }
            else if(OrderType()==1)
              {
               if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),slip))
                  Print("Error: ",GetLastError()," during closing the order.");
              }
            else if(OrderType()>1)
              {
               if(!OrderDelete(OrderTicket()))
                  Print("Error: ",GetLastError()," during deleting the pending order.");
              }
           }
        }
 
  1.       for(int i=0; i<OrdersTotal(); i++)

    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 a position 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.
                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().

  2. Do you really want to close/delete all orders on all charts?

    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.02.15)
              PositionClose is not working - MQL5 programming forum (2020.02.21)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (24 July 2006
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (1 February 2011

  3.             if(OrderType()==0)
                  {
                   if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),slip))
                      Print("Error: ",GetLastError()," during closing the order.");
                  }
                else if(OrderType()==1)
                  {
                   if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),slip))
                      Print("Error: ",GetLastError()," during closing the order.");
                  }

    You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.

  4.          if(StringSubstr(OrderComment(),0,3)!="Orders") continue;
    

    Not a good idea to use comments, brokers can change comments, including complete replacement.