//+------------------------------------------------------------------+
//|                                         WPR Candles (alerts).mq5 |
//|                                             modified by jblanked |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Christof Risch (iya)"
#property link      "https://www.github.com/jblanked"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots 4

#property indicator_type1 DRAW_HISTOGRAM
#property indicator_type2 DRAW_HISTOGRAM
#property indicator_type3 DRAW_HISTOGRAM
#property indicator_type4 DRAW_HISTOGRAM

#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3

#property indicator_label1 "Wick"
#property indicator_label1 "Wick"
#property indicator_label1 "Candle"
#property indicator_label1 "Candle"

#property indicator_color1  clrGreen//wick
#property indicator_color2  clrRed//wick
#property indicator_color3  clrGreen//candle
#property indicator_color4  clrRed//candle

input ENUM_TIMEFRAMES TimeFrame       = PERIOD_CURRENT;
input int	           WPR_Period		= 14;              // Wpr period
input int	           Overbought		= -20;             // Overbought level
input int	           Oversold			= -80;             // Oversold level
input bool             alertsOn        = true;            // Alerts on true/false?
input bool             alertsOnCurrent = false;           // Alerts current bar true/false?
input bool             alertsMessage   = true;            // Alerts message true/false?
input bool             alertsSound     = false;           // Alerts sound true/false?
input bool             alertsEmail     = false;           // Alerts email true/false?
input bool             alertsNotify    = false;           // Alerts notification true/false?
input string           soundFile       = "alert2.wav";    // Alerts Sound file

double Bar1[],Bar2[],Candle1[],Candle2[],trend[],count[];
string indicatorFileName,signal;
		 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int OnInit()
{
	SetIndexBuffer(0,Bar1); 
	SetIndexBuffer(1,Bar2);				
	SetIndexBuffer(2,Candle1); 
	SetIndexBuffer(3,Candle2); 
	SetIndexBuffer(4,trend);
	SetIndexBuffer(5,count);
	
   
   IndicatorSetString(INDICATOR_SHORTNAME,timeFrameToString(TimeFrame)+" WPR Candles:("+(string)WPR_Period+")");
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
void SetCandleColor(int col, int i)
{
	double high,low,bodyHigh,bodyLow;
	
	bodyHigh = fmax(iOpen(_Symbol,PERIOD_CURRENT,i),iClose(_Symbol,PERIOD_CURRENT,i));
   bodyLow  = fmin(iOpen(_Symbol,PERIOD_CURRENT,i),iClose(_Symbol,PERIOD_CURRENT,i));
   high		= iHigh(_Symbol,PERIOD_CURRENT,i);
   low		= iLow(_Symbol,PERIOD_CURRENT,i);

	Bar1[i]    = Bar2[i]    = EMPTY_VALUE;
   Candle1[i] = Candle2[i] = EMPTY_VALUE;
	
	switch(col)
	{
		case 1: Bar1[i] = high;	Bar2[i] = low; Candle1[i] = bodyHigh;	Candle2[i] = bodyLow; break;
      case 2: Bar2[i] = high;	Bar1[i] = low; Candle2[i] = bodyHigh;	Candle1[i] = bodyLow; break;
	
	}
}

int  OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int limit = rates_total - prev_calculated;
   
   if(prev_calculated < 1)
   {
   ArrayInitialize(Bar1,EMPTY_VALUE); 
	ArrayInitialize(Bar2,EMPTY_VALUE);				
	ArrayInitialize(Candle1,EMPTY_VALUE); 
	ArrayInitialize(Candle2,EMPTY_VALUE); 
	ArrayInitialize(trend,EMPTY_VALUE);
	ArrayInitialize(count,EMPTY_VALUE);
	}
	else
	   limit++;
	   
	ArraySetAsSeries(Bar1,true); 
	ArraySetAsSeries(Bar2,true);				
	ArraySetAsSeries(Candle1,true); 
	ArraySetAsSeries(Candle2,true); 
	ArraySetAsSeries(trend,true);
	ArraySetAsSeries(count,true);
	
   
	for (int i = 0; i < limit; i++)
	{
		double wpr = iWPR_Price(i); 
		trend[i]   = (wpr>Overbought) ? 1 : (wpr<Oversold) ? -1 : 0;
		if (trend[i] ==  1) SetCandleColor(1,i); 
      if (trend[i] == -1) SetCandleColor(2,i);	
	}
	
	if (alertsOn)
   {
      int whichBar = (alertsOnCurrent) ? 0 : 1;
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] == 1) doAlert(whichBar,"buy");
         if (trend[whichBar] ==-1) doAlert(whichBar,"sell");
      }         
   }   
     
return(rates_total);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------

void doAlert(int forBar, string doWhat)
{
     static string   previousAlert="nothing";
     static datetime previousTime;
     string message;
   
     if (previousAlert != doWhat || previousTime != iTime(_Symbol,PERIOD_CURRENT,forBar)) {
         previousAlert  = doWhat;
         previousTime   = iTime(_Symbol,PERIOD_CURRENT,forBar);


          //
          //
          //

          message = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToString(TimeLocal(),TIME_SECONDS)+" Wpr candles "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(_Symbol+" Wpr candles ",message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}



double iWPR_Price(int shift)
  {
   int WPR_val = iWPR(_Symbol,PERIOD_CURRENT,WPR_Period);
   
   return CopyBufferMQL4(WPR_val,0,shift);
  }
  
 
 

double CopyBufferMQL4(int handle,int index,int shift)
  {
   double buf[];
   switch(index)
     {
      case 0: if(CopyBuffer(handle,0,shift,1,buf)>0)
         return(buf[0]); break;
      default: break;
     }
   return(EMPTY_VALUE);
  }
