MQL5 sendmail. EA only sends me an email one time and then it stops placing new trades, help.

 

Why does my EA send me an email of the last deal's profit only once upon initializing the EA onto a chart and then after that it completely stops placing new trades?

The way i'm trying to make this code work is, whenever a position closes, i want my EA to send me an email notification of the most recent deal closed result. So that i don't have to keep checking my MT5 account to see my profits/loss

i have been struggling to figure this out for weeks can somebody anybody help me please

This is the code that i am trying to edit:

//global scope
//--- input parameters
input uchar    InpN        = 1;  // N
input datetime InpFromDate = 0;  // From Date (D'1970.01.01 00:00' -> OFF parameter)
input string   InpSymbol   = ""; // Symbol ("" -> all symbols)
input long     InpMagic    = -1; // Magic number (<0 -> all magics)
//---
datetime m_from_date       = 0;  // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+


int OnInit(){

//--- request trade history
   HistorySelect(m_from_date,TimeTradeServer()+60*60*24*3);
   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);
         //---
         if((InpSymbol==deal_symbol || InpSymbol=="") && (InpMagic==deal_magic || InpMagic<0)){
         
            if(deal_entry==DEAL_ENTRY_OUT && deal_time>lastdealtime){
            
               counter++;
               text=text+Symbol()+", $ "+DoubleToString(deal_commission+deal_swap+deal_profit,2);
               lastdealtime = (datetime)deal_time;  
               if(counter==InpN){
               
                  m_from_date=(datetime)deal_time;
                  break;
                 }
              }
           }
        }
     }
   SendMail(__FILE__, text); 
 //---

   return(INIT_SUCCEEDED);
   
} 

void OnTick(){  

//--- request trade history
   HistorySelect(m_from_date,TimeTradeServer()+60*60*24*3);
   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);
         //---
         if((InpSymbol==deal_symbol || InpSymbol=="") && (InpMagic==deal_magic || InpMagic<0)){
         
            if(deal_entry==DEAL_ENTRY_OUT && deal_time>lastdealtime){
            
               counter++;
               text=text+Symbol()+", $ "+DoubleToString(deal_commission+deal_swap+deal_profit,2);
               lastdealtime = (datetime)deal_time;  
               if(counter==InpN){
               
                  m_from_date=(datetime)deal_time;
                  break;
                 }
              }
           }
        }
     }
   SendMail(__FILE__, text);              
}
Reason: