//------------------------------------------------------------------
//
//    original idea and first version 
//          made by pip (http://www.pricemasterpro.com)
//    this version made by mladen
//
//------------------------------------------------------------------
#property copyright "2011(c) - PriceMasterPro.Com"
#property copyright "2011(c) - forex-tsd.Com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  PaleVioletRed
#property indicator_color2  DeepSkyBlue
#property indicator_color3  DeepSkyBlue
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2

//
//
//
//
//

extern int Period1 = 46;
extern int Period2 = 23;
extern int Price   = PRICE_CLOSE;

double dce[];
double dceUa[];
double dceUb[];
double slope[];

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

int init()
{
   IndicatorBuffers(4);
      SetIndexBuffer(0,dce);
      SetIndexBuffer(1,dceUa);
      SetIndexBuffer(2,dceUb);
      SetIndexBuffer(3,slope);
      IndicatorShortName("DCE"+Period1+","+Period2);
   return(0);
}

//
//
//
//
//
//

int start()
{
   int limit,counted_bars=IndicatorCounted();

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-1,Bars-counted_bars+MathMax(Period1,Period2));


   //
   //
   //
   //
   //
   
   if (slope[limit]==1) CleanPoint(limit,dceUa,dceUb);
   for(int i=limit; i>=0; i--)
   {
      dce[i]   = iTma(Price,Period2,i)-iTma(Price,Period1,i);
      dceUa[i] = EMPTY_VALUE;
      dceUb[i] = EMPTY_VALUE;
      slope[i] = slope[i+1];
         if (dce[i] < dce[i+1]) slope[i] = -1;
         if (dce[i] > dce[i+1]) slope[i] =  1;
         if (slope[i]==1) PlotPoint(i,dceUa,dceUb,dce);
   }   
   return(0);
}




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

double iTma(int price, int halfLength, int i, int instanceNo=0)
{
   double sum  = (halfLength+1)*iMA(NULL,0,1,0,MODE_SMA,price,i);
   double sumw = (halfLength+1);
   int j,k;
      for(j=1, k=halfLength; j<=halfLength; j++, k--)
         {
            sum  += k*iMA(NULL,0,1,0,MODE_SMA,price,i+j);
            sumw += k;
            if (j<=i)
            {
               sum  += k*iMA(NULL,0,1,0,MODE_SMA,price,i-j);
               sumw += k;
            }
         }
      return(sum/sumw);
}

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

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

