//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  LimeGreen
#property indicator_color2  PaleVioletRed
#property indicator_color3  PaleVioletRed
#property indicator_color4  Silver
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2

//
//
//
//
//

extern int Length = 25;
extern int Price  = PRICE_TYPICAL;

double cg[];
double storaw[];
double stocg[];
double stocgda[];
double stocgdb[];
double stosig[];
double trend[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(7);
      SetIndexBuffer(0,stocg);
      SetIndexBuffer(1,stocgda);
      SetIndexBuffer(2,stocgdb);
      SetIndexBuffer(3,stosig);
      SetIndexBuffer(4,cg);
      SetIndexBuffer(5,storaw);
      SetIndexBuffer(6,trend);
   return(0); 
}
int deinit() { return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //

   if (trend[limit]==-1) CleanPoint(limit,stocgda,stocgdb);
   for(int i=limit; i>=0; i--)
   {
      double num = 0;
      double den = 0;
         for (int k=0; k<Length; k++)
         {
            double price = iMA(NULL,0,1,0,MODE_SMA,Price,i+k);
                   num += price*(k+1);
                   den += price;
         }
         if (den!=0)
               cg[i] = -num/den+(Length+1)/2.0;
         else  cg[i] = 0;
         
         //
         //
         //
         //
         //
         
         double hh = cg[ArrayMaximum(cg,Length,i)];          
         double ll = cg[ArrayMinimum(cg,Length,i)];
         if (hh!=ll)
               storaw[i] = (cg[i]-ll)/(hh-ll);
         else  storaw[i] = 0;
         double smtcg = (4.0*storaw[i]+3.0*storaw[i+1]+2.0*storaw[i+2]+storaw[i+3])/10.0;
         
         //
         //
         //
         //
         //
         
         
            stocg[i]   = 0.5*MathLog((1+1.98*(smtcg-0.5))/(1-1.98*(smtcg-0.5)));
            stosig[i]  = stocg[i+1];
            stocgda[i] = EMPTY_VALUE;
            stocgdb[i] = EMPTY_VALUE;
            trend[i]   = trend[i+1];
               if (stocg[i]>stosig[i]) trend[i] =  1;
               if (stocg[i]<stosig[i]) trend[i] = -1;
               if (trend[i]==-1) PlotPoint(i,stocgda,stocgdb,stocg);
   }
   return(0);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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;
      }
}