//+------------------------------------------------------------------+
//|                      MTF_Stochastic_with_custom_alert_levels.mq4 |
//|  Copyright © 2017, ScarletPip, based on initial work by Keris2112|
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2017 ScarletPip"
#property link      "evidencebasedfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 clrPaleGreen
#property indicator_color2 clrPink
#property indicator_color3 clrLawnGreen
#property indicator_color4 clrCrimson
#property indicator_color5 clrPaleGreen
#property indicator_color6 clrPink
#property indicator_level1 80
#property indicator_level2 20
#property indicator_maximum 100
#property indicator_minimum 0

//---- input parameters
/*************************************************************************
PERIOD_M1   1
PERIOD_M5   5
PERIOD_M15  15
PERIOD_M30  30 
PERIOD_H1   60
PERIOD_H4   240
PERIOD_D1   1440
PERIOD_W1   10080
PERIOD_MN1  43200
You must use the numeric value of the timeframe that you want to use
when you set the TimeFrame' value with the indicator inputs.
---------------------------------------
MODE_SMA    0 Simple moving average, 
MODE_EMA    1 Exponential moving average, 
MODE_SMMA   2 Smoothed moving average, 
MODE_LWMA   3 Linear weighted moving average. 
You must use the numeric value of the MA Method that you want to use
when you set the 'ma_method' value with the indicator inputs.

**************************************************************************/
extern int    LevelUp2               = 80; //Level Up
extern int    LevelDown2             = 20; //Level Down
extern int TimeFrame2=15;
extern int KPeriod2=30;
extern int DPeriod2=5;
extern int Slowing2=12;
extern int MAMethod2=1;
extern int PriceField2=0;// PriceField:  0=Hi/Low   1=Close/Close
extern int indiArrowSize2 = 1;
extern int chartArrowSize2 = 1;
extern string note_TimeFrames2 = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN";
extern string __MA_Method2 = "SMA0 EMA1 SMMA2 LWMA3";
extern string __PriceField2 = "0=Hi/Low   1=Close/Close";
extern string Alert_Settings2________="-------< Alerts >-------";//Alert Settings ........................................................................................
extern bool   AlertsOn2              = TRUE;//Alerts On 
extern bool   AlertsOnCurrent2       = TRUE;//Alerts On first
extern bool   AlertsMessage2         = TRUE;//Alerts Message
extern bool   AlertsSound2           = TRUE;//Alerts Sound
extern bool   AlertsNotify2          = TRUE;//Alerts Notify
extern bool   AlertsEmail2           = TRUE;//Alerts Email
extern string InpSoundFile2          ="news.wav";//Sound File
//extern string __Price = "0O,1C 2H3L,4Md 5Tp 6WghC: Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";


double ExtMapBuffer1b[];
double ExtMapBuffer2b[];
double ExtMapBuffer3b[];
double ExtMapBuffer4b[];
double ExtMapBuffer5b[];
double ExtMapBuffer6b[];
datetime last_t2=0;
int Pip2 = 1,Value2=0,zz2=0,xx2=0;
string notice2 = "nothing";
datetime TimeNow2=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator line
   SetIndexBuffer(0,ExtMapBuffer1b);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2b);
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexLabel(0,  "MTF_Stochastic("+KPeriod2+","+DPeriod2+","+Slowing2+")TF"+TimeFrame2+"");
   SetIndexLabel(1,"MTF_Stochastic("+KPeriod2+","+DPeriod2+","+Slowing2+")TF"+TimeFrame2+"");
   SetIndexBuffer(2,ExtMapBuffer3b);
   SetIndexStyle(2,DRAW_ARROW,EMPTY,indiArrowSize2);
   SetIndexArrow(2,233);
   SetIndexBuffer(3,ExtMapBuffer4b);
   SetIndexStyle(3,DRAW_ARROW,EMPTY,indiArrowSize2);
   SetIndexArrow(3,234);
   SetIndexBuffer(4,ExtMapBuffer5b);
   SetIndexStyle(4,DRAW_ARROW,EMPTY,indiArrowSize2);
   SetIndexArrow(4,233);
   SetIndexBuffer(5,ExtMapBuffer6b);
   SetIndexStyle(5,DRAW_ARROW,EMPTY,indiArrowSize2);
   SetIndexArrow(5,234);
