OnTrade() called 3 times in row after each deal

 

Have you noticed that OnTrade() is called 3 times in row after each deal?

This doesn't affect me because the way my routine is written, but this should be treated as dysfunction.

 

Documentation says:

 The function is called when the Trade event occurs, which appears when you change the list of placed orders and open positions, the history of orders and history of deals.

It seems that here lies the problem: each of these list changes after a trade, so it is called after each change.

 

Comments?

 
graziani:

Have you noticed that OnTrade() is called 3 times in row after each deal?

This doesn't affect me because the way my routine is written, but this should be treated as dysfunction.

 

Documentation says:

 The function is called when the Trade event occurs, which appears when you change the list of placed orders and open positions, the history of orders and history of deals.

It seems that here lies the problem: each of these list changes after a trade, so it is called after each change.

 

Comments?

I don't see a problem, this is how events are working. 1 order results in 1 deal (usually) and 3 trades events (sometimes more or less ?).

Anyway it's now better to use OnTradeTransaction() who provides directly 3 structures with all needed information and can results up to 5 events.

 
angevoyageur:

I don't see a problem, this is how events are working. 1 order results in 1 deal (usually) and 3 trades events (sometimes more or less ?).

Anyway it's now better to use OnTradeTransaction() who provides directly 3 structures with all needed information and can results up to 5 events.

How can an order result with more then one deal?

What 3 trade events? There is only one trade event, which results in update of three list/tables (or more).

The only reason i see for using OnTrade() is to avoid checking if a position is closed outside of program in every OnTick() pass.

For OnTradeTransactions() i don't see any usage (unless it's called only once, i will check it).

But i guess i should use your logic: it works that way, don't get upset about it. ;) 
So from now on, there are two angevoyageurs on the forum! :)

 

 
graziani:

How can an order result with more then one deal?

What 3 trade events? There is only one trade event, which results in update of three list/tables (or more).

The only reason i see for using OnTrade() is to avoid checking if a position is closed outside of program in every OnTick() pass.

For OnTradeTransactions() i don't see any usage (unless it's called only once, i will check it).

But i guess i should use your logic: it works that way, don't get upset about it. ;) 
So from now on, there are two angevoyageurs on the forum! :)

 

You can't give your own definition of what a "trade event" is, you have to follow how it's defined by the developers. As I said OnTradeTransaction() isn't called only once but multiple times for each "action". There are very interesting usages for OnTradeTransaction() . OnTrade() is now useless.

Anyway it works well, as documented, and in a logical way. It could probably be improved, but it's another matter. Sometimes you can miss something, it happens to the bests.

 
Alain Verleyen:

OnTradeTransaction() isn't called only once but multiple times for each "action". 

I know that this thread is old and out dated, however but to those learning the language it is new so I thought this poorly written code might help with some aspects of the initial questionasked

//--- input parameters
int      OnTradecounter = 0;
int      OnTransactioncounter = 0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---reset the counters 
   OnTradecounter = 0;
   OnTransactioncounter = 0;
  }
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
//---increase counter to print number of times OnTrade is executed
   OnTradecounter++;
   printf("OnTrade Counter: %d", OnTradecounter);
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
   //---increase counter to print number of times OnTransaction is executed and indicating the transaction type
  OnTransactioncounter++;
   printf("Transaction Counter: %d  and trasaction: %s", OnTransactioncounter,EnumToString(trans.type));
  }
//+------------------------------------------------------------------+
Reason: