//+------------------------------------------------------------------+
//|                                                   AlertOrder.mq4 |
//|                               Copyright © 2011, Vladimir Hlystov |
//|                                                cmillion@narod.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, http://cmillion.narod.ru"
#property link      "cmillion@narod.ru"
#property indicator_chart_window
extern bool MailSend    = true;//отправлять на почту
int Orders;
//+------------------------------------------------------------------+
int init()
  {
   Orders=OrdersTotal();
   return(0);
  }
//+------------------------------------------------------------------+
int start()
  {
   int O=OrdersTotal();
   string txt;
   if (Orders!=O) 
   {
      if (Orders>O) txt = HistoryOrder();
      if (Orders<O) txt = Order();
      Alert(txt);
      if (MailSend) SendMail("Торговый счет "+AccountNumber(),txt);
   }
   Orders=O;
   return(0);
  }
//+------------------------------------------------------------------+
string HistoryOrder()
{
   string txt,txt1;
   double OCP;
   int tip;
   int i=OrdersHistoryTotal()-1;
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
   {                                     
      tip=OrderType();
      OCP=OrderClosePrice();
      if (OCP==OrderStopLoss()  ) txt1="SL";
      if (OCP==OrderTakeProfit()) txt1="TP";
      if (tip==OP_BUY) txt="BUY";
      if (tip==OP_SELL) txt="SELL";
      if (tip==OP_BUYSTOP) txt="BUYSTOP";
      if (tip==OP_SELLSTOP) txt="SELLSTOP";
      if (tip==OP_BUYLIMIT) txt="BUYLIMIT";
      if (tip==OP_SELLLIMIT) txt="SELLLIMIT";
      txt=StringConcatenate("Закрыт ордер ",txt,"  ",Symbol(),"  ",OrderTicket()," по ",txt1,"  ",
      DoubleToStr(OCP,Digits),"\n прибыль ",DoubleToStr(OrderProfit()+OrderSwap()+OrderCommission(),2),"    Баланс = ",DoubleToStr(AccountBalance(),2));
      return(txt);
   }  
}
//+------------------------------------------------------------------+
string Order()
{
   string txt;
   int tip;
   int i=OrdersTotal()-1;
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   {                                     
      tip=OrderType();
      if (tip==OP_BUY) txt="BUY";
      if (tip==OP_SELL) txt="SELL";
      if (tip==OP_BUYSTOP) txt="BUYSTOP";
      if (tip==OP_SELLSTOP) txt="SELLSTOP";
      if (tip==OP_BUYLIMIT) txt="BUYLIMIT";
      if (tip==OP_SELLLIMIT) txt="SELLLIMIT";
      txt=StringConcatenate("Открыт ордер ",txt,"  ",Symbol(),"  N ",OrderTicket(),"\nпо цене ",
      DoubleToStr(OrderOpenPrice(),Digits)," лот ",DoubleToStr(OrderLots(),2)," SL = ",DoubleToStr(OrderStopLoss(),2)," TP = ",DoubleToStr(OrderTakeProfit(),2));
      return(txt);
   }  
}
//+------------------------------------------------------------------+

