Watch how to download trading robots for free
Find us on Facebook!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

TradeTransaction Class - library for MetaTrader 5

Views:
8308
Rating:
(32)
Published:
2019.03.06 01:31
Updated:
2021.04.27 20:45
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

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.


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

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



    Daily Percent Change Daily Percent Change

    This indicator uses the open price for the day, and computes the percent change for that day. Use on any chart TF up to and including D1.

    Average trend - multi time frame Average trend - multi time frame

    Short description.

    Vortex 2 Vortex 2

    Vortex 2

    Simple moving average Simple moving average

    Simple moving average