Wrong DEAL_ENTRY Properties on Backtest

 
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
 
   if(trans.symbol!=Symbol())
      return;

   if(trans.type != TRADE_TRANSACTION_DEAL_ADD)
     {
      return;
     }

   ulong current_ticket = trans.deal;

   HistorySelectByPosition(trans.position);

   int num_in_history = HistoryOrdersTotal();

   ulong ticket  = HistoryDealGetTicket(0);

   ENUM_DEAL_TYPE deal_type = trans.deal_type;

   double deal_size = trans.volume;

   string label = HistoryDealGetString(ticket,DEAL_COMMENT);

   ulong deal_entry = HistoryDealGetInteger(ticket,DEAL_ENTRY);

   if(deal_entry == DEAL_ENTRY_OUT)
     {
      ....}

hello everybody,

I'm struggling on a problem with backtesting my code, when the function OnTradeTransaction is called the DEAL_ENTRY is never a DEAL_ENTRY_OUT (also when a trade is closed).

When launching the same EA in real time scenario it works and i get a DEAL_ENTRY_OUT when a trade is closed. Can anyone help me understand why this happens?

I appreciate any ideas.

Thanks

 

You are selecting historical deals related to a specific position:

   HistorySelectByPosition(trans.position);

Then you read ticket of the first deal which is always ENTRY_IN:

   ulong ticket  = HistoryDealGetTicket(0);

How do you expect this condition to be true?

   if(deal_entry == DEAL_ENTRY_OUT)
     {
      ....}
 
bro where you coding in mt4 platform or mt5?
 
SQN Trade #:
bro where you coding in mt4 platform or mt5?

The onTradeTransaction event handler is not supported in MT4.

 
Yashar Seyyedin #:

You are selecting historical deals related to a specific position:

Then you read ticket of the first deal which is always ENTRY_IN:


How do you expect this condition to be true?

Sorry, i posted the wrong code after lot of tries

ulong current_ticket = trans.deal;

   HistorySelectByPosition(trans.position);

   int num_in_history = HistoryOrdersTotal();

   ulong ticket  = HistoryDealGetTicket(0);
   
   ENUM_DEAL_TYPE deal_type = trans.deal_type;

   double deal_size = trans.volume;

   string label = HistoryDealGetString(ticket,DEAL_COMMENT);
   
   ulong deal_entry = HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
if(deal_entry == DEAL_ENTRY_OUT)
     {
...}

I use the historyDealGetTicket to effectively get the entry in label (variable ticket), but for the deal entry i use the trans.deal ticket (variable current ticket).

In real time scenario it works, the problem is only in backtest.

Thanks for your help

 

This is how I do this:

#include <Trade\DealInfo.mqh>
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
   if(trans.type!=TRADE_TRANSACTION_DEAL_ADD) return;
   if(!HistoryDealSelect(trans.deal)) return;
   CDealInfo      m_deal;                    
   m_deal.Ticket(trans.deal);

   long deal_type=-1;
   m_deal.InfoInteger(DEAL_ENTRY,deal_type);

   if(deal_type == DEAL_ENTRY_OUT)
   {...}
  }
 

This one executes faster:

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
   if(!HistoryDealSelect(trans.deal)) return;
   int deal_entry = HistoryDealGetInteger(trans.deal, DEAL_ENTRY);
   if(deal_entry == DEAL_ENTRY_OUT)
   {...}
  }