//+------------------------------------------------------------------+
//|                              KH_MTF_Stochastic_Alert_Average.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
 
#property indicator_separate_window
#property indicator_buffers 40
#property indicator_color39 clrLime
#property indicator_color40 clrWhite 


#property indicator_level1 20
#property indicator_level2 80



enum vline_placement
{
   line_in_chart_window,
   line_in_separate_window
};

enum tf_option
{
   TF1,
   TF2,
   BOTH
};


extern ENUM_TIMEFRAMES TimeFrame=PERIOD_CURRENT;
extern int Stoch_K_Period_01=6;
extern int Stoch_K_Period_02=7;
extern int Stoch_K_Period_03=8;
extern int Stoch_K_Period_04=9;
extern int Stoch_K_Period_05=10;
extern int Stoch_K_Period_06=11; 
extern int Stoch_K_Period_07=12;
extern int Stoch_K_Period_08=13;
extern int Stoch_K_Period_09=14;
extern int Stoch_K_Period_10=15;
extern int Stoch_K_Period_11=16;
extern int Stoch_K_Period_12=17;
extern int Stoch_K_Period_13=18;
extern int Stoch_K_Period_14=19;
extern int Stoch_K_Period_15=20;
extern int Stoch_K_Period_16=21;
extern int Stoch_K_Period_17=22;
extern int Stoch_K_Period_18=23;
extern int Stoch_K_Period_19=24;

extern int Stoch_D_Period=12;
extern int Stoch_Slowing=3;
extern ENUM_MA_METHOD Stoch_MA_Method=MODE_EMA;
extern ENUM_STO_PRICE Stoch_Price_Field=STO_LOWHIGH;

extern double Stoch_OverBought=80;        //Stoch OverBought Line
extern double Stoch_OverSold=20;          //Stoch OverSold Line



int gap_v=18;

double stochmain01[];
double stochmain02[];
double stochmain03[];
double stochmain04[];
double stochmain05[];
double stochmain06[];
double stochmain07[];
double stochmain08[];
double stochmain09[];
double stochmain10[];
double stochmain11[];
double stochmain12[];
double stochmain13[];
double stochmain14[];
double stochmain15[];
double stochmain16[];
double stochmain17[];
double stochmain18[];
double stochmain19[];

double stochsignal01[];
double stochsignal02[];
double stochsignal03[];
double stochsignal04[];
double stochsignal05[];
double stochsignal06[];
double stochsignal07[];
double stochsignal08[];
double stochsignal09[];
double stochsignal10[];
double stochsignal11[];
double stochsignal12[];
double stochsignal13[];
double stochsignal14[];
double stochsignal15[];
double stochsignal16[];
double stochsignal17[];
double stochsignal18[];
double stochsignal19[];


double stochAVK[];
double stochAVD[];



double     myPoint;
int        myDigits;
int myBars;
int myBars2;
int myBars3;
int stochKPeriod[19];
string indi_code="KH_MTF_Stochastic_30";

double ratio=1000000;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
   myPoint     =  MarketInfo(Symbol(),MODE_POINT);
   myDigits    =  MarketInfo(Symbol(),MODE_DIGITS);
   if(myDigits==3 || myDigits==5)
   {
      myPoint     =  myPoint  *  10;
      myDigits    =  myDigits -1;
   }    
  

