//+------------------------------------------------------------------+
//|                                       ADR_Projections Alerts.mq4 |
//|                                   Copyright © 2012, ericjschroed |
//|                     alerts by fxdaytrader, http://ForexBaron.net |
//+------------------------------------------------------------------+
//////////////////////////////////////////////////////////////////////
// !!! YOU MAY COMPILE THIS INDICATOR WITH THE BUILD 509 COMPILER, download the metaeditor for build 509 at http://www.forexfactory.com/showthread.php?t=470340 !!!
//////////////////////////////////////////////////////////////////////
#property copyright "Copyright © 2012, ericjschroed // Alerts by fxdaytrader"
#property link      "http://ForexBaron.net"
#property indicator_chart_window

extern int     NumOfDays         = 20;
extern color   LineTargetColor   = DarkOrange;
extern color   LineExceededColor = DarkOrange;
extern color   TextColor         = Silver;
extern int     ShiftLabel        = 9;
extern bool    FullScreenLines   = true;
extern string  FSL = "If false, lines start at current bar";
extern int     Line_Length       = 17;
//
extern string ahi0=" ******* Alert settings:";
extern double PipsAwayFromADRPips  = 20.0;
int  SignalCandle           = 0;
extern bool PopupAlerts            = TRUE;
extern bool EmailAlerts            = FALSE;
extern bool PushNotificationAlerts = FALSE;
extern bool SoundAlerts            = FALSE;
extern string SoundFileLong        = "alert.wav";
extern string SoundFileShort       = "alert2.wav";
extern bool ShowScreenComment = TRUE;
//----------------------------------------

color LineColor;

string Period_Text, LineLabel, AvgRgLine, PeriodTxt;
int    LineType;
int multiplier;

//double pnt; int    dig;
int Multiplier; double pips2dbl;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
 BrokerDigitAdjust(Symbol());//fxdaytrader
/*
  pnt = MarketInfo(Symbol(),MODE_POINT);
  dig = MarketInfo(Symbol(),MODE_DIGITS);
  if (dig == 3 || dig == 5) {
    pnt *= 10;
  }  
*/
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
   if (ShowScreenComment) Comment("");
   DeleteLines();
   
   //----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{

   //---- exit if period is greater than hourly charts
   if(Period() > PERIOD_D1)
   {
      return(-1); // then exit
   }

   
int c=0;
double sum=0;
double AvgRange=0;
double AvgRgHigh, AvgRgLow, TodaysRange;

for (int i=1; i<Bars-1; i++)  {
    double hi = iHigh(NULL,PERIOD_D1,i);
    double lo = iLow(NULL,PERIOD_D1,i);
    datetime dt = iTime(NULL,PERIOD_D1,i);
    if (TimeDayOfWeek(dt) > 0 && TimeDayOfWeek(dt) < 6)  {
      sum += hi - lo;
      c++;  
         
      AvgRange = AvgRange + (iHigh(NULL, PERIOD_D1, i) - iLow(NULL, PERIOD_D1, i));
      
      if (c>=NumOfDays) break;    
  } }
  hi = iHigh(NULL,PERIOD_D1,0);
  lo = iLow(NULL,PERIOD_D1,0);
  
  AvgRange = AvgRange / NumOfDays;
  TodaysRange = (hi-lo);
    
  AvgRgHigh = NormalizeDouble(lo + AvgRange, Digits-1);
  AvgRgLow =  NormalizeDouble(hi - AvgRange, Digits-1);
   
//-----------------------------------------------------------------------   
   
   if(FullScreenLines)
      LineType = OBJ_HLINE;
   else
      LineType = OBJ_TREND;
      
   //---- Set line labels on chart window

   if(TodaysRange > AvgRange)
      LineColor = LineExceededColor;
   else
      LineColor = LineTargetColor;

   string HighName = "ADR "+"High";
   string LowName = "ADR "+"Low";
   string HighText = HighName+": "+DoubleToStr(AvgRgHigh,Digits-1);
   string LowText = LowName+": "+DoubleToStr(AvgRgLow,Digits-1);
   string HighLabel = HighName+" Label";
   string LowLabel = LowName+" Label";

   DrawLine(HighLabel, HighText, HighName, LineType, LineColor, AvgRgHigh, TodaysRange, AvgRange);
   DrawLine(LowLabel, LowText, LowName, LineType, LineColor, AvgRgLow, TodaysRange, AvgRange);
   //Alerts:
   static bool lastAlertTime1=0;
   static bool lastAlertTime2=0;
   double price = iClose(Symbol(),0,SignalCandle);
    if (price>(AvgRgHigh+PipsAwayFromADRPips*pips2dbl)) {
     if (lastAlertTime1!=iTime(Symbol(),PERIOD_D1,0)) {
      lastAlertTime1=iTime(Symbol(),PERIOD_D1,0);
      doAlerts("Upper ADR projection broken",SoundFileLong);
     }
    }
    
    if (price<(AvgRgLow-PipsAwayFromADRPips*pips2dbl)) {
     if (lastAlertTime2!=iTime(Symbol(),PERIOD_D1,0)) {
      lastAlertTime2=iTime(Symbol(),PERIOD_D1,0);
      doAlerts("Lower ADR projection broken",SoundFileShort);
     }
    }
    
    if (ShowScreenComment) Comment("ADR_Projections Alerts, © 2012, ericjschroed // Alerts by fxdaytrader ***** http://ForexBaron.net"+"\n"+
                                   " upper projection: "+DoubleToStr(AvgRgHigh,Digits)+", lower projection: "+DoubleToStr(AvgRgLow,Digits)+"\n"+
                                   " alerts at price>"+DoubleToStr((AvgRgHigh+PipsAwayFromADRPips*pips2dbl),Digits)+" or price<"+DoubleToStr((AvgRgLow-PipsAwayFromADRPips*pips2dbl),Digits));




   
   //---- End Of Program
   return(0);
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine to draw lines and labels                                |                                                                                  |
//+-------------------------------------------------------------------------------------------+

void DrawLine(string LineLabel, string LineText, string AvgRgLine, int LineType, color LineColor, double LinePrice, double TodaysRange, double AvgRange)
   {
      if(ObjectFind(LineLabel) != 0)
      {
         ObjectCreate(LineLabel, OBJ_TEXT, 0, Time[0]+Period()*60*ShiftLabel, LinePrice);
         ObjectSetText(LineLabel, LineText, 8, "Verdana", TextColor);
      }
      else
      {
         ObjectMove(LineLabel, 0, Time[0]+Period()*60*ShiftLabel, LinePrice);
         ObjectSetText(LineLabel, LineText, 8, "Verdana", TextColor);
      }

   int Style, Width;
   {
      Style = STYLE_DOT;
      Width = 1;
   }

      if(ObjectFind(AvgRgLine) != 0)
      
      {
         ObjectCreate(AvgRgLine, LineType, 0, Time[1]+Period()*60, LinePrice, Time[0]+Period()*60*Line_Length, LinePrice);
         ObjectSet(AvgRgLine, OBJPROP_STYLE, Style);
         ObjectSet(AvgRgLine, OBJPROP_COLOR, LineColor);
         ObjectSet(AvgRgLine, OBJPROP_WIDTH, Width);
        ObjectSet(AvgRgLine, OBJPROP_RAY, false);
       
      }
      else
      {

         ObjectMove(AvgRgLine, 0, Time[1]+Period()*60, LinePrice);
         ObjectMove(AvgRgLine, 1, Time[0]+Period()*60*Line_Length, LinePrice);
    }
   }
//---------------------------------------------------------


void DeleteLines(){

  int    obj_total=ObjectsTotal();
  string ObjName;
  
  for(int i = ObjectsTotal() - 1; i > -1; i--)
    {
         
     ObjName=ObjectName(i); 
     if (StringFind(ObjName,"ADR ",0) == 0) ObjectDelete(ObjName);
    }
}
//+------------------------------------------------------------------+

void BrokerDigitAdjust(string symbol) {
 Multiplier = 1;
 if (MarketInfo(symbol,MODE_DIGITS) == 3 || MarketInfo(symbol,MODE_DIGITS) == 5) Multiplier = 10;
 if (MarketInfo(symbol,MODE_DIGITS) == 6) Multiplier = 100;   
 if (MarketInfo(symbol,MODE_DIGITS) == 7) Multiplier = 1000;
 pips2dbl = Multiplier*MarketInfo(symbol,MODE_POINT);
}

void doAlerts(string msg,string SoundFile) {
 static bool lastAlertTime=0;
  
        msg="ADR_Projections Alert on "+Symbol()+", period "+TFtoStr(Period())+": "+msg+", BID = "+DoubleToStr(MarketInfo(Symbol(),MODE_BID),Digits)+", servertime: "+TimeToStr(TimeCurrent());
 string emailsubject="MT4 alert on acc. "+AccountNumber()+", "+WindowExpertName()+" - Alert on "+Symbol()+", period "+TFtoStr(Period());
  if (PopupAlerts) Alert(msg);
  if (EmailAlerts) SendMail(emailsubject,msg);
  if (PushNotificationAlerts) SendNotification(msg);
  if (SoundAlerts) PlaySound(SoundFile);
}//void doAlerts(string msg,string SoundFile) {

string TFtoStr(int period) {
 if (period==0) period=Period();
 switch(period) {
  case 1     : return("M1");  break;
  case 5     : return("M5");  break;
  case 15    : return("M15"); break;
  case 30    : return("M30"); break;
  case 60    : return("H1");  break;
  case 240   : return("H4");  break;
  case 1440  : return("D1");  break;
  case 10080 : return("W1");  break;
  case 43200 : return("MN1"); break;
  default    : return(DoubleToStr(period,0));
 }
 return("UNKNOWN");
}//string TFtoStr(int period) {