OnTradeTransaction Event is triggered 4 times when closing one position

To add comments, please log in or register
Jorg1
1086
Jorg1  

Hi there,

this code is triggered 4 times when I'm closing one position.

It's important for me to do (// do something) in the following code only one time. But how?


void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result){

   // Select Deals from History
   datetime to = TimeCurrent();
   datetime from = to - PeriodSeconds(PERIOD_M5); // check deals for the last 5 minutes
   
   HistorySelect(from, to);
   int deals_total = HistoryDealsTotal();
   
   if(deals_total < 1){
      return;  
   }

newest_deal_ticket = HistoryDealGetTicket(deals_total - 1);

ulong deal_entry            = HistoryDealGetInteger(newest_deal_ticket, DEAL_ENTRY);
ulong deal_reason          = HistoryDealGetInteger(newest_deal_ticket, DEAL_REASON);
ulong deal_type            = HistoryDealGetInteger(newest_deal_ticket, DEAL_TYPE);

if((deal_entry == DEAL_ENTRY_OUT) && (deal_reason == DEAL_REASON_EXPERT) && ((deal_type == DEAL_TYPE_BUY ) || (deal_type == DEAL_TYPE_SELL)) ){
// do something
}

}
Alain Verleyen
40759
Alain Verleyen  
Of course it is. Read the documentation, some articles about transactions, and fix your code.
Vladimir Karputov
Moderator
200143
Vladimir Karputov  
Jorg1:

Hi there,

this code is triggered 4 times when I'm closing one position.

It's important for me to do (// do something) in the following code only one time. But how?


Пример:

//+------------------------------------------------------------------+
//| 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)
     {
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);
      else
         return;
      if(m_deal.Symbol()==m_symbol.Name() && m_deal.Magic()==InpMagic)
        {
         if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL)
           {
            if(m_deal.Entry()==DEAL_ENTRY_IN || m_deal.Entry()==DEAL_ENTRY_INOUT)
               m_last_deal_in=iTime(m_symbol.Name(),Period(),0);
To add comments, please log in or register