//---- indicators

   for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);
   IndicatorBuffers(40);
   
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,Stoch_OverBought);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,Stoch_OverSold);
   
   SetIndexBuffer(0, stochmain01);
   SetIndexBuffer(1, stochmain02);
   SetIndexBuffer(2, stochmain03);
   SetIndexBuffer(3, stochmain04);
   SetIndexBuffer(4, stochmain05);
   SetIndexBuffer(5, stochmain06);
   SetIndexBuffer(6, stochmain07);
   SetIndexBuffer(7, stochmain08);
   SetIndexBuffer(8, stochmain09);
   SetIndexBuffer(9, stochmain10);
   SetIndexBuffer(10, stochmain11);
   SetIndexBuffer(11, stochmain12);
   SetIndexBuffer(12, stochmain13);
   SetIndexBuffer(13, stochmain14);
   SetIndexBuffer(14, stochmain15);
   SetIndexBuffer(15, stochmain16);
   SetIndexBuffer(16, stochmain17);
   SetIndexBuffer(17, stochmain18);
   SetIndexBuffer(18, stochmain19);

   SetIndexBuffer(19, stochsignal01);
   SetIndexBuffer(20, stochsignal02);
   SetIndexBuffer(21, stochsignal03);
   SetIndexBuffer(22, stochsignal04);
   SetIndexBuffer(23, stochsignal05);
   SetIndexBuffer(24, stochsignal06);
   SetIndexBuffer(25, stochsignal07);
   SetIndexBuffer(26, stochsignal08);
   SetIndexBuffer(27, stochsignal09);
   SetIndexBuffer(28, stochsignal10);
   SetIndexBuffer(29, stochsignal11);
   SetIndexBuffer(30, stochsignal12);
   SetIndexBuffer(31, stochsignal13);
   SetIndexBuffer(32, stochsignal14);
   SetIndexBuffer(33, stochsignal15);
   SetIndexBuffer(34, stochsignal16);
   SetIndexBuffer(35, stochsignal17);
   SetIndexBuffer(36, stochsignal18);
   SetIndexBuffer(37, stochsignal19);

   SetIndexBuffer(38, stochAVK);
   SetIndexBuffer(39, stochAVD);


//---- drawing settings
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_NONE);
   SetIndexStyle(2,DRAW_NONE);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexStyle(4,DRAW_NONE);
   SetIndexStyle(5,DRAW_NONE);
   SetIndexStyle(6,DRAW_NONE);
   SetIndexStyle(7,DRAW_NONE);
   SetIndexStyle(8,DRAW_NONE);
   SetIndexStyle(9,DRAW_NONE);
   SetIndexStyle(10,DRAW_NONE);
   SetIndexStyle(11,DRAW_NONE);
   SetIndexStyle(12,DRAW_NONE);
   SetIndexStyle(13,DRAW_NONE);
   SetIndexStyle(14,DRAW_NONE);
   SetIndexStyle(15,DRAW_NONE);
   SetIndexStyle(16,DRAW_NONE);
   SetIndexStyle(17,DRAW_NONE);
   SetIndexStyle(18,DRAW_NONE);
   
   SetIndexStyle(19,DRAW_NONE);
   SetIndexStyle(20,DRAW_NONE);
   SetIndexStyle(21,DRAW_NONE);
   SetIndexStyle(22,DRAW_NONE);
   SetIndexStyle(23,DRAW_NONE);
   SetIndexStyle(24,DRAW_NONE);
   SetIndexStyle(25,DRAW_NONE);
   SetIndexStyle(26,DRAW_NONE);
   SetIndexStyle(27,DRAW_NONE);
   SetIndexStyle(28,DRAW_NONE);
   SetIndexStyle(29,DRAW_NONE);
   SetIndexStyle(30,DRAW_NONE);
   SetIndexStyle(31,DRAW_NONE);
   SetIndexStyle(32,DRAW_NONE);
   SetIndexStyle(33,DRAW_NONE);
   SetIndexStyle(34,DRAW_NONE);
   SetIndexStyle(35,DRAW_NONE);
   SetIndexStyle(36,DRAW_NONE);
   SetIndexStyle(37,DRAW_NONE);
   
   SetIndexStyle(38,DRAW_LINE,STYLE_SOLID); //STYLE_SOLID,2
   SetIndexStyle(39,DRAW_LINE,STYLE_SOLID);   //STYLE_DOT
   
   SetIndexLabel(0,"");
   SetIndexLabel(1,"");
   SetIndexLabel(2,"");
   SetIndexLabel(3,"");
   SetIndexLabel(4,"");
   SetIndexLabel(5,"");
   SetIndexLabel(6,"");
   SetIndexLabel(7,"");
   SetIndexLabel(8,"");
   SetIndexLabel(9,"");
   SetIndexLabel(10,"");
   SetIndexLabel(11,"");
   SetIndexLabel(12,"");
   SetIndexLabel(13,"");
   SetIndexLabel(14,"");
   SetIndexLabel(15,"");
   SetIndexLabel(16,"");
   SetIndexLabel(17,"");
   SetIndexLabel(18,"");
   SetIndexLabel(19,"");
   SetIndexLabel(20,"");
   SetIndexLabel(21,"");
   SetIndexLabel(22,"");
   SetIndexLabel(23,"");
   SetIndexLabel(24,"");
   SetIndexLabel(25,"");
   SetIndexLabel(26,"");
   SetIndexLabel(27,"");
   SetIndexLabel(28,"");
   SetIndexLabel(29,"");
   SetIndexLabel(30,"");
   SetIndexLabel(31,"");
   SetIndexLabel(32,"");
   SetIndexLabel(33,"");
   SetIndexLabel(34,"");
   SetIndexLabel(35,"");
   SetIndexLabel(36,"");
   SetIndexLabel(37,"");

   SetIndexLabel(38, "Stoch Avg K");
   SetIndexLabel(39, "Stoch Avg D");
   


   stochKPeriod[0]=Stoch_K_Period_01;
   stochKPeriod[1]=Stoch_K_Period_02;
   stochKPeriod[2]=Stoch_K_Period_03;
   stochKPeriod[3]=Stoch_K_Period_04;
   stochKPeriod[4]=Stoch_K_Period_05;
   stochKPeriod[5]=Stoch_K_Period_06;
   stochKPeriod[6]=Stoch_K_Period_07;
   stochKPeriod[7]=Stoch_K_Period_08;
   stochKPeriod[8]=Stoch_K_Period_09;
   stochKPeriod[9]=Stoch_K_Period_10;
   stochKPeriod[10]=Stoch_K_Period_11;
   stochKPeriod[11]=Stoch_K_Period_12;
   stochKPeriod[12]=Stoch_K_Period_13;
   stochKPeriod[13]=Stoch_K_Period_14;
   stochKPeriod[14]=Stoch_K_Period_15;
   stochKPeriod[15]=Stoch_K_Period_16;
   stochKPeriod[16]=Stoch_K_Period_17;
   stochKPeriod[17]=Stoch_K_Period_18;
   stochKPeriod[18]=Stoch_K_Period_19;



   IndicatorDigits(Digits);
   TimeFrame=MathMax(TimeFrame,_Period);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  
   int    counted_bars=IndicatorCounted();
   
