EA auto tp and sl are put after the email notification are sent (help plz)

 

Hi there,

i have a problem with 2 EA, the auto tp and sl EA are put after the email notification are send, Sometimes its work fine like this :

SELL USDCHF 
Price 0.99318 
StopLoss  0.99618 
TakeProfit 0.99018 

And Sometimes no like this :

SELL USDJPY 
Price 110.69600 
StopLoss  0.00000 
TakeProfit 0.00000 


can i set the EA email notification send after the EA auto put the TP and SL ?

this is the Email notifications EA  (help plz)


#property indicator_chart_window

datetime dt;

int temp;

int oldtemp;


extern string CommentProg0 = "===============";


extern string CommentProgName = "Email Alert Indicator";

extern string CommentProgVersion = "0.4.2";

extern string CommentProgAuthor0 = "Dmitry Reznikov";

extern string CommentProgAuthor1 = "FemtoTrader";


extern string CommentProg1 = "===============";


extern string CommentTitle = "=== Notifications ===";

extern string Title = "Metaforexsignals";

extern bool EmailNotification = TRUE;

extern bool PushNotification = TRUE;

extern bool AlertNotification = FALSE;

//extern bool SoundNotification = TRUE;

//extern string SoundFilename = "alert.wav";


extern string CommentSymbols = "=== Symbols ===";

extern bool NotifyOnlyOnChartSymbol = FALSE;

//extern string NotifySymbolsList = "EURUSD,USDJPY"; // ToDo (split, ...)


extern string CommentOpen = "=== Open ===";

extern bool NotifyOpenBuy = TRUE;

extern bool NotifyOpenSell = TRUE;


extern string CommentClose = "=== Close ===";

extern bool NotifyCloseBuy = TRUE;

extern bool NotifyCloseSell = TRUE;


extern string CommentOpenPending= "=== Open Pending ===";

extern bool NotifyOpenBuyLimit = TRUE;

extern bool NotifyOpenSellLimit = TRUE;

extern bool NotifyOpenBuyStop = TRUE;

extern bool NotifyOpenSellStop = TRUE;


extern string CommentClosePending= "=== Close Pending ===";

extern bool NotifyCloseBuyLimit = TRUE;

extern bool NotifyCloseSellLimit = TRUE;

extern bool NotifyCloseBuyStop = TRUE;

extern bool NotifyCloseSellStop = TRUE;


extern string CommentDeletePending= "=== Delete Pending ===";

extern bool NotifyDeleteBuyLimit = TRUE;

extern bool NotifyDeleteSellLimit = TRUE;

extern bool NotifyDeleteBuyStop = TRUE;

extern bool NotifyDeleteSellStop = TRUE;


/* OpenPending ClosePending DeletePending */

/* ModifyTP ModifySL */


string strTitle;


void SendNotifications(string subject, string message)

{

if (EmailNotification) SendMail(subject, message);

if (PushNotification) SendNotification(subject + "\n" + message); // need MetaQuotes ID - support only MT4>=v4.00 build 427

if (AlertNotification) Alert(message);

//if (SoundNotification) PlaySound(SoundFilename);

}


//+------------------------------------------------------------------+

//| Custom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

//---- indicators

dt = Time[2];

//oldtemp = 0;

oldtemp = OrdersTotal(); // modif by if loaded with opened trades

}


   

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----


//----

return(0);

}


   

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

int start()

{

bool ret = True;


if (NotifyOnlyOnChartSymbol) {

temp = 0;

for (int i=0;i<OrdersTotal();i++)

{

if (OrderSymbol() == Symbol()) {

ret = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

temp++;

}

}


if (temp<oldtemp || temp==0) {

ret = OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY); // last history order

if (OrderSymbol() != Symbol()) {

ret = false;

}

} else {


}


} else { /* === FemtoTrader : notif for any symbols */

temp = OrdersTotal();


if (temp<oldtemp || temp==0) {

ret = OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY); // last history order

} else {

ret = OrderSelect(temp-1, SELECT_BY_POS, MODE_TRADES); // last trade order

}

}


//if



string msg;

msg = CommentProgName + " " + "v"+CommentProgVersion+"\n"+CommentProgAuthor0+" modif by "+CommentProgAuthor1+"\n"+oldtemp+"/"+temp;

if (oldtemp != temp) {

msg = msg + "\n" + "Send Email for Ticket#: "+OrderTicket();

msg = msg + "\n" + "OrderType: " + OrderTypeToStr(OrderType());

msg = msg + "\n" + "OrderCloseTime: " + OrderCloseTime();

//msg = msg + "\n" + "Test: " + (NotifyPendingBuyLimit && OrderType()==OP_BUYLIMIT);

//Alert(msg); // debug only

}

Comment(msg);


if (oldtemp != temp && ret) { // to be fixed (oldtemp!=temp) oldtemp<temp && ret

strTitle = Title + "[" + AccountNumber() + "]" + " ";

//SendNotifications("New event","New event");


string ordertyp;

double x=OrderOpenPrice(),y=OrderClosePrice(),pips;

pips = y-x; //MathAbs(y - x);

//pips = pips * 10000; // ToFix for xxxJPY

double point = PipPoint(OrderSymbol());

pips = pips/point;


if(NotifyOpenBuy && ( OrderType()==OP_BUY ) && ( OrderCloseTime()==0 ) ) {

ordertyp="BUY";

msg = msg + "\n" + ordertyp;

SendNotifications("Forex Alert "+ordertyp+" "+OrderSymbol()+"",

""+ordertyp+" "+OrderSymbol()+" \n"+

"Price  "+DoubleToStr(OrderOpenPrice(),5)+" \n"+

"StopLoss  "+DoubleToStr(OrderStopLoss(),5)+" \n"+

"TakeProfit  "+DoubleToStr(OrderTakeProfit(),5)+" \n"+

"///////////////////////"+" \n"+


"Very Important : sometimes the SL and the TP is not calculated by our system"+" \n"+

"Please set your SL and TP at -/+ 30 pips"+" \n"+


//"===================="+" \n"+

// "CloseTime: "+TimeToStr(OrderCloseTime())+" \n"+

// "ClosePrice: "+DoubleToStr(OrderClosePrice(),5)+" \n"+

// "Profit: "+DoubleToStr(OrderProfit(),2)+" \n"+

// "Pips: "+DoubleToStr(pips,1)+" \n\n"+

"Good Luck !"+" \n"+

"///////////////////////"+" \n"+



"mt4 notification" );

}


if(NotifyOpenSell && ( OrderType()==OP_SELL ) && ( OrderCloseTime()==0 ) ) {

ordertyp="SELL";

msg = msg + "\n" + ordertyp;

SendNotifications("Forex Alert "+ordertyp+" "+OrderSymbol()+"",

""+ordertyp+" "+OrderSymbol()+" \n"+

"Price "+DoubleToStr(OrderOpenPrice(),5)+" \n"+

"StopLoss  "+DoubleToStr(OrderStopLoss(),5)+" \n"+

"TakeProfit "+DoubleToStr(OrderTakeProfit(),5)+" \n"+

"///////////////////////"+" \n"+

"Very Important : sometimes the SL and the TP is not calculated by our system"+" \n"+

"Please set your SL and TP at -/+ 30 pips"+" \n"+

//"===================="+" \n"+

// "CloseTime: "+TimeToStr(OrderCloseTime())+" \n"+

// "ClosePrice: "+DoubleToStr(OrderClosePrice(),5)+" \n"+

// "Profit: "+DoubleToStr(OrderProfit(),2)+" \n"+

// "Pips: "+DoubleToStr(pips,1)+" \n\n"+

"Good Luck !"+" \n"+

"///////////////////////"+" \n"+


"mt4 notification" );

}


if(NotifyCloseBuy && ( OrderType()==OP_BUY ) && ( OrderCloseTime()!=0 ) ) {

ordertyp="CLOSE BUY";

msg = msg + "\n" + ordertyp;

SendNotifications(" "+ordertyp+" "+OrderSymbol()+" ($"+DoubleToStr(OrderProfit(),2)+") Pips "+DoubleToStr(pips,1)+" ",

""+ordertyp+" "+OrderSymbol()+"  \n"+

"Open Time  "+TimeToStr(OrderOpenTime())+" \n"+

"Profit  $"+DoubleToStr(OrderProfit(),2)+" \n"+


"///////////////////////"+" \n"+


"Open Price  "+DoubleToStr(OrderOpenPrice(),5)+" \n"+

"Close Price  "+DoubleToStr(OrderClosePrice(),5)+" \n"+

"StopLoss  "+DoubleToStr(OrderStopLoss(),5)+" \n"+

"TakeProfit  "+DoubleToStr(OrderTakeProfit(),5)+" \n"+

"///////////////////////"+" \n"+

"mt4 notification" );

}


if(NotifyCloseSell && ( OrderType()==OP_SELL ) && ( OrderCloseTime()!=0 ) ) {

ordertyp="CLOSE SELL";

msg = msg + "\n" + ordertyp;


SendNotifications(" "+ordertyp+" "+OrderSymbol()+" ($"+DoubleToStr(OrderProfit(),2)+") Pips "+DoubleToStr(pips,1)+" ",

""+ordertyp+" "+OrderSymbol()+"  \n"+

"OpenTime: "+TimeToStr(OrderOpenTime())+" \n"+

"Profit  $"+DoubleToStr(OrderProfit(),2)+" \n"+


"///////////////////////"+" \n"+


"Open Price  "+DoubleToStr(OrderOpenPrice(),5)+" \n"+

"Close Price  "+DoubleToStr(OrderClosePrice(),5)+" \n"+

"StopLoss  "+DoubleToStr(OrderStopLoss(),5)+" \n"+

"TakeProfit  "+DoubleToStr(OrderTakeProfit(),5)+" \n"+

"///////////////////////"+" \n"+

"MT4 notification" );


}


/*

bool b_pending = ( ( NotifyOpenBuyLimit || NotifyCloseBuyLimit || NotifyDeleteBuyLimit ) && ( OrderType()==OP_BUYLIMIT ) )

|| ( ( NotifyOpenBuyStop || NotifyCloseBuyStop || NotifyDeleteBuyStop ) && ( OrderType()==OP_BUYSTOP ) )

|| ( ( NotifyOpenSellLimit || NotifyCloseSellLimit || NotifyDeleteSellLimit ) && ( OrderType()==OP_SELLLIMIT ) )

|| ( ( NotifyOpenSellStop || NotifyCloseSellStop || NotifyDeleteSellStop ) && ( OrderType()==OP_SELLSTOP ) ) ;

*/


bool b_pending = ( OrderType()==OP_BUYLIMIT ) || ( OrderType()==OP_BUYSTOP ) || ( OrderType()==OP_SELLLIMIT ) || ( OrderType()==OP_SELLSTOP );

bool b_notify = false;


if ( b_pending ) {


if(OrderType()==OP_BUYLIMIT) {

ordertyp = "BUYLIMIT";

b_notify = NotifyOpenBuyLimit || NotifyCloseBuyLimit || NotifyDeleteBuyLimit;

}

if(OrderType()==OP_BUYSTOP) {

ordertyp = "BUYSTOP";

b_notify = NotifyOpenBuyStop || NotifyCloseBuyStop || NotifyDeleteBuyStop;

}

if(OrderType()==OP_SELLLIMIT) {

ordertyp = "SELLLIMIT";

b_notify = NotifyOpenSellLimit || NotifyCloseSellLimit || NotifyDeleteSellLimit;

}

if(OrderType()==OP_SELLSTOP) {

ordertyp = "SELLSTOP";

b_notify = NotifyOpenSellStop || NotifyCloseSellStop || NotifyDeleteSellStop;

}


if( OrderCloseTime()==0 ) {

ordertyp = "OPEN " + ordertyp;

b_notify = ( b_notify && (NotifyOpenBuyLimit || NotifyOpenBuyStop || NotifyOpenSellLimit || NotifyOpenSellStop) );

} else {

if (OrderComment()=="cancelled") { // ToFix : autre solution ?

ordertyp = "DELETE " + ordertyp;

b_notify = ( b_notify && (NotifyDeleteBuyLimit || NotifyDeleteBuyStop || NotifyDeleteSellLimit || NotifyDeleteSellStop) );

} else {

ordertyp = "CLOSE " + ordertyp;

b_notify = ( b_notify && (NotifyCloseBuyLimit || NotifyCloseBuyStop || NotifyCloseSellLimit || NotifyCloseSellStop) );

}

}

msg = msg + "\n" + ordertyp;


if (b_notify) {

SendNotifications(strTitle+ordertyp+" "+DoubleToStr(OrderLots(),2)+" "+OrderSymbol()+"@"+DoubleToStr(OrderOpenPrice(),5)+", bal: "+DoubleToStr(AccountBalance(),2)+", eq: "+DoubleToStr(AccountEquity(),2)+", "+OrderComment()+"",

"Symbol: "+OrderSymbol()+" "+ordertyp+" \n"+

"MagicNumber: "+OrderMagicNumber()+" \n"+

"Comment: "+OrderComment()+" \n"+

"Ticket#: "+OrderTicket()+" \n"+

"Size: "+DoubleToStr(OrderLots(),2)+" \n"+

"===================="+" \n"+

"OpenTime: "+TimeToStr(OrderOpenTime())+" \n"+

"OpenPrice: "+DoubleToStr(OrderOpenPrice(),5)+" \n"+

"OrderStopLoss: "+DoubleToStr(OrderStopLoss(),5)+" \n"+

"OrderTakeProfit: "+DoubleToStr(OrderTakeProfit(),5)+" \n"+

"===================="+" \n"+

"CloseTime: "+TimeToStr(OrderCloseTime())+" \n"+

"ClosePrice: "+DoubleToStr(OrderClosePrice(),5)+" \n"+ // ?

"Profit: "+DoubleToStr(OrderProfit(),2)+" \n"+ // ?

"Pips: "+DoubleToStr(pips,1)+" \n\n"+ // ?

"===================="+" \n"+

"Balance: "+DoubleToStr(AccountBalance(),2)+" \n"+

"Used Margin: "+DoubleToStr(AccountMargin(),2)+" \n"+

"Free Margin: "+DoubleToStr(AccountFreeMargin(),2)+" \n"+

"Equity: "+DoubleToStr(AccountEquity(),2)+" \n"+

"Open Orders: "+DoubleToStr(OrdersTotal(),0)+" \n\n"+

"Broker: "+AccountCompany()+" \n"+

"Leverage: "+AccountLeverage()+"" );

}

}


}

oldtemp = temp;


Comment(msg);


//----

return(0);

}

//+------------------------------------------------------------------+



string OrderTypeToStr (int ot) {

if (ot==OP_BUY) {

return("BUY");

} else if (ot==OP_SELL) {

return("SELL");

} else if (ot==OP_BUYLIMIT) {

return("BUYLIMIT");

} else if (ot==OP_BUYSTOP) {

return("BUYSTOP");

} else if (ot==OP_SELLLIMIT) {

return("SELLLIMIT");

} else if (ot==OP_SELLSTOP) {

return("SELLSTOP");

} else {

return("UNDEF");

}

}


double PipPoint(string symbol) {

int CalcDigits = MarketInfo(symbol, MODE_DIGITS);

if(CalcDigits == 2 || CalcDigits == 3) {

double CalcPoint = 0.01;

} else if(CalcDigits == 4 || CalcDigits == 5) {

CalcPoint = 0.0001;

}

return(CalcPoint);

}


--------------------------------------------------------------------------------------------------------------

THIS IS THE EA AUTO TP SL :


//+------------------------------------------------------------------+

//| Auto SL-TP                                             |

//+------------------------------------------------------------------+


/*

   This utility EA places Stoploss & TakeProfit if there are positions without them.

*/


extern string  About = "<<< Automatic SL and TP manager >>>";


extern bool    Set_StopLoss = true;

extern bool    Set_TakeProfit = true;


// StopLoss

extern string  SLExp = "<<< StopLoss >>>";

extern string  StopLossExp = "1=Fixed Pips  2=ATR Multiple";

extern int     StopLoss_Method = 1 ;

extern int     Fixed_SL = 30;

extern double  StopLoss_ATR = 0.7 ;


// TakeProfit

extern string  TPExp = "<<< TakeProfit >>>";

extern string  TakeProfitExp = "1=Fixed Pips  2=ATR Multiple";

extern int     TakeProfit_Method = 1 ;

extern int     Fixed_TP = 30;

extern double  TakeProfit_ATR = 1.8 ;


// ATR Setting

extern string  ATRExp = "<<< ATR Setting >>>";

extern string  TF_note = "0=Chart 1=M1 2=M5 3=M15 4=M30 5=1H 6=4H 7=D1 8=W1 9=MN";

extern int     ATR_TimeFrame = 7;

extern int     ATR_Period = 30;


// Misc

extern string  AboutMisc = "<<< Miscellaneous >>>";

extern bool    Alert_On = false ;


string NAME = "Auto SL-TP Setter v1";


static int     ATR_TF;


//+------------------------------------------------------------------+

//| init                                                            

//+------------------------------------------------------------------+

int init()

{

   ATR_TF = TF_Selector(ATR_TimeFrame);


   return(0);

}


//+------------------------------------------------------------------+

//| TF Selector                                                      |

//+------------------------------------------------------------------+

int TF_Selector(int TIMEFRAME)

{

   int TF;


   // Indicator Time Frame

   switch(TIMEFRAME) // 0=Chart 1=M1 2=M5 3=M15 4=M30 5=1H 6=4H 7=D1 8=W1 9=MN

   {

      case 0 : TF = 0; break; 

      case 1 : TF = 1; break; 

      case 2 : TF = 5; break; 

      case 3 : TF = 15; break; 

      case 4 : TF = 30; break; 

      case 5 : TF = 60; break; 

      case 6 : TF = 240; break; 

      case 7 : TF = 1440; break; 

      case 8 : TF = 10080; break; 

      case 9 : TF = 43200; break; 

      default: TF = 0; break; 

   }


   return(TF);

}


