//+------------------------------------------------------------------+
//|                                             Stochastic Alert.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://ForexBaron.net"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 3
#property indicator_color1 DodgerBlue
#property indicator_color2 LightGray
#property indicator_color3 LightGray
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_DOT
#property indicator_style3 STYLE_DOT
//---- input parameters
extern int STOKPeriod=5;
extern int STODPeriod=3;
extern int STOSlowing=8;
extern int STOMethod=0;
extern int STOMode=0;
extern int ApplyTo=0;

extern bool AlertMode=true;
extern int OverBought=80;
extern int OverSold=20;

extern string ahi="******* ALERT SETTINGS:";
extern int    AlertCandle            = 0;//0:current, 1:last bar, etc.
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";
int lastAlert=3;

//---- buffers
double STOBuffer[];
double STOOBBuffer[];
double STOOSBuffer[];

int TimeFrame;
string TF;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,STOBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,STOOBBuffer);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,STOOSBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="STO-Alert("+STOKPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetIndexLabel(1,"OverBought");
   SetIndexLabel(2,"OverSold");
//----
   SetIndexDrawBegin(0,STOKPeriod);
   
    
switch(TimeFrame){
	
		case 1:		TF="M1";  break;
		case 5:		TF="M5";  break;
		case 15:		TF="M15"; break;
		case 30:		TF="M30"; break;
		case 60:		TF="H1";  break;
		case 240:	TF="H4";  break;
		case 1440:	TF="D1";  break;
		case 10080:	TF="W1";  break;
		case 43200:	TF="MN1"; break;
		default:	  {TimeFrame = Period(); init(); return(0);}
	}
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i,counted_bars=IndicatorCounted();
//----
   if(Bars<=STOKPeriod) return(0);
//----
   i=Bars-STOKPeriod-1;

 
   if(counted_bars>=STOKPeriod) i=Bars-counted_bars-1;
   while(i>=0)
   {
      STOBuffer[i]=iStochastic(NULL,0,STOKPeriod,STODPeriod,STOSlowing,STOMethod,STOMode,ApplyTo,i);
      STOOBBuffer[i]=OverBought;
      STOOSBuffer[i]=OverSold;
      i--;
   }
   
if(AlertMode)
   {
//       if(lastAlert!=1 && STOBuffer[AlertCandle+1]<OverBought && STOBuffer[AlertCandle]>=OverBought){
      if(lastAlert!=1 && STOBuffer[AlertCandle+1]>OverSold && STOBuffer[AlertCandle]<=OverSold){
         lastAlert=1;
         doAlerts("STOCH cross DOWN "+OverSold,SoundFileShort); 
         }
         //Alert(Symbol()+" [ "+TF+" ] STO Overbought @ level "+OverBought);
         
         
//       else if(lastAlert!=2 && STOBuffer[AlertCandle+1]>OverSold && STOBuffer[AlertCandle]<=OverSold){
      else if(lastAlert!=2 && STOBuffer[AlertCandle+1]<OverBought && STOBuffer[AlertCandle]>=OverBought){
         lastAlert=2; 
         doAlerts("STOCH cross UP "+OverBought,SoundFileLong); 
         }
         //Alert(Symbol()+" [ "+TF+" ] STO Oversold @ level "+OverSold);
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//                                                                   +
//+------------------------------------------------------------------+
void doAlerts(string msg,string SoundFile) {

 msg="Stochastic Alert on "+Symbol()+", TF "+TFtoStr(Period())+": "+msg;
//  string emailsubject="MT4 alert on acc. "+AccountNumber()+", "+WindowExpertName()+" - Alert on "+Symbol()+", period "+TFtoStr(Period());
 string emailsubject="Alert on "+Symbol()+", TF"+TFtoStr(Period());
 
  if (PopupAlerts) Alert(msg);
  if (EmailAlerts) SendMail(emailsubject,msg);
  if (PushNotificationAlerts) SendNotification(msg);
  if (SoundAlerts) PlaySound(SoundFile);

return;

}
//+------------------------------------------------------------------+
//                                                                   +
//+------------------------------------------------------------------+
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) {
