//+------------------------------------------------------------------
//|                        metatrader version and additions by mladen
//+------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1  DimGray
#property indicator_color2  PaleVioletRed
#property indicator_color3  DeepSkyBlue
#property indicator_color4  DeepSkyBlue
#property indicator_width2  2
#property indicator_width3  2
#property indicator_width4  2

//
//
//
//
//

#define PRICE_OHLC 7

extern int  Length         = 10;
extern int  Price          = PRICE_OHLC;
extern int  MaMode         = 11;
extern bool MultiColor     = true;
extern bool AverageVisible = true;

//
//
//
//
//

double corma[];
double cormaUa[];
double cormaUb[];
double trend[];
double prices[];
double average[];

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

int init()
{
   IndicatorBuffers(6);
      SetIndexBuffer(0,average);
      SetIndexBuffer(1,corma);
      SetIndexBuffer(2,cormaUa);
      SetIndexBuffer(3,cormaUb);
      SetIndexBuffer(4,trend);
      SetIndexBuffer(5,prices);
         IndicatorShortName("Corrected "+getAverageName(MaMode)+" )"+Length+")");
   return(0);
}
int deinit() { return(0); }

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

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

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

   //
   //
   //
   //
   //

   if (MultiColor && trend[limit]== 1) CleanPoint(limit,cormaUa,cormaUb);
   for(i=limit; i>=0; i--) 
   {
      prices[i] = getPrice(Price,i); if (i>Bars-Length) { corma[i] = prices[i]; continue; }
      
      //
      //
      //
      //
      //
      
         double avg = iCustomMa(MaMode,prices[i],Length,i,0);
         double dev = iDeviation(prices,Length,avg,i);
         double v1  = MathPow(dev,2);
         double v2  = MathPow(corma[i+1]-avg,2);
         double k   = 0; if (v1<=v2 && v2!=0) k = 1.0-v1/v2;
               corma[i] = corma[i+1]+k*(avg-corma[i+1]);
               if (AverageVisible)
                     average[i] = avg;
   
      //
      //
      //
      //
      //
      
      if (MultiColor)
      {
         cormaUa[i] = EMPTY_VALUE;
         cormaUb[i] = EMPTY_VALUE;
         trend[i]   = trend[i+1];
            if (avg>corma[i]) trend[i]= 1;
            if (avg<corma[i]) trend[i]=-1;
            if (trend[i]==1) PlotPoint(i,cormaUa,cormaUb,corma);
      }
   }      
   return(0);
}

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

double getPrice(int type, int i)
{
   if (type==PRICE_OHLC)
         return((Open[i]+High[i]+Low[i]+Close[i])/4.0);
   else  return(iMA(NULL,0,1,0,MODE_SMA,type,i));
}

//
//
//
//
//

double iDeviation(double& array[], double period, double ma, int i)
{
   double sum = 0.00;
      for(int k=0; k<period; k++) sum += (array[i+k]-ma)*(array[i+k]-ma);
   return(MathSqrt(sum/period));
}

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

string methodNames[] = {"SMA","EMA","SMMA","LWMA","LW parabolic MA","Volume weghted MA","Hull MA","Triangular MA","Sine weighted MA","Liner regression","NonLag MA","Zero lag MA"};
string getAverageName(int& method)
{
   int max = ArraySize(methodNames);
      method=MathMax(MathMin(method,max),0); return(methodNames[method]);
}

//
//
//
//
//


#define _maWorkBufferx1 1
#define _maWorkBufferx2 2
#define _maWorkBufferx3 3

double iCustomMa(int mode, double price, double length, int i, int instanceNo)
{
   int r = Bars-i-1;
   switch (mode)
   {
      case 0  : return(iSma(price,length,r,instanceNo));
      case 1  : return(iEma(price,length,r,instanceNo));
      case 2  : return(iSmma(price,length,r,instanceNo));
      case 3  : return(iLwma(price,length,r,instanceNo));
      case 4  : return(iLwmp(price,length,r,instanceNo));
      case 5  : return(iWwma(price,length,r,instanceNo));
      case 6  : return(iHull(price,length,r,instanceNo));
      case 7  : return(iTma(price,length,r,instanceNo));
      case 8  : return(iSineWMA(price,length,r,instanceNo));
      case 9  : return(iLinr(price,length,r,instanceNo));
      case 10 : return(iNonLagMa(price,length,r,instanceNo));
      case 11 : return(iZeroLag(price,length,r,instanceNo));
      default : return(0);
   }
}

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

double workSma[][_maWorkBufferx1];
double iSma(double price, double period, int r, int instanceNo=0)
{
   if (ArraySize(workSma)!= Bars) ArrayResize(workSma,Bars);

   //
   //
   //
   //
   //
      
   workSma[r][instanceNo] = price;
   double sum = price; 
         for(int k=1; k<period && (r-k)>=0; k++) sum += workSma[r-k][instanceNo];  
   return(sum/k);
}

//
//
//
//
//

double workEma[][_maWorkBufferx1];
double iEma(double price, double period, int r, int instanceNo=0)
{
   if (ArraySize(workEma)!= Bars) ArrayResize(workEma,Bars);

   //
   //
   //
   //
   //
      
   double alpha = 2.0 / (1.0+period);
          workEma[r][instanceNo] = workEma[r-1][instanceNo]+alpha*(price-workEma[r-1][instanceNo]);
   return(workEma[r][instanceNo]);
}

//
//
//
//
//

double workSmma[][_maWorkBufferx2];
#define _smmp 0
#define _smma 1

double iSmma(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workSmma,0)!= Bars) ArrayResize(workSmma,Bars); instanceNo *= 2;

   //
   //
   //
   //
   //
      
   if (r<period)
   {
      workSmma[r][_smmp+instanceNo] = price;
      workSmma[r][_smma+instanceNo] = price; 
         for(int k=1; k<period && (r-k)>=0; k++) workSmma[r][_smma+instanceNo] += workSmma[r-k][_smmp+instanceNo];  
                                                 workSmma[r][_smma+instanceNo] /= k;
   }      
   else   workSmma[r][_smma+instanceNo] = (workSmma[r-1][_smma+instanceNo]*(period-1.0)+price)/period;
   return(workSmma[r][_smma+instanceNo]);
}

//
//
//
//
//

double workLwma[][_maWorkBufferx1];
double iLwma(double price, double period, int r, int instanceNo=0)
{
   if (ArraySize(workLwma)!= Bars) ArrayResize(workLwma,Bars);
   
   //
   //
   //
   //
   //
   
   workLwma[r][instanceNo] = price;
      double sumw = period;
      double sum  = period*price;

      for(int k=1; k<period && (r-k)>=0; k++)
      {
         double weight = period-k;
                sumw  += weight;
                sum   += weight*workLwma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//
//
//

double workLwmp[][_maWorkBufferx1];
double iLwmp(double price, double period, int r, int instanceNo=0)
{
   if (ArraySize(workLwmp)!= Bars) ArrayResize(workLwmp,Bars);
   
   //
   //
   //
   //
   //
   
   workLwmp[r][instanceNo] = price;
      double sumw = period*period;
      double sum  = sumw*price;

      for(int k=1; k<period && (r-k)>=0; k++)
      {
         double weight = (period-k)*(period-k);
                sumw  += weight;
                sum   += weight*workLwmp[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//
//
//

double workTma[][_maWorkBufferx1];
double iTma(double price, double period, int r, int instanceNo=0)
{
   if (ArraySize(workTma)!= Bars) ArrayResize(workTma,Bars);
   
   //
   //
   //
   //
   //
   
   workTma[r][instanceNo] = price;

      double half = (period+1.0)/2.0;
      double sum  = price;
      double sumw = 1;

      for(int k=1; k<period && (r-k)>=0; k++)
      {
         double weight = k+1; if (weight > half) weight = period-k;
                sumw  += weight;
                sum   += weight*workTma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//
//
//

double workSineWMA[][_maWorkBufferx1];
#define Pi 3.14159265358979323846264338327950288

double iSineWMA(double price, int period, int r, int instanceNo=0)
{
   if (period<1) return(price);
   if (ArraySize(workSineWMA)!= Bars) ArrayResize(workSineWMA,Bars);
   
   //
   //
   //
   //
   //
   
   workSineWMA[r][instanceNo] = price;
      double sum  = 0;
      double sumw = 0;
  
      for(int k=0; k<period && (r-k)>=0; k++)
      { 
         double weight = MathSin(Pi*(k+1.0)/(period+1.0));
                sumw  += weight;
                sum   += weight*workSineWMA[r-k][instanceNo]; 
      }
      return(sum/sumw);
}

//
//
//
//
//

double workWwma[][_maWorkBufferx1];
double iWwma(double price, double period, int r, int instanceNo=0)
{
   if (ArraySize(workWwma)!= Bars) ArrayResize(workWwma,Bars);
   
   //
   //
   //
   //
   //
   
   workWwma[r][instanceNo] = price;
      int    i    = Bars-r-1;
      double sumw = Volume[i];
      double sum  = sumw*price;

      for(int k=1; k<period && (r-k)>=0; k++)
      {
         double weight = Volume[i+k];
                sumw  += weight;
                sum   += weight*workWwma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}


//
//
//
//
//

double workHull[][_maWorkBufferx2];
double iHull(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workHull,0)!= Bars) ArrayResize(workHull,Bars);

   //
   //
   //
   //
   //

      int HmaPeriod  = MathMax(period,2);
      int HalfPeriod = MathFloor(HmaPeriod/2);
      int HullPeriod = MathFloor(MathSqrt(HmaPeriod));
      double hma,hmw,weight; instanceNo *= 2;

         workHull[r][instanceNo] = price;

         //
         //
         //
         //
         //
               
         hmw = HalfPeriod; hma = hmw*price; 
            for(int k=1; k<HalfPeriod && (r-k)>=0; k++)
            {
               weight = HalfPeriod-k;
               hmw   += weight;
               hma   += weight*workHull[r-k][instanceNo];  
            }             
            workHull[r][instanceNo+1] = 2.0*hma/hmw;

         hmw = HmaPeriod; hma = hmw*price; 
            for(k=1; k<period && (r-k)>=0; k++)
            {
               weight = HmaPeriod-k;
               hmw   += weight;
               hma   += weight*workHull[r-k][instanceNo];
            }             
            workHull[r][instanceNo+1] -= hma/hmw;

         //
         //
         //
         //
         //
         
         hmw = HullPeriod; hma = hmw*workHull[r][instanceNo+1];
            for(k=1; k<HullPeriod && (r-k)>=0; k++)
            {
               weight = HullPeriod-k;
               hmw   += weight;
               hma   += weight*workHull[r-k][1+instanceNo];  
            }
   return(hma/hmw);
}

//
//
//
//
//

double workLinr[][_maWorkBufferx1];
double iLinr(double price, double period, int r, int instanceNo=0)
{
   if (ArraySize(workLinr)!= Bars) ArrayResize(workLinr,Bars);

   //
   //
   //
   //
   //
   
      period = MathMax(period,1);
      workLinr[r][instanceNo] = price;
         double lwmw = period; double lwma = lwmw*price;
         double sma  = price;
         for(int k=1; k<period && (r-k)>=0; k++)
         {
            double weight = period-k;
                   lwmw  += weight;
                   lwma  += weight*workLinr[r-k][instanceNo];  
                   sma   +=        workLinr[r-k][instanceNo];
         }             
   
   return(3.0*lwma/lwmw-2.0*sma/period);
}

//
//
//
//
//

double workZl[][_maWorkBufferx2];
#define _ema1 0
#define _ema2 1

double iZeroLag(double price, double length, int r, int instanceNo=0)
{
   if (ArrayRange(workZl,0)!=Bars) ArrayResize(workZl,Bars);

   //
   //
   //
   //
   //
      
   double alpha = 2.0/(length+1.0); instanceNo *= 2;
      workZl[r][_ema1+instanceNo] = workZl[r-1][_ema1+instanceNo]+alpha*(price                      -workZl[r-1][_ema1+instanceNo]);
      workZl[r][_ema2+instanceNo] = workZl[r-1][_ema2+instanceNo]+alpha*(workZl[r][_ema1+instanceNo]-workZl[r-1][_ema2+instanceNo]);
   return(2.0*workZl[r][_ema1+instanceNo]-workZl[r][_ema2+instanceNo]);
      
}

//
//
//
//
//

#define _length  0
#define _len     1
#define _weight  2

double  nlm_values[3][_maWorkBufferx1];
double  nlm_prices[ ][_maWorkBufferx1];
double  nlm_alphas[ ][_maWorkBufferx1];

//
//
//
//
//

double iNonLagMa(double price, double length, int r, int instanceNo=0)
{
   if (ArrayRange(nlm_prices,0) != Bars) ArrayResize(nlm_prices,Bars);
                               nlm_prices[r][instanceNo]=price;
   if (length<3 || r<3) return(nlm_prices[r][instanceNo]);
   
   //
   //
   //
   //
   //
   
   if (nlm_values[_length][instanceNo] != length)
   {
      double Cycle = 4.0;
      double Coeff = 3.0*Pi;
      int    Phase = length-1;
      
         nlm_values[_length][instanceNo] = length;
         nlm_values[_len   ][instanceNo] = length*4 + Phase;  
         nlm_values[_weight][instanceNo] = 0;

         if (ArrayRange(nlm_alphas,0) < nlm_values[_len][instanceNo]) ArrayResize(nlm_alphas,nlm_values[_len][instanceNo]);
         for (int k=0; k<nlm_values[_len][instanceNo]; k++)
         {
            if (k<=Phase-1) 
                 double t = 1.0 * k/(Phase-1);
            else        t = 1.0 + (k-Phase+1)*(2.0*Cycle-1.0)/(Cycle*length-1.0); 
            double beta = MathCos(Pi*t);
            double g = 1.0/(Coeff*t+1); if (t <= 0.5 ) g = 1;
      
            nlm_alphas[k][instanceNo]        = g * beta;
            nlm_values[_weight][instanceNo] += nlm_alphas[k][instanceNo];
         }
   }
   
   //
   //
   //
   //
   //
   
   if (nlm_values[_weight][instanceNo]>0)
   {
      double sum = 0;
           for (k=0; k < nlm_values[_len][instanceNo]; k++) sum += nlm_alphas[k][instanceNo]*nlm_prices[r-k][instanceNo];
           return( sum / nlm_values[_weight][instanceNo]);
   }
   else return(0);           
}

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

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i]   = 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;
      }
}