//----   
   if(counted_bars>0) counted_bars--;
   int limit=MathMin(MathMax(Bars-counted_bars,TimeFrame/_Period),Bars-1);

   for(int i=limit-1; i>=0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
      stochmain01[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_01,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain02[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_02,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain03[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_03,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain04[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_04,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain05[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_05,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain06[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_06,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain07[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_07,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain08[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_08,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain09[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_09,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain10[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_10,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain11[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_11,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain12[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_12,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain13[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_13,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain14[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_14,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain15[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_15,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain16[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_16,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain17[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_17,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain18[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_18,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);
      stochmain19[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_19,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_MAIN,y);

      stochsignal01[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_01,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal02[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_02,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal03[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_03,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal04[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_04,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal05[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_05,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal06[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_06,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal07[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_07,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal08[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_08,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal09[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_09,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal10[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_10,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal11[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_11,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal12[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_12,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal13[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_13,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal14[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_14,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal15[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_15,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal16[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_16,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal17[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_17,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal18[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_18,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);
      stochsignal19[i]=iStochastic(Symbol(),TimeFrame,Stoch_K_Period_19,Stoch_D_Period,Stoch_Slowing,Stoch_MA_Method,Stoch_Price_Field,MODE_SIGNAL,y);      
   
      stochAVK[i]=(stochmain01[i]+stochmain02[i]+stochmain03[i]+stochmain04[i]+stochmain05[i]+stochmain06[i]+stochmain07[i]+stochmain08[i]+stochmain09[i]+stochmain10[i]
                    +stochmain11[i]+stochmain12[i]+stochmain13[i]+stochmain14[i]+stochmain15[i]+stochmain16[i]+stochmain17[i]+stochmain18[i]+stochmain19[i])/19;
      stochAVD[i]=(stochsignal01[i]+stochsignal02[i]+stochsignal03[i]+stochsignal04[i]+stochsignal05[i]+stochsignal06[i]+stochsignal07[i]+stochsignal08[i]+stochsignal09[i]+stochsignal10[i]
                    +stochsignal11[i]+stochsignal12[i]+stochsignal13[i]+stochsignal14[i]+stochsignal15[i]+stochsignal16[i]+stochsignal17[i]+stochsignal18[i]+stochsignal19[i])/19;
      
   }
   

    
   //----
   return(0);
  }
//+------------------------------------------------------------------+



