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

Asked by Radhesh Joshi · SOLVED
1 reply 436 views Jump to solution Reply
Radhesh Joshi Original Post
Strategy Builder

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?

Posted Aug 4
Reply

    1 Reply

    Tech Support AlgoDelta
    Tech Support AlgoDelta Automation Expert ·

    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.

    Verified Solution
      Markdown supported · ```json for code blocks · paste or drop screenshots

      Be specific and kind. Never share API keys, passwords or personal account details.

      Welcome to the community

      Log in or create your account in under a minute.

      1. 1Mobile
      2. 2E-mail
      3. 3Password
      +91

      Have a password?

      By continuing you agree to the community guidelines. We’ll send a one-time code by SMS.