Email Alert within EA:

 
I am using an EA I struggled to make. It works as I intended it to. Now I want to add an email alert function. The code is trading, however it is not sending me an email when a trade is opened. WhenI run the compiler, I see no errors. I have set up the email server and I am able to send a test email from MT4.I run this code and it will open trades, but why will it not send emails? ANy help would be very welcomed.
void OnTick()
{
double v1 = iMACD(NULL,0,12,26,9,PRICE_CLOSE, MODE_MAIN,1),
       v2 = iMACD(NULL,0,12,26,9,PRICE_CLOSE, MODE_MAIN,2),
       s1 = iMACD(NULL,0,12,26,9,PRICE_CLOSE, MODE_SIGNAL,1),
       s2 = iMACD(NULL,0,12,26,9,PRICE_CLOSE, MODE_SIGNAL,2);
bool wasUp = v2 > s2,
     isUp = v1 > s1,
     crossed = isUp != wasUp,
     isUpCross = isUp && !wasUp;
      
int total=OrdersTotal();
if(isUpCross && total<5)
OrderSend (
                      _Symbol,                             //Pair to use
                      OP_BUY,                      //buy or sell operator
                      0.15,                             // how much
                      Ask,                              // price to pay 
                      3,                        // slippage ??
                      Bid-100*_Point,           // Stop Loss
                      Ask+125*_Point,           // Take Prof
                      NULL,                           // use if you want to ID the trade
                      0,                                      // Magic Number
                      0,                                      // Expiration, 0 for no expiration
                      Green
                    );  

}
void SendTradeEmail(int ticket)
{
   if(ticket>0)
   {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
      {
         string MsgSubject = OrderSymbol() + " ";
         string MsgBody = "Pair Traded: "+OrderSymbol();
         MsgBody = MsgBody + "nBuy Or Sell: ";
         if (OrderType() == OP_BUY)
         {
            MsgSubject = MsgSubject + "BUY ";
            MsgBody = MsgBody + "BUY";
         }
         if (OrderType() == OP_SELL)
         {
            MsgSubject = MsgSubject + "SELL ";
            MsgBody = MsgBody + "SELL";
         }
         MsgBody = MsgBody + "nTime: "+TimeToStr(OrderOpenTime(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
         MsgSubject = MsgSubject + DoubleToStr(OrderOpenPrice(),Digits);
         MsgBody = MsgBody + "nOpen Price: "+DoubleToStr(OrderOpenPrice(),Digits);
         MsgBody = MsgBody + "nLot Size: "+DoubleToStr(OrderLots(),3);
      
         SendMail(MsgSubject,MsgBody);
      }
   }
}
 
Where is called your function SendTradeEmail() ?
 
Last line...I think I am calling wrong function
 
wcrowder:
Last line...I think I am calling wrong function
You didn't call it, you defined it.
 
I am sure you are right, but I am not a coding expert at all...what do you mean? 

If all those things happen, sendmail - right? 
 

Yes you also have to call it when it is needed.

void OnTick()
{
double v1 = iMACD(NULL,0,12,26,9,PRICE_CLOSE, MODE_MAIN,1),
       v2 = iMACD(NULL,0,12,26,9,PRICE_CLOSE, MODE_MAIN,2),
       s1 = iMACD(NULL,0,12,26,9,PRICE_CLOSE, MODE_SIGNAL,1),
       s2 = iMACD(NULL,0,12,26,9,PRICE_CLOSE, MODE_SIGNAL,2);
bool wasUp = v2 > s2,
     isUp = v1 > s1,
     crossed = isUp != wasUp,
     isUpCross = isUp && !wasUp;
      
int total=OrdersTotal();
if(isUpCross && total<5){
                        
int ticket = OrderSend (
                      _Symbol,                             //Pair to use
                      OP_BUY,                      //buy or sell operator
                      0.15,                             // how much
                      Ask,                              // price to pay 
                      3,                        // slippage ??
                      Bid-100*_Point,           // Stop Loss
                      Ask+125*_Point,           // Take Prof
                      NULL,                           // use if you want to ID the trade
                      0,                                      // Magic Number
                      0,                                      // Expiration, 0 for no expiration
                      Green
                    );
  SendTradeEmail(ticket);         
 }  
}
void SendTradeEmail(int ticket)
{
   if(ticket>0)
   {
      if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
      {
         string MsgSubject = OrderSymbol() + " ";
         string MsgBody = "Pair Traded: "+OrderSymbol();
         MsgBody = MsgBody + "nBuy Or Sell: ";
         if (OrderType() == OP_BUY)
         {
            MsgSubject = MsgSubject + "BUY ";
            MsgBody = MsgBody + "BUY";
         }
         if (OrderType() == OP_SELL)
         {
            MsgSubject = MsgSubject + "SELL ";
            MsgBody = MsgBody + "SELL";
         }
         MsgBody = MsgBody + "nTime: "+TimeToStr(OrderOpenTime(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
         MsgSubject = MsgSubject + DoubleToStr(OrderOpenPrice(),Digits);
         MsgBody = MsgBody + "nOpen Price: "+DoubleToStr(OrderOpenPrice(),Digits);
         MsgBody = MsgBody + "nLot Size: "+DoubleToStr(OrderLots(),3);
      
         SendMail(MsgSubject,MsgBody);
      }
   }
}
 
How do I call it, when a trade opens???
 
Read the code.