//---- name for DataWindow and indicator subwindow label   
   switch(TimeFrame2)
   {
      case 1 : string TimeFrameStr2="Period_M1"; break;
      case 5 : TimeFrameStr2="Period_M5"; break;
      case 15 : TimeFrameStr2="Period_M15"; break;
      case 30 : TimeFrameStr2="Period_M30"; break;
      case 60 : TimeFrameStr2="Period_H1"; break;
      case 240 : TimeFrameStr2="Period_H4"; break;
      case 1440 : TimeFrameStr2="Period_D1"; break;
      case 10080 : TimeFrameStr2="Period_W1"; break;
      case 43200 : TimeFrameStr2="Period_MN1"; break;
      default : TimeFrameStr2="Current Timeframe";
   } 
   IndicatorShortName("MTF_Stochastic2("+KPeriod2+","+DPeriod2+","+Slowing2+") "+TimeFrameStr2);  
   start();
   return(0);
  }
//----
   
 
//+------------------------------------------------------------------+
//| MTF Stochastic                                                   |
//+------------------------------------------------------------------+
 int deinit()
  {
   for (int i2=Bars;i2>=0;i2--){
      ObjectDelete("st2"+Symbol()+"per"+Period()+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0));
   }
   return(0);
  }

bool up2_a=false;
bool dn2_a=false;
int start()
  {
   datetime TimeArray2[];
   ArrayResize(TimeArray2,Bars);
   int    i2,limit2,y2=0,counted_bars2=IndicatorCounted();
    
// Plot defined timeframe on to current timeframe   
   ArrayCopySeries(TimeArray2,MODE_TIME,Symbol(),TimeFrame2); 
   
 //  limit=Bars-counted_bars+TimeFrame/Period(); //igorad
limit2=Bars-1;
limit2=MathMax(limit2,TimeFrame2/Period());
//limit=MathMin(limit,BarsToCount);
   for(i2=0,y2=0;i2<limit2;i2++)
   {
   if (Time[i2]<TimeArray2[y2]) y2++; 
   
 /***********************************************************   
   Add your main indicator loop below.  You can reference an existing
      indicator with its iName  or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator timeframe
   Rule 3:  Use 'y' for the indicator's shift value
 **********************************************************/  
   ExtMapBuffer3b[i2]=EMPTY_VALUE;  
   ExtMapBuffer4b[i2]=EMPTY_VALUE;
   ExtMapBuffer5b[i2]=EMPTY_VALUE;  
   ExtMapBuffer6b[i2]=EMPTY_VALUE;
   ExtMapBuffer1b[i2]=EMPTY_VALUE;  
   ExtMapBuffer2b[i2]=EMPTY_VALUE;

   ExtMapBuffer1b[i2]=iStochastic(NULL,TimeFrame2,KPeriod2,DPeriod2,Slowing2,MAMethod2,PriceField2,0,y2);
   ExtMapBuffer2b[i2]=iStochastic(NULL,TimeFrame2,KPeriod2,DPeriod2,Slowing2,MAMethod2,PriceField2,1,y2);
   ObjectDelete("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0));   

   if (ExtMapBuffer1b[i2] < LevelDown2 && ExtMapBuffer2b[i2] < LevelDown2 && NormalizeDouble(ExtMapBuffer1b[i2],Digits)>NormalizeDouble(ExtMapBuffer2b[i2],Digits)
    && NormalizeDouble(ExtMapBuffer1b[i2+1],Digits)<=NormalizeDouble(ExtMapBuffer2b[i2+1],Digits)  && 
       NormalizeDouble(ExtMapBuffer1b[i2+1],Digits)!=NormalizeDouble(ExtMapBuffer1b[i2],Digits))
   {
      ExtMapBuffer3b[i2]=ExtMapBuffer1b[i2];
      ObjectDelete("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0));
      ObjectCreate("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0),22,0,Time[i2],Low[i2]-5*Point);
      ObjectSet("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0),6,clrPaleGreen);
      ObjectSet("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0),14,233);
      ObjectSet("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0),OBJPROP_WIDTH,chartArrowSize2);       
   }
   if (ExtMapBuffer1b[i2] > LevelUp2 && ExtMapBuffer2b[i2] > LevelUp2  &&  NormalizeDouble(ExtMapBuffer1b[i2],Digits)<NormalizeDouble(ExtMapBuffer2b[i2],Digits) 
      && NormalizeDouble(ExtMapBuffer1b[i2+1],Digits)>=NormalizeDouble(ExtMapBuffer2b[i2+1],Digits)  && 
      NormalizeDouble(ExtMapBuffer1b[i2+1],Digits)!=NormalizeDouble(ExtMapBuffer1b[i2],Digits))
   {
      ExtMapBuffer4b[i2]=ExtMapBuffer1b[i2];
      ObjectDelete("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0));
      ObjectCreate("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0),22,0,Time[i2],High[i2]+5*Point);
      ObjectSet("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0),6,clrCrimson);
      ObjectSet("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0),14,234);
      ObjectSet("st2"+Symbol()+"per"+Period()+"KDSsum"+DoubleToStr(KPeriod2+DPeriod2+Slowing2,0)+DoubleToStr(i2,0),OBJPROP_WIDTH,chartArrowSize2);      
   }
     
   if (ExtMapBuffer1b[i2] > LevelDown2 && ExtMapBuffer2b[i2] > LevelDown2 && NormalizeDouble(ExtMapBuffer1b[i2],Digits)>NormalizeDouble(ExtMapBuffer2b[i2],Digits)
    && NormalizeDouble(ExtMapBuffer1b[i2+1],Digits)<=NormalizeDouble(ExtMapBuffer2b[i2+1],Digits)  && 
       NormalizeDouble(ExtMapBuffer1b[i2+1],Digits)!=NormalizeDouble(ExtMapBuffer1b[i2],Digits))  
     { ExtMapBuffer5b[i2]=ExtMapBuffer1b[i2];}
 
   if (ExtMapBuffer1b[i2] < LevelUp2 && ExtMapBuffer2b[i2] < LevelUp2  && NormalizeDouble(ExtMapBuffer1b[i2],Digits)<NormalizeDouble(ExtMapBuffer2b[i2],Digits) 
      && NormalizeDouble(ExtMapBuffer1b[i2+1],Digits)>=NormalizeDouble(ExtMapBuffer2b[i2+1],Digits)  && 
      NormalizeDouble(ExtMapBuffer1b[i2+1],Digits)!=NormalizeDouble(ExtMapBuffer1b[i2],Digits))
   {ExtMapBuffer6b[i2]=ExtMapBuffer1b[i2];}
     
     
     
     
  }
   //----  Refresh buffers ++++++++++++++ 
   if (TimeFrame2 < Period()) TimeFrame2 = Period();
   if (TimeFrame2>Period()) {
     int PerINT2=TimeFrame2/Period()+1;
     datetime TimeArr2[]; ArrayResize(TimeArr2,PerINT2);
     ArrayCopySeries(TimeArr2,MODE_TIME,Symbol(),Period()); 
     for(i2=0;i2<PerINT2+1;i2++) {if (TimeArr2[i2]>=TimeArray2[0]) {
 /********************************************************     
    Refresh buffers:         buffer[i] = buffer[0];
 ************************************************************/  

   ExtMapBuffer1b[i2]=ExtMapBuffer1b[0];
   ExtMapBuffer2b[i2]=ExtMapBuffer2b[0];

   } } }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++   Raff 

   if (ExtMapBuffer3b[0]!=EMPTY_VALUE && !up2_a){
      up2_a=true;
      dn2_a=false;    
     // Alert(TimeFrame + "min MTF Stoch Long cross Signal on "+Symbol());
   }
   if (ExtMapBuffer4b[0]!=EMPTY_VALUE && !dn2_a){
      dn2_a=true;
      up2_a=false;    
     // Alert(TimeFrame + "min MTF Stoch Short cross Signal on "+Symbol());
   }
   ALERT();
   return(0);
  }
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH//
void ALERT() {
   int sig2;
   if (AlertsOn2) {
      if (AlertsOnCurrent2) sig2 = 0;
      else sig2 = 1;
      if (ExtMapBuffer3b[sig2] != EMPTY_VALUE && ExtMapBuffer3b[sig2+1] == EMPTY_VALUE) Alerts(sig2, "Long cross");
      if (ExtMapBuffer4b[sig2] != EMPTY_VALUE && ExtMapBuffer4b[sig2+1] == EMPTY_VALUE) Alerts(sig2, "Short cross");
   }
}
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH//
void Alerts(int x2, string z2) {
   string Strconcat2;
   if (notice2 != z2 || TimeNow2 != iTime(NULL,0,x2)) {
      notice2 = z2; TimeNow2 = iTime(NULL,0,x2);
      Strconcat2 = StringConcatenate(" ","", z2 ,":"," ",Symbol()," ","on"," ", 
      TF_String2()," ","(",TimeToStr(TimeLocal(), TIME_SECONDS),")");
      if (AlertsMessage2) Alert(Strconcat2);
      if (AlertsNotify2) SendNotification(Strconcat2);
      if (AlertsEmail2) SendMail(StringConcatenate(Symbol(), " Signal "), Strconcat2);
      if (AlertsSound2) PlaySound(InpSoundFile2);
   }
}
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH//
//-----------------------------------------------------------------------------\
// Function: TF String()                                                      ||
//-----------------------------------------------------------------------------/
string TF_String2() {
  switch(Period()) {
    case PERIOD_M1: return("M1");
    case PERIOD_M5: return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1");
    case PERIOD_H4: return("H4");
    case PERIOD_D1: return("D1");
    case PERIOD_W1: return("W1");
    case PERIOD_MN1: return("MN1");
   }
  return((string)0);
}
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH//
