title: My EA sends multiple duplicate emails
body: <p>I have an EA on my MetaTrader4 Platform which is designed to send an email via the <code>SendMail</code> function whenever a trade is executed, and another when that trade is closed out. </p>
  
  <p>This all works without a hitch, however, it sends multiple duplicate emails whenever a trade position is closed. It obviously needs to only send one Email.</p>
  
  <p>This is also the case with the <code>SendNotification</code> function as well</p>
  
  <p>My setup is as follows:</p>
  
  <ul>
  <li>Windows 2012 R2 VPS Server </li>
  <li>MetaQuotes MT4</li>
  </ul>
  
  <p><strong>Other Useful Information</strong>   </p>
  
  <ul>
  <li><p>I did set up an auto forwarding on my Email client to send all Trade Notifications to another address. So these emails are in two separate inboxes.   </p></li>
  <li><p>I only have one MT4 Terminal.</p></li>
  <li><p>I have one EA per symbol</p></li>
  </ul>
  
  <p>My question is, has anyone else had this issue and/or does anyone know the reason for this &amp; how would I fix this issue?</p>
  
  <p>Thanks</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>//+------------------------------------------------------------------+
  //| forexmts, forexmts@mail.ru, v4.0vs-                              |
  //| Events function,                                                 |
  //| detects changes in orders list                                   |
  //+------------------------------------------------------------------+
  void fEvents(ENUM_FUNCTION_MODE enMode)
     {   
     if (enMode == MODE_INIT) { fOrders(); return; }
     
     if (enMode == MODE_WORK)
     {
     bool bFoundFlag;
     fOrders();
     //existing orders check
     for(int i = 0; i &lt; ArraySize(stcOrdOldArr); i++)  //search all orders from the old array
        {
        bFoundFlag = false;
        for(int j = 0; j &lt; ArraySize(stcOrdNewArr); j++) //.. in the new array
           {         
           if (stcOrdOldArr[i].ticket == stcOrdNewArr[j].ticket) //found by ticket 
              {
              if (stcOrdOldArr[i].type != stcOrdNewArr[j].type) { }//but the type has changed
              if (stcOrdOldArr[i].sl != stcOrdNewArr[j].sl) { } //SL was changed
              if (stcOrdOldArr[i].tp != stcOrdNewArr[j].tp) { } //TP was changed
                 
              bFoundFlag = true;
              break;                              //stop searching in the new array
              }
           }
  
        if (bFoundFlag == false)                 //order wasn't found
           {
           if (stcOrdOldArr[i].type &lt;= 1)
              //we can't use profit and close price, lots and commentary (for partial close) from the dOrdOldArr, because we need their after close values
              if (OrderSelect(stcOrdOldArr[i].ticket, SELECT_BY_TICKET))
                 {
                 string sKindOfExit = "";
                 if ((OrderType() == OP_BUY &amp;&amp; OrderClosePrice() &lt;= stcOrdOldArr[i].sl) || (OrderType() == OP_SELL &amp;&amp; OrderClosePrice() &gt;= stcOrdOldArr[i].sl &amp;&amp; stcOrdOldArr[i].sl &gt; 0) || StringFind(OrderComment(), "[sl]") &gt;= 0)
                    sKindOfExit = " at StopLoss"; //close by sl
                 else
                 if ((OrderType() == OP_BUY &amp;&amp; OrderClosePrice() &gt;= stcOrdOldArr[i].tp &amp;&amp; stcOrdOldArr[i].tp &gt; 0) || (OrderType() == OP_SELL &amp;&amp; OrderClosePrice() &lt;= stcOrdOldArr[i].tp) || StringFind(OrderComment(), "[tp]") &gt;= 0)
                    sKindOfExit = " at TakeProfit"; //close be tp
                 else
                    { }         //ordinary close
                 
                 SendMail("(TNEPC)Trade Notification Email of Position Closure","Order #"+IntegerToString(OrderTicket())+" has been closed "+sKindOfExit+" on the account "+AccountName()+" "+AccountServer()+
                 "\n"+
                 "\nThe order exit price for this trade is "+DoubleToStr(OrderClosePrice(),_Digits)+" with a "+(OrderProfit()&gt;=0?"profit":"loss")+" of £"+DoubleToStr(OrderProfit(),2)+
                 "\n"+
                 "\nThis is an Automated Message from xxxxxx"+
                 "\n-----------------------------------------------------------------------"+
                 "\n"+
                 "\nPRIVACY NOTICE"+
                 "\nThis email is intended for the recipient stated at the address at the top of this email. If you have received this in error, please delete this email immediately and contact us at xxxxxxx"+
                 "\n"+
                 "\n"+
                 "\nH"+
                 "\n"+
                 "\n"+
                 "\n");
                 SendNotification("Ticket #"+IntegerToString(OrderTicket())+" has closed with a "+(OrderProfit()&gt;=0?"profit":"loss")+" of "+DoubleToStr(OrderProfit(),2));
                 }
              else
                 { }   //order not found
  
           if (stcOrdOldArr[i].type &gt; 1) { }      //pending order is deleted
           
           }
        }
     
    
        
     }
     
    }</code></pre>
  </div>
  </div>
  </p>