magic number for each position ?

 

hello

is it possible in mql5 to set a different magic number for each position ? 

i searched but i cant seem to find how can i do it 

anyone can help on this ?

thank you 

 
max s: is it possible in mql5 to set a different magic number for each position? i searched but i cant seem to find how can i do it. anyone can help on this ?

Yes! Just set a different magic number for each new order sent.

However, why do you need that?

Each position already has a unique ticket number, so why apply a unique magic number as well?

It will just be redundant!

 
max s: is it possible in mql5 to set a different magic number for each position ?

Yes and unnecessary.

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.

 

thank you for your reply

i am just trying to id each position opened with my EA and separate it from other already opened positions or from position that will be opened later by the user

i dont want to loop through all opened positions and compare open time and so on

any easier way to do that ?

 
max s #: i am just trying to id each position opened with my EA and separate it from other already opened positions or from position that will be opened later by the use. i dont want to loop through all opened positions and compare open time and so on.any easier way to do that ?

You still have to loop through all open positions anyway to search for a magic number. I suggest you rethink your code logic again.

Positions opened manually by the user will have a magic number of "0". EAs usually only use one magic number to identify its own trades, and then use the positions ticket number for further processing.

In order to prevent "looping", just save the ticket number after you have opened a position and reference later when you need it.

 
Fernando Carreiro #: In order to prevent "looping", just save the ticket number after you have opened a position and reference later when you need it.

EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

Use a OrderSelect / Position select loop on the first tick, or persistent storage (GV+flush or files) of ticket numbers required.

 
The way my MQL4 are written, rely heavily on Magic Number. It may not be of important and redundant to 99% of traders, but for my type trading, its very important. I am trying to convert my code to MQL5, and this issue is another complication. So I am searching myself for an easy way to convert MQL4 to MQL5. Magic number is crucial to what I do. Thanks
 
gebarinsllc #:
The way my MQL4 are written, rely heavily on Magic Number. It may not be of important and redundant to 99% of traders, but for my type trading, its very important. I am trying to convert my code to MQL5, and this issue is another complication. So I am searching myself for an easy way to convert MQL4 to MQL5. Magic number is crucial to what I do. Thanks

What issue exactly are you facing 

 
gebarinsllc #: The way my MQL4 are written, rely heavily on Magic Number. It may not be of important and redundant to 99% of traders, but for my type trading, its very important. I am trying to convert my code to MQL5, and this issue is another complication. So I am searching myself for an easy way to convert MQL4 to MQL5. Magic number is crucial to what I do. Thanks

Both MQL4 and MQL5 support Magic numbers and they work almost exactly the same. There should be no difficulty converting to magic number management to to MQl5.

 
Use -1 Magic Number , Its Auto Change Every Order
Reason: