//+------------------------------------------------------------------+
//|                                                          CMA.mq4 |
//|                                                             Raff |
//|                                          just_raff1410@yahoo.com |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Yellow
#property indicator_color2 Aqua
#property indicator_color3 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 3

extern int Ma_Period = 2;
extern int Ma_PeriodStd = 5;
extern int Ma_Method = 3;
extern int Ma_Price = 4;
extern int Count_bars = 1500;

double K;
double v1;
double v2;
//---- buffers
double CA[];
double DIR[];
double UpBufferTop[];
double UpBufferBottom[];
double DnBufferTop[];
double DnBufferBottom[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   diff = 10 * Point;
//---- indicators
   IndicatorBuffers(6);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(4,CA);
   SetIndexBuffer(0,UpBufferTop);
   SetIndexBuffer(1,UpBufferBottom);
   SetIndexBuffer(5,DIR);
   SetIndexBuffer(2,DnBufferTop);
   SetIndexBuffer(3,DnBufferBottom);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
double diff;
int start()
  {   
   if (Count_bars+1>Bars-2)  Count_bars=Bars-2;
   CA[Count_bars+1]=Close[Count_bars+1];
   for(int i=Count_bars;i>=0;i--)
   {
    v1=MathSqrt(iStdDev(NULL,0,Ma_PeriodStd,0,Ma_Method,Ma_Price,i));
    v2=MathSqrt(MathAbs(CA[i+1]-iMA(NULL,0,Ma_Period,0,Ma_Method,Ma_Price,i)));
    if (v2<v1) {K=0;}
    else {K=1-(v1/v2);}
    CA[i]=CA[i+1]+K*(iMA(NULL,0,Ma_Period,0,Ma_Method,Ma_Price,i)-CA[i+1]);

     DIR[i]=DIR[i+1];
     if (CA[i]-CA[i+1] > 0) DIR[i]= 1; 
     if (CA[i+1]-CA[i] > 0) DIR[i]=-1; 
        if (DIR[i]>0)
        {  
        UpBufferTop[i] = CA[i]+diff;
        UpBufferBottom[i] = CA[i]-diff;
        //if (DIR[i+1]<0) UpBuffer[i+1]=CA[i+1];
        DnBufferTop[i] = EMPTY_VALUE;
        DnBufferBottom[i] = EMPTY_VALUE;
        }
        else if (DIR[i]<0) 
        {
        DnBufferTop[i] = CA[i]+diff;
        DnBufferBottom[i] =  CA[i] -diff;
        //if (DIR[i+1]>0) DnBuffer[i+1]=CA[i+1];
        UpBufferTop[i] = EMPTY_VALUE;
        UpBufferBottom[i] = EMPTY_VALUE;
        }
        
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+