How to check if a position has been closed with MQL5?

 

In the following snippet, I'm trying to print the details of closed positions. 

However, when I tested this snippet by placing an order and close it manually on MT5, 

this OnTradeTransaction function not get triggered. Is anything wrong in this code?

//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
//---       
      if (trans.type == TRADE_TRANSACTION_DEAL_DELETE)
      {         
         if (trans.deal != NULL)
         {
            // Position closed
                     
            if (trans.type == ORDER_TYPE_BUY_LIMIT)
               Print("Buy Position closed. Symbol:", request.symbol, ", Order:", trans.order, ", Deal:", trans.deal, ", Position:",trans.position);
            if (trans.type == ORDER_TYPE_SELL_LIMIT)
               Print("Sell Position closed. Symbol:", request.symbol, ", Order:", trans.order, ", Deal:", trans.deal, ", Position:",trans.position);
         }
      }   

  } 

Improperly formatted code edited by moderator.

 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  2. Limit orders are pending orders, not deals.

 
Yu Song: In the following snippet, I'm trying to print the details of closed positions. 

Please always use the CODE button (Alt-S) when inserting code.

Code button in editor

 

William Roeder

Thank you for your reply. 

I tested my code using MQL5 debugging mode and found out that when a position gets closed the OnTradeTransaction() function not triggered.

Can anyone explain why this is so?