Difference in setting magic number in mql5 versus mql4

I want to set the Magic Number in my robots in Metatrader 5 (MQL5). In MQL4 was easy, on OrderSend() I just put the Magic number in order to identify what orders are from each expert advisors (I want to use several different robots, and each robot can only close the positions the same robot open, not other orders),

Please help, I understand a little MQL5 and have made some robots, but I’m not sure how to set the Magic Number.

Thank you!

https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade/ctradesetexpertmagicnumber

Thank you. I define the trade object as:

“CTrade trade”

And just after make the trade (for example trade.BUY(XXX)), I set the Magic Number like this:

       	trade.SetExpertMagicNumber(00001);

Is ok to set the magic this way? I don’t have error when compiling but I’m not sure if the program is really doing this. Is there a way to know for sure if is set the magic number?

Since is not the same way as in MQL4, meaning I set the magic number after the trade was send and not while is set as in metatrader 4, I’m dubious if it’s ok.

Thanks!

You must set the Magic Number BEFORE the trade…

It doesn’t make sense doing it AFTER…

So I just add the same code I have before the trade, like this (sell case)?

trade.SetExpertMagicNumber(00001);

trade.Sell (VOLUME_LOT,NULL,m_symbol.Bid(), (m_symbol.Bid()+STOPLOSS,(m_symbol.Bid()-TAKEPROFIT) ,NULL);

Thanks!

#define EXPERT_MAGIC 123456   // MagicNumber of the expert
//+------------------------------------------------------------------+
//| Opening Sell position                                            |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request={0};
   MqlTradeResult  result={0};
//--- parameters of request
   request.action   =TRADE_ACTION_DEAL;                     // type of trade operation
   request.symbol   =Symbol();                              // symbol
   request.volume   =0.2;                                   // volume of 0.2 lot
   request.type     =ORDER_TYPE_SELL;                       // order type
   request.price    =SymbolInfoDouble(Symbol(),SYMBOL_BID); // price for opening
   request.deviation=5;                                     // allowed deviation from the price
   request.magic    =EXPERT_MAGIC;                          // MagicNumber of the order
//--- send the request
   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
//--- information about the operation
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  }
//+------------------------------------------------------------------+

Just read the documentation it’s all there.
https://www.mql5.com/en/docs/constants/tradingconstants/enum_trade_request_actions

Use this library.


Then you can set the magic in the same way as in MT4.