//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  LimeGreen
#property indicator_color2  PaleVioletRed
#property indicator_color3  PaleVioletRed
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_minimum 0
#property indicator_maximum 100

//
//
//
//
//

extern string TimeFrame          = "Current time frame";
extern int    StochasticPeriod   = 55;
extern int    EmaSmoothingPeriod = 15;
extern double UpLevel            = 80;
extern double DownLevel          = 20;

double sto[];
double stoDa[];
double stoDb[];
double slope[];

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,sto);
   SetIndexBuffer(1,stoDa);
   SetIndexBuffer(2,stoDb);
   SetIndexBuffer(3,slope);
      SetLevelValue(0,UpLevel);
      SetLevelValue(1,DownLevel);
   
      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = TimeFrame=="calculateValue"; if (calculateValue) { return(0); }
         returnBars        = TimeFrame=="returnBars";     if (returnBars)     { return(0); }
         timeFrame         = stringToTimeFrame(TimeFrame);
      
      //
      //
      //
      //
      //

      IndicatorShortName(timeFrameToString(timeFrame)+" Double smoothed stochastic - slope ("+StochasticPeriod+","+EmaSmoothingPeriod+")");
   return(0);
}
int deinit() { return(0); }


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if (counted_bars<0) return(0);
      if (counted_bars>0) counted_bars--;
            int limit=MathMin(Bars-counted_bars,Bars-1);
            if (returnBars) { sto[0] = MathMin(limit+1,Bars-1); return(0); }
 
   //
   //
   //
   //
   //
 
   if (calculateValue || timeFrame == Period())
   {
      if (slope[limit]==-1) CleanPoint(limit,stoDa,stoDb);
      for(int i=limit; i>=0; i--)
      {
         stoDa[i] = EMPTY_VALUE;
         stoDb[i] = EMPTY_VALUE;
         sto[i]   = iDss(StochasticPeriod,EmaSmoothingPeriod,i);
         slope[i] = slope[i+1];
            if (sto[i]>sto[i+1]) slope[i] =  1;
            if (sto[i]<sto[i+1]) slope[i] = -1;
            if (slope[i] == -1) PlotPoint(i,stoDa,stoDb,sto);
      }
      return(0);
   }
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   if (slope[limit]==-1) CleanPoint(limit,stoDa,stoDb);
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         sto[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",StochasticPeriod,EmaSmoothingPeriod,0,y);
         stoDa[i] = EMPTY_VALUE;
         stoDb[i] = EMPTY_VALUE;
         slope[i] = slope[i+1];
            if (sto[i]>sto[i+1]) slope[i] =  1;
            if (sto[i]<sto[i+1]) slope[i] = -1;
            if (slope[i] == -1) PlotPoint(i,stoDa,stoDb,sto);
   }
   return(0);
}


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workDss[][2];
double iDss(int StoPeriod, int EMAPeriod, int i, int instanceNo=0)
{
   if (ArrayRange(workDss,0) != Bars) ArrayResize(workDss,Bars); int r=Bars-i-1; instanceNo*=2;

   //
   //
   //
   //
   //

      double alpha = 2.0/(1.0+EMAPeriod);
      double stoc  = 0;
      double max   = High[i]; 
      double min   = Low[i];
         for(int k=1; k<StoPeriod; k++)
         {
            max = MathMax(max,High[i+k]);
            min = MathMin(min,Low [i+k]);
         }            
         if (max!=min) stoc = (Close[i]-min)/(max-min)*100.00;

         workDss[r][instanceNo] = workDss[r-1][instanceNo]+alpha*(stoc-workDss[r-1][instanceNo]);
      
         //
         //
         //
         //
         //
      
         stoc = 0;
         max  = workDss[r][instanceNo]; 
         min  = workDss[r][instanceNo];
            for(k=1; k<StoPeriod; k++)
            {
               max = MathMax(max,workDss[r-k][instanceNo]);
               min = MathMin(min,workDss[r-k][instanceNo]);
            }
            if (max!=min) stoc = (workDss[r][instanceNo]-min)/(max-min)*100.00;

         workDss[r][instanceNo+1] = workDss[r-1][instanceNo+1]+alpha*(stoc-workDss[r-1][instanceNo+1]);

         //
         //
         //
         //
         //
         
   return(workDss[r][instanceNo+1]);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]  = from[i];
         second[i] = EMPTY_VALUE;
      }
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}