Who can help me fix my EA use TrappingPrice

 
I'm newbie. i have EA use TrappingPrice but in first time it correct but after then it have some problem, it not close pending order and run false 
Files:
 

Forum on trading, automated trading systems and testing trading strategies

Convert MQL4 Indicator To Mql5

Sergey Golubev, 2019.08.07 06:11

Just to remind - 

----------------

Coders (any coder) are coding for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members of this forum.

----------------

and Freelance section of the forum should be used in most of the cases.


 
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2.   if (OrdersTotal() == 0)
    ⋮
          Select = OrderSelect(0,SELECT_BY_POS);
    ⋮
      if (OrdersTotal()%2 == 0 && Bid == Zone3) //make sure SELLSTOP has done
    ⋮
      if (OrdersTotal()%2 == 1 && Ask == Zone2)  //make sure BUYSTOP has done
    
    Using OrdersTotal (or OrdersHistoryTotal) directly and/or no Magic number filtering on your OrderSelect 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
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  3. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum

  4.      for(int i = OrdersTotal(); OrdersTotal() > 0; i--)
         {
            Select = OrderSelect(i - 1,SELECT_BY_POS);
            if (Select){ … }
    
    Once i reaches zero you have an infinite loop. Your select will always be false, but your loop termination doesn't depend on i.

  5. 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:
    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
      For In First Out (FIFO rules-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order, close it, and on a successful operation, reprocess all remaining positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16

    2. and check OrderSelect in case earlier 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
    3. 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 (be direction independent and use) OrderClosePrice().

Reason: