Multi Symbol, Magic Number & repeat orders

 

I have this function which checks whether there are open trades using a certain magic number. The problem is I intend on using this EA for multiple currencies at once. Each order using a certain magic number depends on certain criteria being met but other criteria could appear in other currency pairs. How can I tailor this code to check magic numbers but only for the same currency pairs?

 
bool CheckIfOpenOrdersByMagicNB(int magicNB)
  {
      int openOrders = OrdersTotal();
   
      for(int i = 0; i < openOrders; i++)
      {
         if(OrderSelect(i,SELECT_BY_POS)==true)
         {
            if(OrderMagicNumber() == magicNB) 
            {
               return true;
            }  
         }
      }
      return false;
   } 
 
luxecapital:

I have this function which checks whether there are open trades using a certain magic number. The problem is I intend on using this EA for multiple currencies at once. Each order using a certain magic number depends on certain criteria being met but other criteria could appear in other currency pairs. How can I tailor this code to check magic numbers but only for the same currency pairs?

I would say the Solution in your problem is not in the code where when you are searching for the magic number; but rather how you create the magic number itself.

Per example: using 10000 as a base for this expert. Increments of 100 are used to identify the pair. (10100 EUR/USD, 10200, GPB/USD, and so on)
Then, when you want to look for all expert trades on different charts you use the range of the Magic Numbers. (Ex: 10000 to 11000).

That way, you can identify trade over all charts per ea, and the refine your search by pair.

There may be a better way to do it; but this is how I actually do it.

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

You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

 
So you need a únique Magic number for each trade… generate a MN… what I use is to get char for pairs I trade… and transform in int and add them toghether … add also _Period on this number… it will be a big number number unique
 
Why? You can even select.the symbol
Reason: