#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  clrChartreuse
#property indicator_color2  clrRed
#property indicator_color3  clrRed
#property strict

//
//
//

input int                KPeriod                = 15;                // Stochastic K period
input int                DPeriod                = 3;                 // Stochastic D period
input int                Slowing                = 5;                 // Stochastic slowing
input ENUM_MA_METHOD     MAMethod               = MODE_SMA;          // Stochastic ma method
input ENUM_STO_PRICE     PriceField             = STO_CLOSECLOSE;    // Stochastic price   
input int                LSMAPeriod             = 14;                // Smoothing period
input int                LinesWidth             = 3;     

double val[],vala[],valb[],sto[],valc[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int OnInit()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0,val, INDICATOR_DATA); SetIndexStyle(0,DRAW_LINE,EMPTY,LinesWidth);
   SetIndexBuffer(1,vala,INDICATOR_DATA); SetIndexStyle(1,DRAW_LINE,EMPTY,LinesWidth);
   SetIndexBuffer(2,valb,INDICATOR_DATA); SetIndexStyle(2,DRAW_LINE,EMPTY,LinesWidth);
   SetIndexBuffer(3,sto);
   SetIndexBuffer(4,valc);
   
   IndicatorSetString(INDICATOR_SHORTNAME, " lsma stochastic");
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int i,limit=fmin(rates_total-prev_calculated+1,rates_total-1);
   
   //
   //
   //
   
   if (valc[limit]==-1) CleanPoint(limit,rates_total,vala,valb);
   for(i=limit; i>=0; i--) sto[i] = iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MAMethod,PriceField,MODE_MAIN,i);
   for(i=limit; i>=0; i--)
   {
      val[i] = 3.0*iMAOnArray(sto,0,LSMAPeriod,0,MODE_LWMA,i)-
               2.0*iMAOnArray(sto,0,LSMAPeriod,0,MODE_SMA ,i);
      valc[i] = (i<rates_total-1) ? (val[i]>val[i+1]) ? 1 : (val[i]<val[i+1]) ? -1 : valc[i+1] : 0;
      vala[i] = valb[i] = EMPTY_VALUE;  if (valc[i] == -1) PlotPoint(i,rates_total,vala,valb,val);      
   }
return(rates_total);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------

void CleanPoint(int i, int bars, double& first[], double& second[])
{
   if (i>=bars-3) return;
   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, int bars, double& first[], double& second[], double& from[])
{
   if (i>=bars-2) return;
   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; }
}
