//+----------------------------------------------------------+
//|                             Ehlers center of gravity.mq4 |
//|                                                          |
//+----------------------------------------------------------+
#property  copyright "www.forex-tsd.com"
#property  link      "www.forex-tsd.com"

#property  indicator_separate_window
#property  indicator_buffers 5
#property  indicator_color1  PaleVioletRed
#property  indicator_color2  DeepSkyBlue
#property  indicator_color3  DimGray
#property  indicator_color4  PaleVioletRed
#property  indicator_color5  YellowGreen
#property  indicator_width4  2
#property  indicator_style3  STYLE_DOT
#property  indicator_style5  STYLE_DOT

//
//
//
//
//

#import "dynamicZone.dll"
   double dzBuyP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i,double precision);
   double dzSellP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i,double precision);
#import

//
//
//
//
//
 
extern int    period                 = 10;
extern int    PriceType              = PRICE_MEDIAN;
extern bool   showSignalLine         = true;
extern int    DzLookBackBars         = 70;
extern double DzStartBuyProbability  = 0.05;
extern double DzStartSellProbability = 0.05;

//
//
//
//
//

double buffer1[];
double buffer2[];
double Prices[];
double sli[];
double bli[];
double zli[];

   
//+----------------------------------------------------------+
//|                                                          |
//+----------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(6);
      SetIndexBuffer(0,bli);
      SetIndexBuffer(1,sli);
      SetIndexBuffer(2,zli);
      SetIndexBuffer(3,buffer1);
      SetIndexBuffer(4,buffer2);
      SetIndexBuffer(5,Prices);
   IndicatorShortName("Ehlers\' Center of gravity ("+period+")");
   return(0);
}


//+----------------------------------------------------------+
//|                                                          |
//+----------------------------------------------------------+
//
//
//
//
//

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

   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
           limit = Bars-counted_bars;

   //
   //
   //
   //
   //
         
   for(i=limit; i>=0; i--)
   {  
      Prices[i] = iMA(NULL,0,1,0,MODE_SMA,PriceType,i);
      
      //
      //
      //
      //
      //

         double num   = 0;
         double denom = 0;
            for (int k=0; k<period; k++)
            {
               num   += Prices[i+k]*(k+1);
               denom += Prices[i+k];
            }               

      // 
      //
      //
      //
      //

      if (denom !=0)
            buffer1[i] = -num/denom;
      else  buffer1[i] = 0;       
      if (showSignalLine)
            buffer2[i] = buffer1[i+1];
            bli[i]     = dzBuyP (buffer1, DzStartBuyProbability,  DzLookBackBars, Bars, i,0.0001);
            sli[i]     = dzSellP(buffer1, DzStartSellProbability, DzLookBackBars, Bars, i,0.0001);
            zli[i]     = dzSellP(buffer1, 0.5,                    DzLookBackBars, Bars, i,0.0001);
   }
   return(0);
}