//+------------------------------------------------------------------+

//| Start function                                                   |

//+------------------------------------------------------------------+

int start()

{

   


   if(Set_StopLoss) SetStopLoss();

   if(Set_TakeProfit) SetTakeProfit();

   

   return(0);

}


//+------------------------------------------------------------------+

//| SL_Decision                                                      |

//+------------------------------------------------------------------+

int SL_Decision(string SYMBOL)

{

   if(StopLoss_Method>2 || StopLoss_Method<1) StopLoss_Method = 1; // error correction

   

   double SL = Fixed_SL * DeciQuoteAdjuster(SYMBOL) ;

   if(StopLoss_Method == 2) SL = (ATR(SYMBOL) * StopLoss_ATR) / MarketInfo(SYMBOL,MODE_POINT) ;

   

   int Stoploss = SL;


   return(Stoploss);

}


//+------------------------------------------------------------------+

//| TP_Decision                                                      |

//+------------------------------------------------------------------+

int TP_Decision(string SYMBOL)

{

   if(TakeProfit_Method>2 || TakeProfit_Method<1) TakeProfit_Method = 2; // error correction

   

   double TP = Fixed_TP * DeciQuoteAdjuster(SYMBOL) ;

   if(TakeProfit_Method == 2) TP = (ATR(SYMBOL) * TakeProfit_ATR) / MarketInfo(SYMBOL,MODE_POINT) ;

   

   int TakeProfit = TP;


   return(TakeProfit);

}


//+------------------------------------------------------------------+

//| ATR                                                              |

//+------------------------------------------------------------------+

double ATR(string SYMBOL)

{

   double ATR = iATR(SYMBOL,ATR_TF,ATR_Period,1);

   return(ATR);

}


//+------------------------------------------------------------------+

//| SetStopLoss                                                      |

//+------------------------------------------------------------------+

void SetStopLoss()

{

   int i,Type; bool selected, success;


   for(i=0; i<OrdersTotal(); i++)

   {

      selected = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

      if(selected && (OrderType() == OP_BUY || OrderType() == OP_SELL) && OrderStopLoss() < 1 * MarketInfo(OrderSymbol(),MODE_POINT))

      {

         Type = OrderType();

         success = false;

         

         if(Type == OP_BUY) success = OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice() - SL_Decision(OrderSymbol()) * MarketInfo(OrderSymbol(),MODE_POINT), OrderTakeProfit(), 0, CLR_NONE);

         if(Type == OP_SELL) success = OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice() + SL_Decision(OrderSymbol()) * MarketInfo(OrderSymbol(),MODE_POINT), OrderTakeProfit(), 0, CLR_NONE);


         if(success && Alert_On) Alert("Stoploss for "+OrderSymbol()+" has been placed automatically.") ;

         if (!success) Print("Error code = " + GetLastError());

      }

   }

} // end of SetStopLoss


//+------------------------------------------------------------------+

//| SetStopLoss                                                      |

//+------------------------------------------------------------------+

void SetTakeProfit()

{

   int i,Type; bool selected, success;


   for(i=0; i<OrdersTotal(); i++)

   {

      selected = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

      if(selected && (OrderType() == OP_BUY || OrderType() == OP_SELL) && OrderTakeProfit() < 1 * MarketInfo(OrderSymbol(),MODE_POINT))

      {

         Type = OrderType();

         success = false;

         

         if(Type == OP_BUY) success = OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderOpenPrice() + TP_Decision(OrderSymbol()) * MarketInfo(OrderSymbol(),MODE_POINT), 0, CLR_NONE);

         if(Type == OP_SELL) success = OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderOpenPrice() - TP_Decision(OrderSymbol()) * MarketInfo(OrderSymbol(),MODE_POINT), 0, CLR_NONE);


         if(success && Alert_On) Alert("TakeProfit for "+OrderSymbol()+" has been placed automatically.") ;

         if (!success) Print("Error code = " + GetLastError());

      }

   }

} // end of SetStopLoss


//+------------------------------------------------------------------+

//| Deci Quote Adjuster                                              | 

//+------------------------------------------------------------------+

int DeciQuoteAdjuster(string SYMBOL)

{   

   int DQADJ = 1;

   int DIGITS = MarketInfo(SYMBOL,MODE_DIGITS);

   if(DIGITS == 5 || DIGITS == 3 ) DQADJ = 10;


   return(DQADJ);

}





THX

Reason: