How to check deal type ?

 

I am trying to check deal type when a take profit triggered, i used deal.type method but it only return buy.

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result){
   CDealInfo deal;   
   if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
     {
      if(HistoryDealSelect(trans.deal))
         deal.Ticket(trans.deal);
      else
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: HistoryDealSelect(",trans.deal,")");
         return;
        }
      //---
      long reason=-1;
      if(!deal.InfoInteger(DEAL_REASON,reason))
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: InfoInteger(DEAL_REASON,reason)");
         return;
        }
      if((ENUM_DEAL_REASON)reason==DEAL_REASON_SL){        
         Alert("Stop Loss activation");
      }        
      else if((ENUM_DEAL_REASON)reason==DEAL_REASON_TP){
            double profit = deal.Profit();
            if(profit > 1.40 && profit < 1.60){
            if(deal.Type == DEAL_TYPE_BUY){
               printf("buy deal");
            }else if(deal.Type == DEAL_TYPE_SELL){
               printf("sell deal");
            }
         }
            Alert("Take Profit activation");
         }
     }
}
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Transaction Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Transaction Structure
  • www.mql5.com
Trade Transaction Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Use 

DealType

Gets the deal type

Documentation on MQL5: Standard Library / Trade Classes / CDealInfo / DealType
Documentation on MQL5: Standard Library / Trade Classes / CDealInfo / DealType
  • www.mql5.com
DealType - CDealInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Vladimir Karputov #:

Use 

DealType

Gets the deal type


Please check the code, i am already using DealType but the problem is it return buy even if it's a sell deal.

 
Shah Zeb # :

Please check the code, i am already using DealType but the problem is it return buy even if it's a sell deal.

Not!!! You are using

            if(deal.Type == DEAL_TYPE_BUY){

a, you need to use

DealType

Gets the deal type

Documentation on MQL5: Standard Library / Trade Classes / CDealInfo / DealType
Documentation on MQL5: Standard Library / Trade Classes / CDealInfo / DealType
  • www.mql5.com
DealType - CDealInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov #:

Not!!! You are using

a, you need to use

DealType

Gets the deal type

Now i am using DealType() but still i am getting buy type for sell deals.

***

 
Shah Zeb #:

Now i am using DealType() but still i am getting buy type for sell deals.

***

Please, insert code correctly: when editing a post, click  Code and paste your code in the popup window (the first time I edited your post and inserted the code correctly)
MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 

Use code:

//+------------------------------------------------------------------+
//|                                                      Expert1.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
#include <Trade\DealInfo.mqh>
//---
CDealInfo      m_deal;                       // object of CDealInfo class
//--- input parameters

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
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)
     {
      ResetLastError();
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);
      else
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryDealSelect(",trans.deal,") error: ",GetLastError());
         return;
        }
      ENUM_DEAL_TYPE deal_type   = m_deal.DealType();
      ENUM_DEAL_ENTRY deal_entry = m_deal.Entry();
      if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)
        {
         long deal_reason=-1;
         if(!m_deal.InfoInteger(DEAL_REASON,deal_reason))
           {
            Print(__FUNCTION__,", ERROR:  InfoInteger(DEAL_REASON ...");
            return;
           }
         if((ENUM_DEAL_REASON)deal_reason==DEAL_REASON_SL)
           {
            Print(__FUNCTION__,", DEAL_REASON_SL");
            return;
           }
         if((ENUM_DEAL_REASON)deal_reason==DEAL_REASON_TP)
           {
            Print(__FUNCTION__,", DEAL_REASON_TP");
            return;
           }
        }
     }
  }
//+------------------------------------------------------------------+
Files:
Expert1.mq5  4 kb
Reason: