EA doesn't work properly when attached to multiple charts (MT4)

 

Hi all, I have asked a few questions in the forum lately and very much appreciate the responses I have gotten.  I am hoping for the same kind of luck again.

 

I have written an EA that works perfectly on Strategy Tester.  But I when open it in my live account on two different charts (two different symbols, same time frame), the EA doesn't behave properly for the 2nd chart.  It works fine on the 1st chart but malfunctions on the 2nd.

 

This is in MT4. I have scoured my code and don't see anything that looks to be causing the problem.  But clearly there must be something.

 

The EA is quite simple.  In a nutshell, when the EA is attached it opens two orders:  a buy stop and a sell stop.  Then, on each new bar, it adjusts the entry and stop levels according to specific criteria.

 

The EA has two highly similar OrderSelect() loops: one to count open orders/positions and one to make adjustments as necessary to entry and stop prices.

 

It is this 2nd part, the adjustments, in which the problem occurs for the 2nd chart.

 

In the 1st OrderSelect() loop (for counting orders) everything functions properly. I am printing the relevant trade functions, and can see that they are correct for both the 1st and 2nd charts.  And I have been able to verify that the totalOrders variable is counting correctly, which means OrderMagicNumber() matches my variable MagicNumber, and OrderSymbol() matches _Symbol.  No sign of trouble in the first OrderSelect() loop.

 

int openOrders()

{

   int totalOrders = 0;

   

   for(int order = 0; order < OrdersTotal(); order++) //if OrdersTotal = 0, loop doesn't even need to run and totalOrders will be zero

   {

      select = OrderSelect(order,SELECT_BY_POS,MODE_TRADES);

      

         Print("(openOrders loop) Magic Number: ",MagicNumber," - OrderMagicNumber(): ",OrderMagicNumber());

         Print("(openOrders loop) Magic Number: ",MagicNumber," - OrderSymbol(): ",OrderSymbol());

         Print("(openOrders loop) Magic Number: ",MagicNumber," - _Symbol: ",_Symbol);

         Print("(openOrders loop) Magic Number: ",MagicNumber," - OrderType(): ",OrderType());

         Print("(openOrders loop) Magic Number: ",MagicNumber," - OrderTicket(): ",OrderTicket());

         Print("(openOrders loop) Magic Number: ",MagicNumber," - OrderOpenPrice(): ",OrderOpenPrice());

         Print("(openOrders loop) Magic Number: ",MagicNumber," - OrderStopLoss(): ",OrderStopLoss());

 

      if(OrderMagicNumber() == MagicNumber && OrderSymbol() == _Symbol)

         {

            totalOrders++;

         }

   }

   

   return(totalOrders);

}

 

The 2nd OrderSelect() loop (for adjusting prices) begins almost identically to the 1st (I haven't pasted the entire loop here).  But here is where the problems arise.  From the printing I can see that for the 1st chart all trade function values are exactly right.  But for the second chart it can't seem to find to find the trade function values - most relevantly OrderMagicNumber() and OrderSymbol().  As a result, it skips the entire loop and no adjustments occur to entry and stop prices.  In fact the EA thinks there are no open orders for that symbol (the variables buyOrders and sellOrders remain at zero) and therefore it opens another pair of buy stop and sell stop orders.

 

            if(orderCount > 0)

            {

            

               int buyOrders = 0;

               int sellOrders = 0;

 

               for(int order = 0; order < orderCount; order++)

               {

                  select = OrderSelect(order,SELECT_BY_POS,MODE_TRADES);

                  

                     Print("(orderSelect loop) Magic Number: ",MagicNumber," - OrderMagicNumber(): ",OrderMagicNumber());

                     Print("(orderSelect loop) Magic Number: ",MagicNumber," - OrderSymbol(): ",OrderSymbol());

                     Print("(orderSelect loop) Magic Number: ",MagicNumber," - _Symbol: ",_Symbol);

                     Print("(orderSelect loop) Magic Number: ",MagicNumber," - OrderType(): ",OrderType());

                     Print("(orderSelect loop) Magic Number: ",MagicNumber," - OrderTicket(): ",OrderTicket());

                     Print("(orderSelect loop) Magic Number: ",MagicNumber," - OrderOpenPrice(): ",OrderOpenPrice());

                     Print("(orderSelect loop) Magic Number: ",MagicNumber," - OrderStopLoss(): ",OrderStopLoss());

                     Print("(orderSelect loop) Magic Number: ",MagicNumber," - orderCount: ",orderCount);

 

                  if(OrderMagicNumber() == MagicNumber && OrderSymbol() == _Symbol)

                  {

 

I can't imagine why the 1st OrderSelect() loop works and the 2nd doesn't.  The 2nd OrderSelect() loop follows the 1st directly with no other processing in between.  So between those two loops I don't see any other part of the program that would be creating a problem. The EA is not very complicated.  Effectively, first it counts open orders and then it makes adjustments to entry and stop prices.

 

Again it is only the 2nd chart.  The 1st chart executes flawlessly.  For example, if I attach it 1st to EURUSD and 2nd to USDJPY, EURUSD will perform as expected and USDJPY will have problems.  If I attach them in reverse order, USDJPY will perform as expected and EURUSD will have problems.

 

I am using different Magic Numbers for each chart (although from what I have read, I'm not sure it's necessary).

 

Also I have tried using two different EAs on each of the two charts (i.e. the same code but saved as different files with different names) with two different Magic Numbers, but exactly the same problem occurs.

 

Has anyone run into the situation and found a solution for it?  I have spent many hours trying to figure it out.

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 
  1. Why did you post your MT4 question in the MT5 EA 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. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  3. 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 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.
                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().

 
Thanks for these insightful comments and links William, including about how to post in the forum.