Help to fix indicator code

 
This is my first attempt to write an indicator. The MQ4 posted below works except for a couple of problems. I have even gone so far as to read coderguru's tutorial and the MQL documentation and book, but cannot find the trouble. I need a pro! The problems are:
1. The filter parameters do not control the filter. Changing the parameters in the code changes the filter, but changing them on a chart has no effect.
2. The last point in the filter is always bad.

This filter comes from J.Ehlers, and is a nice way to distinguish trends from ranging markets.

If someone can help, I would also appreciate an explanation of my errors. Criticism is the only way to learn, so I invite yours.
//+------------------------------------------------------------------+
//|                                     Adaptive Laguerre Filter.mq4 |
//| Original algorithm by J. Ehlers                                  |
//| Adapted by MadCow 3/26/09                                                                |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int       PriceType=0;   //0=Close, 1=(H+L)/2
extern int       MedianLength=5; //Period for averaging the coefficient alpha
extern int       Length=20;      //Lookback period for alpha
extern int       numbars=500; // Smaller speeds calculation
//---- buffers
double ALF[];
double L0[];
double L1[];
double L2[];
double L3[];
double HH[];
double LL[];
double Diff[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
     IndicatorBuffers(8);
//---- indicators
   SetIndexStyle(0, DRAW_LINE);
   SetIndexDrawBegin(0, 1);
	SetIndexLabel(0, "Adaptive Laguerre Filter");
	SetIndexEmptyValue(0, -0.01);
   SetIndexBuffer(0, ALF);
//---- Aux buffers   
   SetIndexBuffer(1, L0);
   SetIndexBuffer(2, L1);
   SetIndexBuffer(3, L2);
   SetIndexBuffer(4, L3);
   SetIndexBuffer(5, HH);
   SetIndexBuffer(6, LL);
   SetIndexBuffer(7, Diff);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    limit=0;
	int    counted_bars = IndicatorCounted();
	double alpha=0.5;
	double alphaO=0.5;
	double x=0.5;
	double beta=2/(1+MedianLength);
	double price=0;
	
	int i;

	if(counted_bars<0)return(-1);
	if(counted_bars>0)counted_bars--;

	limit = Bars - counted_bars-Length;
	if(numbars != 0) limit=numbars;
   //Initilize the buffers, price is not critical
   for( i=limit+Length+10;i>=0;i--)
   {
      ALF[i] = Close[i];
      L0[i] = Close[i];
      L1[i] = Close[i];
      L2[i] = Close[i];
      L3[i] = Close[i];
   }
   
	//----Laguerre Filter of Price
	for (i=limit; i>=0; i--)
	{
	  switch (PriceType)
      {
         case 0: 
            price = Close[i];
            break;
         case 1: 
            price = (High[i]+Low[i])/2;
            break;
         default:  
            price = Close[i];
            break;
       }
     
	  Diff[i] = MathAbs(price - ALF[i+1]);

	  HH[i] = Diff[i];
	  LL[i] = Diff[i];
	//---Set the Laguerre coef 
	  for(int j=0; j<Length-1; j++)
	  {
	    if (Diff[i+j] > HH[i]) HH[i] = Diff[i+j];
		 if (Diff[i+j] < LL[i]) LL[i] = Diff[i+j];
	  }
     if(HH[i]!=LL[i]){
		 x=(Diff[i] - LL[i]) / (HH[i] - LL[i]);
		 alpha = x*beta + alphaO*(1-beta);
	  }
	  alphaO=alpha;
      L0[i] = alpha*price + (1 - alpha)*L0[i-1];
      L1[i] = -(1 - alpha)*L0[i] + L0[i+1] + (1 - alpha)*L1[i+1];
      L2[i] = -(1 - alpha)*L1[i] + L1[i+1] + (1 - alpha)*L2[i+1];
      L3[i] = -(1 - alpha)*L2[i] + L2[i+1] + (1 - alpha)*L3[i+1];
      ALF[i] = (L0[i] + 2*L1[i] + 2*L2[i] + L3[i]) / 6;        
   }

   return(0);	
  }
//+------------------------------------------------------------------+
 
MadCow:
This is my first attempt to write an indicator. The MQ4 posted below works except for a couple of problems. I have even gone so far as to read coderguru's tutorial and the MQL documentation and book, but cannot find the trouble. I need a pro! The problems are:
1. The filter parameters do not control the filter. Changing the parameters in the code changes the filter, but changing them on a chart has no effect.
2. The last point in the filter is always bad.

This filter comes from J.Ehlers, and is a nice way to distinguish trends from ranging markets.

If someone can help, I would also appreciate an explanation of my errors. Criticism is the only way to learn, so I invite yours.
 

I have found the source of my errors on another forum, so please ignore the above post. Sorry, but I cannot find a way to edit or remove this topic. Also sorry for the duplicate post. I have not mastered the intricacies of this forum.

...MadCow

Reason: