How to check reason SL or TP from deal

 

I want to check weather the deal in history is SL or TP. How do I track the deal from the order number that assigned when place order (name: ticket01).

I understand that 'HistoryDealGetTicket' parameter is 'Number of a deal in the list of deals'. But what is that?

      ulong deal = HistoryDealGetTicket(HistorySelectByPosition(ticket01));

      int reason = HistoryDealGetInteger(deal,DEAL_REASON);

I am very new to coding in general and here, Thank you for every suggestion and answer!

 
kotto_98:

I want to check weather the deal in history is SL or TP. How do I track the deal from the order number that assigned when place order (name: ticket01).

I understand that 'HistoryDealGetTicket' parameter is 'Number of a deal in the list of deals'. But what is that?

      ulong deal = HistoryDealGetTicket(HistorySelectByPosition(ticket01));

      int reason = HistoryDealGetInteger(deal,DEAL_REASON);

I am very new to coding in general and here, Thank you for every suggestion and answer!

Have a look at the deal tracking code directly below:

//--- select the total number of deals and orders

in:

Code Base

Show Positions on Custom Chart (or standard chart) for MT5

Ryan L Johnson, 2025.06.02 14:02

This indicator is a utility that shows labelled trade levels on any chart. If you want to replace your native trade levels on a native chart, then turn off "Show trade levels" in your F8 Chart Properties and attach this indicator. If you want to show trade levels on a Custom Chart (where native trade levels cannot be shown), then simply attach this indicator. BaseSymbol - specify the Symbol from which the trade level data will be pulled--handy for unique Custom Symbols. TextBarsBack - specify the number of bars back in history from the current bar where the level labels will be drawn. Note: Although magic number is referenced in the code, it is not presently included in the trade lines. If you're running multiple EA's on the same Symbol, you can edit the code to show magic numbers and then run multiple instances of the indicator on one chart. You can also edit the font sizes and text spacing in the object properties as needed for different display resolutions.

 

The field to read is DEAL_REASON on the closing deal, not anything derived from the position itself:

if(HistorySelectByPosition(positionId))
{
   for(int i = 0; i < HistoryDealsTotal(); i++)
   {
      ulong ticket = HistoryDealGetTicket(i);
      if(HistoryDealGetInteger(ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
      {
         ENUM_DEAL_REASON reason = (ENUM_DEAL_REASON)HistoryDealGetInteger(ticket, DEAL_REASON);
         if(reason == DEAL_REASON_SL) Print("Closed by Stop Loss");
         if(reason == DEAL_REASON_TP) Print("Closed by Take Profit");
      }
   }
}

HistorySelectByPosition() takes the position ID (not the ticket number the position opened with), which is why the OP's original HistorySelectByPosition(ticket01) call wasn't returning anything useful, ticket01 is an order ticket, not a position ID. Reference: DEAL_REASON docs.

Documentation on MQL5: Deal Properties / Constants, Enumerations and Structures
Documentation on MQL5: Deal Properties / Constants, Enumerations and Structures
  • www.mql5.com
A deal is the reflection of the fact of a trade operation execution based on an order that contains a trade request. Each trade is described by...