//+------------------------------------------------------------------+
//|                                                    LineAlert.mq4 |
//+------------------------------------------------------------------+

#property copyright "Designerr, 2015"
#property link      "http://www.tradelikeapro.ru"

#property indicator_chart_window

extern color  LineColor  = DarkGoldenrod;
extern color  AlertColor = Brown;
extern int    LineStyle  = 0;
extern int    LineWidth  = 2;
extern bool   LineOnBack = true;

extern bool   SavePos    = true;

extern bool   OneTouch   = true;

extern bool   RangeTouch = false;
extern int    PipRange   = 10;
extern string SoundFile  = "alert2.wav";
extern bool   SendEmail  = false;

string LineName = "AlertLine";
string name     = "";
string global   = "";
double target   = 0.0;
double _init    = 0.0;
string symb     = Symbol();

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init(){
  
  int i = 1;
  
  name = LineName+"_"+i;
  
  while(ObjectFind(name) != -1){
    
    i++;
    name = LineName+"_"+i;
  }
  
  global = symb+"_"+name;
  
  if(!SavePos && GlobalVariableCheck(global)){
    
    GlobalVariableDel(global);
  }
  
  CreateObject();
  target = 0.0;
  
  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit(){
  
  double current = NormalizeDouble(ObjectGet(name, OBJPROP_PRICE1), 5);
  
  if(SavePos && current != _init){
    
    GlobalVariableSet(global, current);
    
  }else if(!SavePos && GlobalVariableCheck(global)){
    
    GlobalVariableDel(global);
  }
  
  ObjectDelete(name);
  return(0);
  
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start(){
  
  string tf      = TfToStr(Period());
  double g_price = 0.0;
  
  if(ObjectFind(name) == -1){
    
    CreateObject();
    target = 0.0;
  }
  
  double current = NormalizeDouble(ObjectGet(name, OBJPROP_PRICE1), 5);
  
  if(current == target){
    
    ObjectSet(name, OBJPROP_COLOR, AlertColor);
    
  }else{
    
    ObjectSet(name, OBJPROP_COLOR, LineColor);
    target = 0.0;
  }
  
  if((Ask >= current && Bid <= current) && current != target){
    
    if(OneTouch)  Alert(symb+" "+tf+": Цена достигла уровня "+current+".");
    if(SendEmail) SendMail("Сигнал от LineAlert | "+symb+" "+tf, TimeToStr(TimeCurrent(), TIME_SECONDS)+" | "+symb+" "+tf+": Цена достигла уровня "+current+".");
    target = current;
  }
  
  if(RangeTouch && current != target){
    
    if(Ask+PipRange*Point >= current && Bid-PipRange*Point <= current) PlaySound(SoundFile);
  }
  
  return(0);
}

//+------------------------------------------------------------------+
void CreateObject(){
//+------------------------------------------------------------------+
  
  double price = 0.0;
  
  if(SavePos && GlobalVariableCheck(global)){
    
    price = GlobalVariableGet(global);
    
  }else{
    
    price = NormalizeDouble(Bid+PipRange*10*Point, Digits);
    _init = price;
  }
  
  ObjectCreate(name, OBJ_HLINE, 0, 0, price);
  ObjectSet(name, OBJPROP_COLOR, LineColor);
  ObjectSet(name, OBJPROP_STYLE, LineStyle);
  ObjectSet(name, OBJPROP_WIDTH, LineWidth);
  ObjectSet(name, OBJPROP_BACK,  LineOnBack);
  
}

//+------------------------------------------------------------------+
string TfToStr(int t){
//+------------------------------------------------------------------+
  
  switch (t)  {
    case     1 :  return("M1");
    case     5 :  return("M5");
    case    15 :  return("M15");
    case    30 :  return("M30");
    case    60 :  return("H1");
    case   240 :  return("H4");
    case  1440 :  return("D1");
    case 10080 :  return("W1");
    case 43200 :  return("MN");
  }  
  return("");
}
