How to detect why an order closed in MQL5?

 

There are several reasons why an order may have Closed:

  Unknown = -1,

        Expiration = 0,

        Market = 1,

        TakeProfit = 2,

        StopLoss = 3,

        Risk = 4


How can you detect why an order closed in MQL4?

 
Don Baechtel:

There are several reasons why an order may have Closed:

  Unknown = -1,

        Expiration = 0,

        Market = 1,

        TakeProfit = 2,

        StopLoss = 3,

        Risk = 4


How can you detect why an order closed in MQL4?

https://www.mql5.com/en/docs/event_handlers/ontradetransaction


Example:

void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value 
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      long     deal_entry        =0;
      long     deal_type         =0;
      double   deal_price        =0.0;
      double   deal_profit       =0.0;
      double   deal_volume       =0.0;
      string   deal_symbol       ="";
      long     deal_magic        =0;
      int      deal_reason       =0;
      if(HistoryDealSelect(trans.deal))
        {
         deal_entry=HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_type=HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_price=HistoryDealGetDouble(trans.deal,DEAL_PRICE);
         deal_profit=HistoryDealGetDouble(trans.deal,DEAL_PROFIT);
         deal_volume=HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_symbol=HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_magic=HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason=HistoryDealGetInteger(trans.deal,DEAL_REASON);
        }
      else
         return;

      if(deal_magic==magic)

         if(deal_entry==DEAL_ENTRY_IN)
            {

              {

              }
            if(deal_type==DEAL_TYPE_SELL)
              {

              }
              

           }
           
      if(deal_entry==DEAL_ENTRY_OUT && DEAL_REASON_TP) 
      { 



      }
                       
           
      if(deal_entry==DEAL_ENTRY_OUT)
      {
      

          
         if(deal_type==DEAL_TYPE_BUY) 
           {
           }
         if(deal_type==DEAL_TYPE_SELL) 
           {
           }

       if (deal_reason==DEAL_REASON_SL)
        {
         if(deal_type==DEAL_TYPE_BUY) 
           {
           }
         if(deal_type==DEAL_TYPE_SELL) 
           {
           }
        }
      }
    }
  }
Reason: