MQL4 mxing up which trade goes with which EA?

 

I have 2 EAs - one to do buys, one to do sells for this strategy I'm playing with. In the EA log file I'm getting the wierdest logs that seem to show that the platform is confusing which EA initiated the trade.

Log excerpts are below. These are the lines automatically generated by the platform - they are not generated by code in the EA.


06:56:33 HyperBasketRDae_v1.6 SELL EURUSD,H1: open #64452298 sell 0.16 EURUSD at 1.3483 sl: 1.3979 tp: 1.3460 ok
06:58:38 HyperBasketRDae_v1.6 BUY EURUSD,Daily: open #64457457 sell (instead of buy) 0.64 EURUSD at 1.3542 sl: 1.4046 tp: 1.3511 ok

06:58:39 HyperBasketRDae_v1.6 SELL EURUSD,H1: open #64457479 buy (instead of sell) 0.02 EURUSD at 1.3545 sl: 1.3061 tp: 1.3556 ok


The first line shows the way it is supposed to be.

The second and third line show something I can't explain and need help to identify. The BUY EA is logged as opening the sell with "(instead of buy)" added to it. At the same time, the Sell EA did not seem to get a valid trade ticket number back, which caused it to open the trade again at the same size, which is a big problem for me.

Same thing with the third line, only in reverse.


So, it seems like, somehow, the platform is getting the trades mixed up, but yet, seems to know this by marking it with the "( instead of buy )". Help!

 
do you 2 EA's have magic numbers? if not this should solve the problem...code magic numbers into them
 
23510:
do you 2 EA's have magic numbers? if not this should solve the problem...code magic numbers into them

They do have different Magic Numbers. I guess I could test to see if that trade was opened back by asking for the trades with that magic number - as a work around. But I'd still like to learn why the heck this is going on.

 
ryanhunt wrote >>

They do have different Magic Numbers. I guess I could test to see if that trade was opened back by asking for the trades with that magic number - as a work around. But I'd still like to learn why the heck this is going on.

Usual problem is loose coding at the point of looping through the Orders.

Lots of people forget to check Magic Number at some point, should be something liike

  void CloseAllBuyOrders(int MN)
  {
  int i, iTotalOrders;
  
  //iTotalOrders=OrdersTotal(); 
  //for (i=0; i<iTotalOrders; i++)
   
   iTotalOrders=OrdersTotal()-1; // Rosh line
  
   for (i=iTotalOrders; i>=0; i--) // Rosh line     
   
   { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      { 
         if (OrderMagicNumber()==MN)
         { 
            if (OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
            if (OrderType()==OP_BUYSTOP) OrderDelete(OrderTicket());
            if (OrderType()==OP_BUYLIMIT) OrderDelete(OrderTicket());
            
         }
      }
   }
}

  void CloseAllSellOrders(int MN)
  {
  int i, iTotalOrders;
  
  // iTotalOrders=OrdersTotal(); 
  // for (i=0; i<iTotalOrders; i++)
   
   iTotalOrders=OrdersTotal()-1; // Rosh line
  
   for (i=iTotalOrders; i>=0; i--) // Rosh line  
   { 
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      { 
         if (OrderMagicNumber()==MN)
         { 
            if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
            if (OrderType()==OP_SELLSTOP) OrderDelete(OrderTicket());
            if (OrderType()==OP_SELLLIMIT) OrderDelete(OrderTicket());
         }
      }
   }
}

Good Luck

-BB-

 
ryanhunt:

I have 2 EAs - one to do buys, one to do sells for this strategy I'm playing with. In the EA log file I'm getting the wierdest logs that seem to show that the platform is confusing which EA initiated the trade.

Log excerpts are below. These are the lines automatically generated by the platform - they are not generated by code in the EA.

[...]

So, it seems like, somehow, the platform is getting the trades mixed up, but yet, seems to know this by marking it with the "( instead of buy )". Help!


In case it helps, I've seen something similar but not quite the same... Under heavy load such as hundreds of orders per hour I've repeatedly (and repeatably) had MetaTrader mix up the orders from different EAs running on different charts. For example, an EA running on a EURUSD chart places an order, and MetaTrader instead creates the order on a different symbol entirely (e.g. USDCHF). Not got the log files to hand any more, but they're absolutely categorical in showing a problem external to the EA. A typical entry would go like this:


06:56:33 MyEAWhatever SELL EURUSD,H1: open #64452298 sell 0.16 USDCHF at 1.3483 sl: 1.3979 tp: 1.3460 ok


I've not had a mix-up of buys and sells, but it sounds related. Both your example and my example could be a broker problem rather than a MetaTrader problem, but it's the sort of thing you tend to get in an application if your multi-threading isn't absolutely watertight.


I managed to fix my problem by implementing the recommendation at https://www.mql5.com/en/articles/1412 - the bit in sections 5 and 6. This has completely solved things for me, albeit at the cost of resorting to what's effectively co-operative multi-tasking, only allowing one EA to run at once.

Reason: