Please help. MQL5 EA sendmail function sends me repeat emails on every tick, how to make it send me an email only once per closed deal?

 

Hey MQL5 community, i have a problem with my expert advisor as you have seen in my subject. 

I put the sendmail function in the body of the ontick function to receive emails of the last deals symbol and profit/loss $ amount, but my problem is my EA sends me repeating emails on every new tick.

Can anyone help point me in the right direction on how to make my EA send me a email only once per closed deal?

Here is the code that i've placed in the body of the ontick function: 

//----------------------------------------------------------------------------------------------------------
//Send email alerts of last closed deals symbol & loss or profit amount in USD
//-----------------------------------------------------------------------------------------------------------   
   static datetime timestamp = 0;
   datetime onetime = iTime(_Symbol,PERIOD_CURRENT,0);
   if (timestamp != onetime){
      timestamp = onetime;
      
//--- request trade history
   HistorySelect(0,TimeCurrent());
   uint total_deals=HistoryDealsTotal();
   ulong ticket_history_deal=0;
   int counter=0;
   string text="";
//--- for all deals
   for(uint i=total_deals-1; i>=0; i--)
     {
      //--- try to get deals ticket_history_deal
      if((ticket_history_deal=HistoryDealGetTicket(i))>0)
        {
         long     deal_ticket       =HistoryDealGetInteger(ticket_history_deal,DEAL_TICKET);
         long     deal_time         =HistoryDealGetInteger(ticket_history_deal,DEAL_TIME);
         long     deal_type         =HistoryDealGetInteger(ticket_history_deal,DEAL_TYPE);
         long     deal_entry        =HistoryDealGetInteger(ticket_history_deal,DEAL_ENTRY);
         long     deal_magic        =HistoryDealGetInteger(ticket_history_deal,DEAL_MAGIC);
         double   deal_commission   =HistoryDealGetDouble(ticket_history_deal,DEAL_COMMISSION);
         double   deal_swap         =HistoryDealGetDouble(ticket_history_deal,DEAL_SWAP);
         double   deal_profit       =HistoryDealGetDouble(ticket_history_deal,DEAL_PROFIT);
         string   deal_symbol       =HistoryDealGetString(ticket_history_deal,DEAL_SYMBOL);
         datetime m_from_date       = 0;
         uchar lastdeal = 1;
         //---                
         
         if((_Symbol==deal_symbol || _Symbol=="") && (Magic==deal_magic || Magic<0)){
         
            if(deal_entry==DEAL_ENTRY_OUT){
               
               counter++;
               string time=TimeToString((datetime)deal_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS);
               SendMail(__FILE__, Symbol()+",OnTick $ "+DoubleToString(deal_commission+deal_swap+deal_profit,2));
               if(counter==lastdeal){                  
               
                  m_from_date=(datetime)deal_time;
                  break;
                 }
              }
           }
        }
     }
   }               
   Comment(text); 
   }
//----------------------------------------------------------------------------------------------------------
//Send email alerts of last closed deals symbol & loss or profit amount in USD
//-----------------------------------------------------------------------------------------------------------   
 

You can save your last deal time to a static datetime variable. Then you can check if your last order time should be greater than that time.

// In Global Scope
static datetime lastdealtime = 0;

if(deal_entry==DEAL_ENTRY_OUT && deal_time>lastdealtime){
               
               counter++;
               string time=TimeToString((datetime)deal_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS);
               SendMail(__FILE__, Symbol()+",OnTick $ "+DoubleToString(deal_commission+deal_swap+deal_profit,2));
               lastdealtime = deal_time;        
               if(counter==lastdeal){                  
               
                  m_from_date=(datetime)deal_time;
                  break;
                 }

Also you can do it in another way. Any Trade event happen it trigger OnTrade() function.

https://www.mql5.com/en/docs/event_handlers/ontrade

Documentation on MQL5: Event Handling / OnTrade
Documentation on MQL5: Event Handling / OnTrade
  • www.mql5.com
OnTrade - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Mahadi Hasan Razu #:

You can save your last deal time to a static datetime variable. Then you can check if your last order time should be greater than that time.

Also you can do it in another way. Any Trade event happen it trigger OnTrade() function.

https://www.mql5.com/en/docs/event_handlers/ontrade

Thank you for the response, i will give this a try.

Also, how would i go about doing it using the OnTradeTransaction() function and would that be an easier way?

 
Mahadi Hasan Razu #:

You can save your last deal time to a static datetime variable. Then you can check if your last order time should be greater than that time.

Also you can do it in another way. Any Trade event happen it trigger OnTrade() function.

https://www.mql5.com/en/docs/event_handlers/ontrade

I tried this and when compiled i have a warning that says "possible loss of data due to type conversion from 'long' to 'datetime'" and the warning is from: lastdealtime = deal_time;

would it be okay to run the EA with that warning?

 
 
Mahadi Hasan Razu #:

You can save your last deal time to a static datetime variable. Then you can check if your last order time should be greater than that time.

Also you can do it in another way. Any Trade event happen it trigger OnTrade() function.

https://www.mql5.com/en/docs/event_handlers/ontrade

Here's the screenshot of where its showing the warning message when compiled
Files:
 
Mahadi Hasan Razu #:

You can save your last deal time to a static datetime variable. Then you can check if your last order time should be greater than that time.

Also you can do it in another way. Any Trade event happen it trigger OnTrade() function.

https://www.mql5.com/en/docs/event_handlers/ontrade

Mahadi it seems to be working good now but the warning is a bother. What would be the fix to get rid of the warning? Thank you in advance
 

Two lines later you force the cast of ling and datetime just do it again:

lastdealtime = (datetime) deal_time;        
               if(counter==lastdeal){                  
               
                  m_from_date=(datetime)deal_time;
                  break;
                 }
 
Carl Schreiber #:

Two lines later you force the cast of ling and datetime just do it again:

it was right in front of my face this whole time duh i just needed to add (datetime) 🤦🏻‍♂️ lol thank you Carl and Mahadi for helping me figure this out 
 
Aesen #:
it was right in front of my face this whole time duh i just needed to add (datetime) 🤦🏻‍♂️ lol thank you Carl and Mahadi for helping me figure this out 

You're welcome. Here I just tried to give you hint. Just needed the typecast to avoid warning.

 
Mahadi Hasan Razu #:

You're welcome. Here I just tried to give you hint. Just needed the typecast to avoid warning.

👍🏼
Reason: