OnTradeTransaction missing magic numbers

 
#include <Trade\Trade.mqh>

CTrade m_trade;
COrderInfo m_order;
CPositionInfo m_position; 


void OnStart()
  {
    m_trade.SetExpertMagicNumber(1 );
  
   m_trade.Buy(0.01,Symbol(),0,0,0,"script");

  }
//+------------------------------------------------------------------+

This script places a buy market execution with a magic number of 1. 


// Expert Advisor
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
{
 
 
  //--- type of transaction
  ENUM_TRADE_TRANSACTION_TYPE trans_type = trans.type;
  if (TRADE_TRANSACTION_REQUEST == trans_type)
  {
    
      Print("MqlTradeTransaction: REQUEST" +
            " Symbol: " + request.symbol +
            " Type " + convert_int_to_OrderType(request.type) +
            " Order " + request.order +
            " Action " + request.action +
            " Price " + request.price +
            " Volume " + request.volume +
            " Magic " + request.magic +
            " Position Ticket " + request.position +
            " TP " + request.tp +
            " SL " + request.sl

      );
    
   }
}

//+------------------------------------------------------------------+
string convert_int_to_OrderType(int inputValue)
{
  if (0 == inputValue)
  {
    return ("BUY");
  }
  if (1 == inputValue)
  {
    return ("SELL");
  }
  if (2 == inputValue)
  {
    return ("BUYLIMIT");
  }
  if (3 == inputValue)
  {
    return ("SELLLIMIT");
  }
  if (4 == inputValue)
  {
    return ("BUYSTOP");
  }
  if (5 == inputValue)
  {
    return ("SELLSTOP");
  }

  return ("UNKNOWN ORDER TYPE: " + inputValue);
}

This is an expert advisor that monitors the OnTradeTransaction "Request"


When the script is run, the expert advisor will print out the following 

MqlTradeTransaction: REQUEST Symbol: BTCUSD Type BUY Order 2478343 Action 1 Price 0.0 Volume 0.01 Magic 1 Position Ticket 0 TP 0.0 SL 0.0


and when the position is closed, the expert advisor will print out the following

MqlTradeTransaction: REQUEST Symbol: BTCUSD Type SELL Order 2478344 Action 1 Price 0.0 Volume 0.01 Magic 0 Position Ticket 2478343 TP 0.0 SL 0.0


Why does the magic number equal 0, at the request to close the position? 



 
torytory: Why does the magic number equal 0, at the request to close the position? 

You didn't provide one when closing.

 
William Roeder:

You didn't provide one when closing.

GUI doesn't have an input to provide a magic number when closing a position. 

 

Trade server is smart enough to know the position Ticket that corresponds with the closing positions, why couldn't the magic number also be included? 


OPEN  REQUEST Symbol: BTCUSD Type BUY Order 2478343Action 1 Price 0.0 Volume 0.01 Magic 1 Position Ticket 0 TP 0.0 SL 0.0



CLOSE REQUEST Symbol: BTCUSD Type SELL Order 2478344 Action 1 Price 0.0 Volume 0.01 Magic 0 Position Ticket 2478343 TP 0.0 SL 0.0

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Transaction Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Transaction Structure
  • www.mql5.com
For example, when sending a market buy order, it is handled, an appropriate buy order is created for the account, the order is then executed and removed from the list of the open ones, then it is added to the orders history, an appropriate deal is added to the history and a new position is created. All these actions are trade transactions...
 

If you manually close a position, magic will always be '0'.

 
Also a mistake: you don’t need transaction 'TRADE_TRANSACTION_REQUEST' - you need transaction 'TRADE_TRANSACTION_DEAL_ADD'.
 
William Roeder:

You didn't provide one when closing.

Can you explain your answer please. Which function/method should be used to set a magic when closing?

 
Did you solve this issue?, I have the exact same problem and it's making me spend weeks not having a solution for it and ruining  my whole process int the EA.
 
It was a pain to figure out, as there is no magic passed except with request. so you will have to get the magic number yourself with the ticket (trans.position or trans.order) from either history or current open positions.  
Reason: