[SOLVED] Independent positions for same symbol with same EA and different magic numbers (MQL5)


Hi, I am trying to have multiple copies of the same EA run in the same symbol with different magic numbers.

I already found older topics that mention this but I did not find any solutions yet. 

I want one EA to know if for his magic number he is bought or sold, for instance.

For this, I use: 

bool updatePositionStatus() {
   int positions_total = 0;

   int ticket;

   for(int i=PositionsTotal()-1; i>=0; i--){
      if(PositionGetSymbol(i)==Symbol() && PositionGetInteger(POSITION_MAGIC)==MagicNumber /* global variable */){
         ticket = (int)PositionGetTicket(i);
         pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);

   if (positions_total == 1) { // EA developed to work with only a single position open 
      if (pos_type == POSITION_TYPE_BUY) {
         G_current_position_status = "bought";
      } else if (pos_type == POSITION_TYPE_SELL) {
         G_current_position_status = "sold";
      return true;

   if (positions_total > 1) { // not sure if needed, seems we can have only 1 position per symbol, but does not hurt to have this here 
      G_current_position_status = "INVALID";
      return false;

   G_current_position_status = "out";
   return true;

What happens is that different EAs with different Magic Numbers all see the same 1 position even if all of them have opened one independently. Then the Magic Number associated with that position seems to be either the first or last EA that created an order on that Symbol.

How can I decouple this so that one EA can easily tell if he is bought or sold, without needing to keep track of orders? I want to determine this programatically through MQL5's APIs in case I stop the bot and restart it with the same Magic Number, for instance. 

Note: I need to know if THAT EA is bought or sold. It's possible one EA for that symbol is bought and another one is sold, for instance.

Am I missing something obvious?

I also already read about PositionSelectByTicket and similars but I still do not see how I should operate differently for EAs to see different positions for the same symbol. Is this possible or is there any different approach I could take?


Kept researching and found this article: 


Class `CPositionVirtualMagic` helps solve the issue by implementing a Virtual Position based on magic by reading the orders history.

Seems to have sorted things for me.

The Use of ORDER_MAGIC for Trading with Different Expert Advisors on a Single Instrument
The Use of ORDER_MAGIC for Trading with Different Expert Advisors on a Single Instrument
  • www.mql5.com
In MQL5 we have a capability to assign a magic number to each pending order, in order to use this information to identify the order. This opens up the vast possibilities of interaction between different Expert Advisors, and the development of even more complex systems. In this article, I would like to inform the public about the undervalued...