Question about the OnTradeTransaction function - page 7

 
votor:

Something I'm not good at explaining, apparently. Here's a real-life example. Here is the code:

void OnTradeTransaction(const MqlTradeTransaction& trans,

                        const MqlTradeRequest& request,

                        const MqlTradeResult& result)

  {

      Count++;   

  Print("Ontrade_test = ",Count);

 }

Обработчик стоит в двух советниках, при совершении 1 сделки он отрабатывается множество раз в двух советниках. Код выдает:

   18:31:06.495 ontrade_trans_functions (MXI-12.17,H1)  Ontrade_test = 1

   18:31:06.495 ontrade_trans_functions2 (MXI-12.17,H1) Ontrade_test = 1

   18:31:06.497 ontrade_trans_functions (MXI-12.17,H1)  Ontrade_test = 2

   18:31:06.497 ontrade_trans_functions2 (MXI-12.17,M5) Ontrade_test = 2

   18:31:06.498 ontrade_trans_functions (MXI-12.17,M5)  Ontrade_test = 3

   18:31:06.498 ontrade_trans_functions2 (MXI-12.17,H1) Ontrade_test = 3

   18:31:06.500 ontrade_trans_functions (MXI-12.17,M5)  Ontrade_test = 4

   18:31:06.500 ontrade_trans_functions2 (MXI-12.17,H1) Ontrade_test = 4 ...

and so on.

We can see that the response time of OnTradeTransaction in two Expert Advisors coincides to milliseconds. So, I have a question: does the "deal" event comes first to one OnTradeTransaction in one EA and then to the next one in another EA or does it somehow get to all handlers of all EAs at once? You know, like a parallel multi-threaded operation or whatever it is called in programming. I'm sure that everything happens sequentially, it is just processed within one millisecond, but I asked just in case.

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
   // Здесь надо поставить фильтр по типу транзакции
   // По символу и магику
   // и только если всё совпало, тогда 
      Count++;   
  Print("Ontrade_test = ",Count);
 }
 
votor:

Something I'm not good at explaining, apparently. Here's a real-life example. Here is the code:

void OnTradeTransaction(const MqlTradeTransaction& trans,

const MqlTradeRequest& request,

const MqlTradeResult& result)

{

Count++;

Print("Ontrade_test = ",Count);

}

The handler is implemented in two Expert Advisors so it is executed multiple times in two Expert Advisors when one trade is performed. The code produces:

18:31:06.495 ontrade_trans_functions (MXI-12.17,H1) Ontrade_test = 1

18:31:06.495 ontrade_trans_functions2 (MXI-12.17,H1) Ontrade_test = 1

18:31:06.497 ontrade_trans_functions (MXI-12.17,H1) Ontrade_test = 2

18:31:06.497 ontrade_trans_functions2 (MXI-12.17,M5) Ontrade_test = 2

18:31:06.498 ontrade_trans_functions (MXI-12.17,M5) Ontrade_test = 3

18:31:06.498 ontrade_trans_functions2 (MXI-12.17,H1) Ontrade_test = 3

18:31:06.500 ontrade_trans_functions (MXI-12.17,M5) Ontrade_test = 4

18:31:06.500 ontrade_trans_functions2 (MXI-12.17,H1) Ontrade_test = 4 ...

and so on.

You can see that the OnTradeTransaction response time in the two Expert Advisors is the same in milliseconds. So, I have a question: does the trade event comes first to one OnTradeTransaction in one EA and then to the next one in another EA or does it somehow get to all handlers of all EAs at once? Well, it's like a parallel multi-threaded operation or whatever it is called in programming. I'm sure that everything happens sequentially, it just works fast within one millisecond, but I asked just in case.


OnTradeTransaction handler for ONE symbol (m_symbol.Name()) and one magic (m_magic).

Catch the transaction first

TRADE_TRANSACTION_DEAL_ADD

Add the transaction to the history. This is done as a result of order execution or account balance transactions.


then we look to see if it is a trade

DEAL_ENTRY_OUT

Market exit


//+------------------------------------------------------------------+
//| 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)
     {
      long     deal_ticket       =0;
      long     deal_order        =0;
      long     deal_time         =0;
      long     deal_time_msc     =0;
      long     deal_type         =-1;
      long     deal_entry        =-1;
      long     deal_magic        =0;
      long     deal_reason       =-1;
      long     deal_position_id  =0;
      double   deal_volume       =0.0;
      double   deal_price        =0.0;
      double   deal_commission   =0.0;
      double   deal_swap         =0.0;
      double   deal_profit       =0.0;
      string   deal_symbol       ="";
      string   deal_comment      ="";
      string   deal_external_id  ="";
      if(HistoryDealSelect(trans.deal))
        {
         deal_ticket       =HistoryDealGetInteger(trans.deal,DEAL_TICKET);
         deal_order        =HistoryDealGetInteger(trans.deal,DEAL_ORDER);
         deal_time         =HistoryDealGetInteger(trans.deal,DEAL_TIME);
         deal_time_msc     =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC);
         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_magic        =HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason       =HistoryDealGetInteger(trans.deal,DEAL_REASON);
         deal_position_id  =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);

         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_price        =HistoryDealGetDouble(trans.deal,DEAL_PRICE);
         deal_commission   =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);
         deal_swap         =HistoryDealGetDouble(trans.deal,DEAL_SWAP);
         deal_profit       =HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_comment      =HistoryDealGetString(trans.deal,DEAL_COMMENT);
         deal_external_id  =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID);
        }
      else
         return;
      if(deal_reason!=-1)
         DebugBreak();
      if(deal_symbol==m_symbol.Name() && deal_magic==m_magic)
         if(deal_entry==DEAL_ENTRY_OUT)
           {

           }
     }
  }
 
Alexey Viktorov:

The eventOnTradeTransaction is a broadcast event, all the actions that your terminal performs are "dumped" there.

It is just that each EA has to filter its own actions.

void OnTradeTransaction( const MqlTradeTransaction &trans,
                         const MqlTradeRequest &request,
                         const MqlTradeResult &result )
{  
  switch(trans.type)
  {
    case TRADE_TRANSACTION_REQUEST:
    break;
    case TRADE_TRANSACTION_DEAL_ADD:
    break;
    case TRADE_TRANSACTION_HISTORY_ADD:
    break;
    case TRADE_TRANSACTION_ORDER_UPDATE:
    break;
  }  
}

Then in each type, we filter by ticket, if the order is sent asynchronously, then

first, we get the ticket by its request ID and then we filter by the ticket.

case TRADE_TRANSACTION_REQUEST:
  if((my_request_id > 0) && (result.request_id == my_request_id))
      {
        if(result.order > 0)
          {
            my_order_ticket = result.order;
          }
      }
break;

TheTRADE_TRANSACTION_REQUEST eventalways comes first, the others "as they want".

Added

This has been discussed many times.

Look it up in Exchange Trading

 

Thanks, seems to be getting the right deals "written down".