What so special about magic numbers?

 

Great!

Now before you come after me because of the title, please hear me out first.

I got into programming EAs and when I heard the words  "magic number", I was intrigued and wanted to know what they represented.

So I looked up some codes on codebase and the forum and my conclusion so far is that it can be used to identify trades that were placed by the Expert advisor.

Originally, I have been using this POSITION_REASON to  identify when trades were opened by an EA.

long reason = PositionGetInteger(POSITION_REASON);
   
      if(reason  ==  3)
//---other code here this position was opened by an EA (You get the point)

So apart from what I have stated, is there any other particular reason why we use magic numbers?

Also can you please let me know if there are any downsides to the current method I use in identifying positions opened by an EA.

Cheers.

 
Yes, the "magic number" is so that an EA is able to identify trades between various EAs that may be operating at the same time, or even the same EA that is being used on different time-frames (provided it also filters by symbol).
 
The ticket number is the reference of your broker of a position or an order, the magic number is/can be your reference or a position or an order. It's up to you whether and how you use it.
 
Fernando Carreiro #:
Yes, the "magic number" is so that an EA is able to identify trades between various EAs that may be operating at the same time, or even the same EA that is being used on different time-frames (provided it also filters by symbol).

I think I get it now.

My approach would behave poorly if the user has multiple EAs on the chart at the same time and  needs to identify only the trades made by a particular EA from the others.

I hope I understood correctly. Thanks for the clarification

 
Carl Schreiber #:
The ticket number is the reference of your broker of a position or an order, the magic number is/can be your reference or a position or an order. It's up to you whether and how you use it.

Thanks for the info!

 

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)
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
          Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
          Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

You need one Magic Number for each symbol/timeframe/strategy.
     Trade current timeframe, one strategy, and filter by symbol requires one MN.
     If trading multiple timeframes, and filter by symbol requires use a range of MN (base plus timeframe).
          Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)

Reason: