//------------------------------------------------------------------
#property link      "https://howtofxmarkets.web.app/"
#property copyright "https://howtofxmarkets.web.app/ StochAndBands by csj179t"
//   - Forex education, mentorship, signals, trading rooms.
//   - Low cost and high leverage, regulated broker. With a lot of FREE additional services.
//   - And a Guide-o-FX is a list of FREE useful forex resources.
//------------------------------------------------------------------
#property copyright "www.forex-station.com"
// Many thanks to forex-station.com, to mrtools, mladen
// and other coder helping for free. Special thanks to Jimmy! 
// I got this code template at forex-station:  bars iterator, mtf feature...
// And making my indicators, by editing modifying it.

#property indicator_separate_window
#property indicator_buffers    5
#property indicator_color1     clrGreen
#property indicator_width1     2
#property indicator_color2     clrRed
#property indicator_width2     2
#property indicator_color3    clrWhite
#property indicator_width3     2
#property indicator_color4     clrWhite
#property indicator_width4    2
#property indicator_color5    clrWhite
#property indicator_width5    2


#property indicator_level1  50
#property indicator_level2  80
#property indicator_level3 20
#property indicator_levelcolor clrWhite
#property indicator_levelstyle STYLE_DOT
#property indicator_levelwidth 1

#property indicator_minimum -25
#property indicator_maximum 125


extern ENUM_TIMEFRAMES           TimeFrame       = PERIOD_CURRENT;
extern int k_period=5;
extern int d_period= 3;
extern int slowing =3;
extern int stoch_mode = 0;
extern ENUM_STO_PRICE stoch_price = STO_LOWHIGH;
extern int band_period = 20;
extern int band_deviation = 2;

extern bool               Interpolate   = true;

extern string             name = "StochAndBands";


double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
double buffer6[];

string indicatorFileName;
bool   returnBars;

string name2 = name + " " + TimeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   
 
   
   
   IndicatorBuffers(6);
   SetIndexBuffer(0,buffer1);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,buffer2);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(2,buffer3);
    SetIndexStyle(2,DRAW_LINE);
     SetIndexBuffer(3,buffer4);
    SetIndexStyle(3,DRAW_LINE);
     SetIndexBuffer(4,buffer5);
    SetIndexStyle(4,DRAW_LINE);
    


   indicatorFileName = WindowExpertName();
   returnBars        = TimeFrame==-99;
   TimeFrame         = fmax(TimeFrame,_Period);
  
   IndicatorShortName(name2);

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {

 

   return(0);

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
  
  

   
   int counted_bars=IndicatorCounted();
   int i,limit;


   if(counted_bars < 0)
      return(-1);
   if(counted_bars > 0)
      counted_bars--;
   limit = MathMin(Bars-counted_bars,Bars-1);
   if(returnBars)
     {
      buffer1[0] = limit+1;
      return(0);
     }
 

   if(TimeFrame == Period())
     {

      for(i=limit; i>=0; i--)
        {
        
    

   
            buffer1[i] =  iStochastic(NULL,0,k_period,d_period,slowing,stoch_mode,stoch_price,MODE_MAIN,i);

            buffer2[i] =   iStochastic(NULL,0,k_period,d_period,slowing,stoch_mode,stoch_price,MODE_SIGNAL,i);
            
            buffer3[i] =   iBandsOnArray(buffer1,0,band_period,band_deviation,0,MODE_MAIN,i);
            buffer4[i] =   iBandsOnArray(buffer1,0,band_period,band_deviation,0,MODE_UPPER,i);
            buffer5[i] =   iBandsOnArray(buffer1,0,band_period,band_deviation,0,MODE_LOWER,i);


        }






      return(0);
     }



   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   for(i=limit; i>=0; i--)
     {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
      
      buffer1[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,k_period,d_period,slowing,stoch_mode,stoch_price,band_period,band_deviation,0,y);
      buffer2[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,k_period,d_period,slowing,stoch_mode,stoch_price,band_period,band_deviation,1,y);
      buffer3[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,k_period,d_period,slowing,stoch_mode,stoch_price,band_period,band_deviation,2,y);
      buffer4[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,k_period,d_period,slowing,stoch_mode,stoch_price,band_period,band_deviation,3,y);
      buffer5[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,k_period,d_period,slowing,stoch_mode,stoch_price,band_period,band_deviation,4,y);
         
      //
      //
      //
      //
      //

      if(!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1]))
         continue;

      //
      //
      //
      //
      //

      datetime time = iTime(NULL,TimeFrame,y);
      for(int n = 1; i+n < Bars && Time[i+n] >= time; n++)
         continue;
      for(int x = 1; x < n; x++)
        {

            buffer1[i+x] = buffer1[i] + (buffer1[i+n] - buffer1[i]) * x/n;    
            buffer2[i+x] = buffer2[i] + (buffer2[i+n] - buffer2[i]) * x/n;
            buffer3[i+x] = buffer3[i] + (buffer3[i+n] - buffer3[i]) * x/n;
            buffer4[i+x] = buffer4[i] + (buffer4[i+n] - buffer4[i]) * x/n;
            buffer5[i+x] = buffer5[i] + (buffer5[i+n] - buffer5[i]) * x/n;
            

        }
     }







   return(0);
  }

