//+------------------------------------------------------------------+
//|                                                   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;           //отправлять на почту
extern string  Sound_Open  = "alert.wav";    //звуковой сигнал при открытии ордера
extern string  Sound_Close = "alert.wav";    //звуковой сигнал при закрытии ордера
int Orders;
//+------------------------------------------------------------------+
int start()
  {
   int O=OrdersTotal();
   string txt;
   if (Orders!=O) 
   {
      if (Orders>O) txt = HistoryOrder();
      if (Orders<O) txt = Order();
      Comment(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)," ",txt);
      if (MailSend) SendMail("Торговый счет "+AccountNumber(),txt);
   }
   Orders=O;
   return(0);
  }
//+------------------------------------------------------------------+
string HistoryOrder()
{
   string txt=NULL;
   double OCP;
   int i=OrdersHistoryTotal()-1;
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
   {                                     
      OCP=OrderClosePrice();
      if (OCP==OrderStopLoss()  ) txt="SL";
      if (OCP==OrderTakeProfit()) txt="TP";
      txt=StringConcatenate("Закрыт ордер ",OrderTicket()," ",Symbol()," по ",txt," ",
      DoubleToStr(OCP,Digits)," прибыль ",DoubleToStr(OrderProfit()+OrderSwap()+OrderCommission(),2)," Баланс = ",DoubleToStr(AccountBalance(),2));
      if (Sound_Close!="") PlaySound(Sound_Close);
      return(txt);
   }  
   return(txt);
}
//+------------------------------------------------------------------+
string Order()
{
   string txt=NULL;
   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("Открыт ордер N ",txt," ",OrderTicket()," ",Symbol()," по цене ",
      DoubleToStr(OrderOpenPrice(),Digits)," лот ",DoubleToStr(OrderLots(),2)," SL = ",DoubleToStr(OrderStopLoss(),2)," TP = ",DoubleToStr(OrderTakeProfit(),2));
      if (Sound_Open!="") PlaySound(Sound_Open);
      return(txt);
   }  
   return(txt);
}
//+------------------------------------------------------------------+

