Home Forums MT4/MT5 Connection How can I detect whether a position was closed by Stop Loss or Take Profit in MT5?

How can I detect whether a position was closed by Stop Loss or Take Profit in MT5?

  • Author
    Posts
    • I’m developing an Expert Advisor (EA) in MT5 and I need to know why my previous trade was closed.

      For example, if a Buy position gets closed, I want my EA to detect whether it was closed because the Stop Loss was hit, the Take Profit was hit, or it was closed manually.

      Is there any way to identify the exact reason for a position being closed in MQL5?

    • Yes sir, you can definitely detect this in MQL5.

      The recommended way is to check the deals generated after a position is closed. MT5 stores the reason for every completed deal in the trading history, and you can read that information programmatically.

      The most commonly used property is:

      HistoryDealGetInteger(dealTicket, DEAL_REASON);

      This returns the reason why the deal was executed.

      Some commonly used values are:

      -DEAL_REASON_SL
      -> The position was closed because the Stop Loss was hit.

      -DEAL_REASON_TP
      -> The position was closed because the Take Profit was hit.

      -DEAL_REASON_EXPERT
      -> The position was closed by an Expert Advisor (EA).

      -DEAL_REASON_CLIENT
      -> The position was closed manually by the trader.

      -DEAL_REASON_SO
      -> The position was closed due to Stop Out.

      A simple example is shown below:

      ulong dealTicket = HistoryDealGetTicket(HistoryDealsTotal() - 1);
      
      ENUM_DEAL_REASON reason = (ENUM_DEAL_REASON)HistoryDealGetInteger(dealTicket, DEAL_REASON);
      
      if(reason == DEAL_REASON_SL)
      {
         Print("Position closed by Stop Loss");
      }
      else if(reason == DEAL_REASON_TP)
      {
         Print("Position closed by Take Profit");
      }
      else if(reason == DEAL_REASON_CLIENT)
      {
         Print("Position closed manually");
      }
      else if(reason == DEAL_REASON_EXPERT)
      {
         Print("Position closed by Expert Advisor");
      }

      Before reading the deal history, make sure you’ve selected the required history range using:

      HistorySelect(fromTime, TimeCurrent());

      This ensures that the completed deals are available for reading.

      This approach is very useful when you want to:

      -Calculate separate statistics for Stop Loss and Take Profit trades.

      -Trigger different logic after a winning or losing trade.

      -Send different webhook messages depending on how the previous trade was closed.

      -Generate detailed trading reports inside your EA.

      So, instead of guessing why a position was closed, you can directly read the deal reason from the MT5 trading history and handle each case accordingly.

Viewing 1 reply thread
×

Start a Discussion

Get help from the AlgoDelta community.

×

Welcome Back!

Enter your email to sign in or create an account. No passwords needed.

⚠️

Delete This?

Are you sure you want to delete this? This action is permanent and cannot be undone.

Scroll to Top