//+------------------------------------------------------------------+
//|                                                  NonLagMA_v2.mq4 |
//|                                Copyright � 2006, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright � 2006, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 SeaGreen
#property indicator_color2 Orange

//---- input parameters
extern int Price4ma =  0;
extern int Len      = 39;
extern double Cycle =  4;
extern int Phase    =  2;
extern double Level =  0.3;
extern double Coeff =  7;
//---- indicator buffers
double MAB[],MABuffer0[],MABuffer1[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   string short_name;
//---- indicator line
   SetIndexStyle(0,DRAW_LINE,EMPTY,2);
   SetIndexStyle(1,DRAW_LINE,EMPTY,2);
   SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   SetIndexBuffer(0,MABuffer0);
   SetIndexBuffer(1,MABuffer1);
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   
//---- name for DataWindow and indicator subwindow label
   short_name="NonLagMA("+Len+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"NLMA");
//----
   SetIndexDrawBegin(0,Len);
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| NonLagMA_v2                                                     |
//+------------------------------------------------------------------+
int start()
{
   int    i,shift, counted_bars=IndicatorCounted(),limit, num;
   double alfa, beta, t, AvgRange,Weight, step,step1,g;
   double pi = 3.1415926535;    
      
   if ( counted_bars > 0 )  limit=Bars-counted_bars;
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-Len-1; 
   
   ArrayResize(MAB,Bars);
   step = 2.0*Cycle/(Len-1);
   num = MathRound((Len-1)/(2.0*Cycle))+Phase;
     
   for(shift=limit;shift>=0;shift--) 
   {	
   Weight=0;AvgRange=0;
   
      for (i=0;i<=Len-1;i++)
	   { 
         if ( i <= num )
         step = 1.0/num;  
         else
         step = (2.0*Cycle-1)/(Len-1-num);
      
      t = i*step;
      g = 1.0/(Coeff*t+1);   
      if (g > Level ) g = 1;
      beta = MathSin(pi*(t + 0.5));
      alfa = g * beta;
      AvgRange = AvgRange + alfa*iMA(NULL,0,1,0,MODE_SMA,Price4ma,shift+i);
      Weight =Weight + alfa;
      
      }
	
	if (Weight > 0) MAB[shift]=AvgRange/Weight;

   if (MAB[shift+1]<=MAB[shift]) MABuffer0[shift]=MAB[shift]; else MABuffer0[shift]=0;
   if (MAB[shift+1]>MAB[shift]) MABuffer1[shift]=MAB[shift]; else MABuffer1[shift]=0;
   }
	return(0);	
}