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!
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.
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.
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
What issue exactly are you facing
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.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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