TradeTransaction Class – library MetaTrader 5

When performing some definite actions on a trade account, its state changes. Such actions include:

  • Sending a trade request from any MQL5 application in the client terminal using OrderSend and OrderSendAsync functions and its further execution;
  • Sending a trade request via the terminal graphical interface and its further execution;
  • activation of pending and stop orders on the server;
  • performing operations on the trade server side.

Special OnTradeTransaction() handler is provided in MQL5 to get trade transactions applied to an account.

void  OnTradeTransaction()
   const MqlTradeTransaction&    trans,     // trade transaction structure
   const MqlTradeRequest&        request,   // request structure
   const MqlTradeResult&         result     // response structure
   );

OnTradeTransaction() function can be used for:

  • Getting real-time notifications about trade transactions as they occur. 
  • Copying trades from one terminal to another.
  • Monitor trade transactions made by other ready-made expert advisors on different charts.
  • Having data on a trading operation type, you can decide on further analysis of the current state of orders, positions and deals on a trading account. (for example, update your calculations when the the list of open positions is changed).
  • Tracking the result of executing the trade request on a server sent by OrderSendAsync() function for conducting asynchronous trade operations without waiting for the trade server’s response to a sent request. 

The OrderSendAsync() function is designed for high-frequency trading, when under the terms of the trading algorithm it is unacceptable to waste time waiting for a response from the server.

Alternative:   Order Escort - EA MetaTrader 5

However, analysis of trade transations using the default OnTradeTransaction() handler provided in MQL5 seems complicated.

TradeTransaction class will map the underlying low-level trade transactions to a custom handler corresponding to the trade operation type. 

The class has the following methods:

//+------------------------------------------------------------------+
//| Class CTradeTransaction.                                         |
//| Purpose: Base class for trade transactions.                      |
//+------------------------------------------------------------------+
class CTradeTransaction
  {
public:
                     CTradeTransaction(void)  {   }
                    ~CTradeTransaction(void)  {   }
   //--- event handler
   void              OnTradeTransaction(const MqlTradeTransaction &trans,
                                        const MqlTradeRequest &request,
                                        const MqlTradeResult &result);
protected:
   //--- trade transactions
   //--- these methods should be overridden in the derived class
   virtual void      TradeTransactionOrderPlaced(ulong order)                      {   }
   virtual void      TradeTransactionOrderModified(ulong order)                    {   }
   virtual void      TradeTransactionOrderDeleted(ulong order)                     {   }
   virtual void      TradeTransactionOrderExpired(ulong order)                     {   }
   virtual void      TradeTransactionOrderTriggered(ulong order)                   {   }

   virtual void      TradeTransactionPositionOpened(ulong position, ulong deal)    {   }
   virtual void      TradeTransactionPositionStopTake(ulong position, ulong deal)  {   }
   virtual void      TradeTransactionPositionClosed(ulong position, ulong deal)    {   }
   virtual void      TradeTransactionPositionCloseBy(ulong position, ulong deal)   {   }
   virtual void      TradeTransactionPositionModified(ulong position)              {   }
  };

//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//---
   CTradeTransaction ExtTransaction;
//---
   ExtTransaction.OnTradeTransaction(trans,request,result);
  }
//+----

Finally this article makes an in-depth discussion about trade transactions in mql5  MQL5 Cookbook: Processing of the TradeTransaction Event

Alternative:   Three Tirone Levels - indicator MetaTrader 5

https://www.mql5.com/en/articles/1111


    📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks