OnTradeTransaction missing magic numbers

To add comments, please log in or register
torytory
193
torytory  
#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? 



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

You didn't provide one when closing.

torytory
193
torytory  
William Roeder:

You didn't provide one when closing.

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

torytory
193
torytory  

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...
Vladimir Karputov
Moderator
201553
Vladimir Karputov  

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

Vladimir Karputov
Moderator
201553
Vladimir Karputov  
Also a mistake: you don’t need transaction 'TRADE_TRANSACTION_REQUEST' - you need transaction 'TRADE_TRANSACTION_DEAL_ADD'.
To add comments, please log in or register