Set Magic Number in MQL5

 

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!

 
patagonia2015:

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
Documentation on MQL5: Standard Library / Trade Classes / CTrade / SetExpertMagicNumber
Documentation on MQL5: Standard Library / Trade Classes / CTrade / SetExpertMagicNumber
  • www.mql5.com
Standard Library / Trade Classes / CTrade / SetExpertMagicNumber - Reference on algorithmic/automated trading language for MetaTrader 5
 

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!



 
patagonia2015:

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...

 
Minions Labs:

You must set the Magic Number BEFORE the trade...

It doesn't make sense doing it AFTER...

Minions Labs, 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.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trading is done by sending orders to open positions using the OrderSend() function, as well as to place, modify or delete pending orders. Each trade order refers to the type of the requested operation. Trading operations are described in the ENUM_TRADE_REQUEST_ACTIONS enumeration...
Reason: