//+------------------------------------------------------------------+
//|                                        TriangularMA centered.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1  DeepSkyBlue
#property indicator_color2  LimeGreen
#property indicator_color3  LimeGreen
#property indicator_color4  Red
#property indicator_color5  Red
#property indicator_color6  DimGray
#property indicator_style3  STYLE_DOT
#property indicator_style4  STYLE_DOT
#property indicator_style6  STYLE_DOT
#property indicator_width1  2

//
//
//
//
//

#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    HalfLength              = 12;
extern int    Price                   = PRICE_CLOSE;
extern bool   ShowMiddleLine          = true;
extern int    DzLookBackBars          = 50;
extern double DzStartBuyProbability1  = 0.10;
extern double DzStartBuyProbability2  = 0.25;
extern double DzStartSellProbability1 = 0.10;
extern double DzStartSellProbability2 = 0.25;

//
//
//
//
//

double buffer1[];
double bl1Buffer[];
double bl2Buffer[];
double sl1Buffer[];
double sl2Buffer[];
double zliBuffer[];

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

int init()
{
   HalfLength=MathMax(HalfLength,1);

   //
   //
   //
   //
   //
   
   SetIndexBuffer(0,buffer1);   SetIndexDrawBegin(0,HalfLength);
   SetIndexBuffer(1,bl1Buffer); SetIndexDrawBegin(1,HalfLength);
   SetIndexBuffer(2,bl2Buffer); SetIndexDrawBegin(2,HalfLength);
   SetIndexBuffer(3,sl2Buffer); SetIndexDrawBegin(3,HalfLength);
   SetIndexBuffer(4,sl1Buffer); SetIndexDrawBegin(4,HalfLength);
   SetIndexBuffer(5,zliBuffer); SetIndexDrawBegin(5,HalfLength);
   return(0);
}
int deinit() { return(0); }




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

int start()
{
   double precision = Point*100.0;
   int counted_bars=IndicatorCounted();
   int i,j,k,limit;

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

   //
   //
   //
   //
   //

   for (i=limit; i>=0; i--)
   {
      double sum  = (HalfLength+1)*iMA(NULL,0,1,0,MODE_SMA,Price,i);
      double sumw = (HalfLength+1);
      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;
         }
      }
      buffer1[i] = sum/sumw;
      if (DzStartBuyProbability1 >0) bl1Buffer[i] = dzBuyP (buffer1, DzStartBuyProbability1,  DzLookBackBars, Bars, i, precision);
      if (DzStartBuyProbability2 >0) bl2Buffer[i] = dzBuyP (buffer1, DzStartBuyProbability2,  DzLookBackBars, Bars, i, precision);
      if (DzStartSellProbability1>0) sl1Buffer[i] = dzSellP(buffer1, DzStartSellProbability1, DzLookBackBars, Bars, i, precision);
      if (DzStartSellProbability2>0) sl2Buffer[i] = dzSellP(buffer1, DzStartSellProbability2, DzLookBackBars, Bars, i, precision);
      if (ShowMiddleLine)            zliBuffer[i] = dzSellP(buffer1, 0.5                    , DzLookBackBars, Bars, i, precision);
  }
   
   //
   //
   //
   //
   //
   
   return(0);
}