Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal


TradeTransaction Class - library for MetaTrader 5


votes: 9
2019.03.06 01:31
2019.03.07 16:32

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
                     CTradeTransaction(void)  {   }
                    ~CTradeTransaction(void)  {   }
   //--- event handler
   void              OnTradeTransaction(const MqlTradeTransaction &trans,
                                        const MqlTradeRequest &request,
                                        const MqlTradeResult &result);
   //--- 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;

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