Download MetaTrader 5

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


TypePendingOrderTriggered - expert for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

votes: 21
2017.03.22 16:09

The Expert Advisor shows an example of how to solve the following problem: How to determine the moment when a pending order has triggered?

How the expert advisor works: in the OnTradeTransaction() function, we wait for a transaction of type "TRADE_TRANSACTION_DEAL_ADD":


Adding a trade to history. It is performed as a result of order execution or an operation with the account balance

Once we "catch" such a transaction, we immediately use the "bln_find_order" flag to search for an order and assign the order ticket to the "ul_find_order" variable:

//| TradeTransaction function                                        |
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
//| TRADE_TRANSACTION_DEAL_*                                         |
//| The following fields in MqlTradeTransaction structure            |
//| are filled for trade transactions related to deals handling      |
//| and TRADE_TRANSACTION_DEAL_DELETE):                              |
//|  •deal - deal ticket;                                            |
//|  •order - order ticket, based on which a deal has been performed;|
//|  •symbol - deal symbol name;                                     |
//|  •type - trade transaction type;                                 |
//|  •deal_type - deal type;                                         |
//|  •price - deal price;                                            |
//|  •price_sl - Stop Loss price (filled, if specified in the order, |
//|  •based on which a deal has been performed);                     |
//|  •price_tp - Take Profit price (filled, if specified             |
//|   in the order, based on which a deal has been performed);       |
//|  •volume - deal volume in lots.                                  |
//|  •position - the ticket of the position that was opened,         |
//|   modified or closed as a result of deal execution.              |
//|  •position_by - the ticket of the opposite position.             |
//|   It is only filled for the out by deals                         |
//|   (closing a position by an opposite one).                       |
//--- get transaction type as enumeration value  
//--- if transaction is result of addition of the transaction in history
      bln_find_order=true;                // true -> you should look for a order

In OnTick(), we always check the state of the "bln_find_order" flag; as soon as the flag is equal to true, we start to search for the order.

  • First we try to find the order by ticket
  • If the attempt succeeds (which means that the order has already been written to history), we determine the order type. In order to access the property of an order from the trade history, use HistoryOrderGetInteger.

//| Expert tick function                                             |
void OnTick()
   if(bln_find_order) // true -> you should look for a order
      static long counter=0;
      Print("Attempt number ",counter);
         long type_order=HistoryOrderGetInteger(ul_find_order,ORDER_TYPE);
         if(type_order==ORDER_TYPE_BUY_LIMIT || type_order==ORDER_TYPE_BUY_STOP ||
            type_order==ORDER_TYPE_SELL_LIMIT ||type_order==ORDER_TYPE_SELL_STOP)
            Print("The pending order ",ul_find_order," is found! Type of order is ",
            bln_find_order=false;         // true -> you should look for a order
            Print("The order ",ul_find_order," is not pending");
            bln_find_order=false;         // true -> you should look for a order
         Print("Order ",ul_find_order," is not find, error#",GetLastError());

Translated from Russian by MetaQuotes Software Corp.
Original code:

XCCXCandleKeltner XCCXCandleKeltner

Keltner Channel built relative to the average value of the XCCX oscillator as a sequence of candlesticks


The STD indicator with the timeframe selection option available in input parameters

HistoryPositionInfo HistoryPositionInfo

Returns position profit in points based on the trading history.

CDir (MT5) - a class for getting directory contents CDir (MT5) - a class for getting directory contents

The CDir class allows getting information about files and folders outside the MQL5 sandbox similar to the MS-DOS Dir command. Call of system DLL is used, therefore you should allow their use.