//+------------------------------------------------------------------+
//|                                    Color Candles Alerts v1.3.mq4 |
//|                           Copyright ©, tigpips, mod. fxdaytrader |
//|                     alerts, range-calcfix 11.2013 by fxdaytrader |
//+------------------------------------------------------------------+
#property copyright "Copyright © tigpips et al"
#property link      "http://forexBaron.net"

#property indicator_chart_window
#property indicator_buffers 4

#property indicator_color1 SkyBlue
#property indicator_color2 SkyBlue
#property indicator_color3 SkyBlue
#property indicator_color4 SkyBlue
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 4
#property indicator_width4 4

extern color color1 = SkyBlue;
extern double pips = 20.0;
extern string rmh="1:close-open, 2:high-low";
extern int RangeMethod=2;

extern string ahi="******* ALERT SETTINGS:";
extern int    AlertCandle=0;
extern bool   PopupAlerts            = true;
extern bool   EmailAlerts            = false;
extern bool   PushNotificationAlerts = false;
extern bool   SoundAlerts            = false;
extern string SoundFile              = "alert.wav";
double pips2dbl;

double buffer1[],buffer2[],buffer3[],buffer4[];

int init()
{
//---- indicators
 SetIndexStyle(0,DRAW_HISTOGRAM,0,1,color1);
 SetIndexBuffer(0,buffer1);
 SetIndexStyle(1,DRAW_HISTOGRAM,0,1,color1);
 SetIndexBuffer(1,buffer2);
 SetIndexStyle(2,DRAW_HISTOGRAM,0,4,color1);
 SetIndexBuffer(2,buffer3);
 SetIndexStyle(3,DRAW_HISTOGRAM,0,4,color1);
 SetIndexBuffer(3,buffer4);

//
 int Multiplier = 1;
 if(Digits == 3 || Digits == 5) Multiplier = 10;
 if(Digits == 6) Multiplier = 100;   
 if(Digits == 7) Multiplier = 1000;
 pips2dbl = Multiplier*Point; 
 
 if (RangeMethod >= 3) RangeMethod=2;//errorchecking
//----
 return(0);
}

int deinit()
{

 return(0);
}

int start()
{   
 int i,direction,imax,counted_bars=IndicatorCounted();
 if(counted_bars>0) imax=Bars-IndicatorCounted();
 else imax=Bars-1;    
//----
 for(i=imax;i>=0;i--) 
 {
  Resetall(i);
  direction=GetBar(i);
  if(direction>0)
  {
   buffer1[i] = iHigh(NULL,0,i);
   buffer2[i] = iLow(NULL,0,i);  
   buffer3[i] = iOpen(NULL,0,i);
   buffer4[i] = iClose(NULL,0,i);
  }
 } 
//----
 return(0);
}

void Resetall(int shift) 
{
 buffer1[shift] = EMPTY_VALUE;
 buffer2[shift] = EMPTY_VALUE;
 buffer3[shift] = EMPTY_VALUE;
 buffer4[shift] = EMPTY_VALUE;
 return;
}
//+------------------------------------------------------------------+
int GetBar(int shift) 
{
 double open=iOpen(NULL,0,shift);
 double close=iClose(NULL,0,shift);
 double high=iHigh(NULL,0,shift);
 double low=iLow(NULL,0,shift);
 double range;
 /*
 int buffer;
 if(Digits == 4)
 {
   buffer = 1000;
 }
 else if(Digits == 5)
 {
   buffer = 10000;
 }
 */
 
 if (RangeMethod==1) range=MathAbs(close-open);
 if (RangeMethod==2) range=MathAbs(high-low);
 //if (range*buffer >= pips)
 if ((range/pips2dbl) >= pips)
 {
   if (shift==AlertCandle) doAlerts((range/pips2dbl));
   return(1);
 }
 else
 {
   return(0);
 }

}

//////////////////////////////////////////////////////////////////////
//stuff by fxdaytrader:
void doAlerts(double candlerange) {
 static datetime lastAlertTime=0;
 if (lastAlertTime!=iTime(NULL,0,0)) {
  lastAlertTime=iTime(NULL,0,0);
  string srange="UNKOWN";
  if (RangeMethod==1) srange="close-open";
  if (RangeMethod==2) srange="high-low";
  
  string msg="Color Candles Alert on "+Symbol()+", period "+TFtoStr(Period())+": Candle at "+TimeToStr(iTime(NULL,0,AlertCandle))+" (servertime) with Range="+DoubleToStr(candlerange,2)+" pips range ("+srange+"), so >= "+DoubleToStr(pips,2)+" pips, formed";
  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);
 }//if (lastAlertTime!=iTime(NULL,0,0) {
}//void doAlerts(string msg,string SoundFile) {

string TFtoStr(int 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) {
//end stuff by fxdaytrader
//////////////////////////////////////////////////////////////////////