//+------------------------------------------------------------------+
//|                                               KalmanBands_v1.mq4 |
//|                           Copyright © 2007, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"
//---- indicator settings
#property indicator_chart_window 
#property indicator_buffers 3 
#property indicator_color1 LightBlue
#property indicator_color2 LightBlue
#property indicator_color3 LightBlue 
#property indicator_width1 1
#property indicator_width2 2
#property indicator_width3 2
//---- indicator parameters
extern int     Price          =  0;  //Price mode : 0-Close,1-Open,2-High,3-Low,4-Median,5-Typical,6-Weighted
extern int     Order          =  3;  //Filter Order: 1-EMA,2-2nd Order,3-3rd Order
extern int     Length         = 14;  //Fast Filter Period 
extern int     PreSmooth      =  3;  //Pre-smoothing period
extern int     PreSmoothMode  =  3;  //Pre-smoothing MA Mode: 0-SMA,1-EMA,2-SMMA,3-LWMA
extern double  K_Sigma        =  2;  //Multiplier of Sigma(Standard Deviation)
//---- indicator buffers
double     Kalman[];
double     UpBand[];
double     DnBand[];
double     LowPass[];
double     Delta[];

int        draw_begin;
double     a, b, c, pi = 3.1415926535;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicator buffers mapping
   IndicatorBuffers(5);
   SetIndexBuffer(0,Kalman);
   SetIndexBuffer(1,UpBand);
   SetIndexBuffer(2,DnBand);
   SetIndexBuffer(3,LowPass);
   SetIndexBuffer(4,Delta);
   
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   draw_begin = 3*(Length+3) + PreSmooth;
   SetIndexDrawBegin(0,draw_begin);
   SetIndexDrawBegin(1,draw_begin);
   SetIndexDrawBegin(2,draw_begin);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("KalmanBands("+Order+","+Length+","+PreSmooth+")");
   SetIndexLabel(0,"KalmanFilter");
   SetIndexLabel(1,"UpKalmanBand");
   SetIndexLabel(2,"DnKalmanBand");
//----
   if(Order == 1)
   a = 2.0/(1+Length);
   else
   if(Order == 2)
   {
   a = MathExp(-MathSqrt(2)*pi/Length);
	b = 2*a*MathCos(MathSqrt(2)*pi/Length);
   }
   else
   if(Order == 3)
   {
   a = MathExp(-pi/Length);
	b = 2*a*MathCos(MathSqrt(3)*pi/Length);
   c = MathExp(-2*pi/Length);
   } 
//---- initialization done
   return(0);
}
//+------------------------------------------------------------------+
//| NonLinearKalman_v1                                                 |
//+------------------------------------------------------------------+
int start()
{
   int limit, i, shift;
   int counted_bars=IndicatorCounted();
   double Smoother, Detrend;
   
   if(counted_bars<1)
   for(i=1;i<=draw_begin;i++) 
   {
   Kalman[Bars-i]=iMA(NULL,0,1,0,0,Price,Bars-i); 
   LowPass[Bars-i]=iMA(NULL,0,1,0,0,Price,Bars-i); 
   Delta[Bars-i]=0; 
   }
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(shift=limit; shift>=0; shift--)
   {
   Smoother = iMA(NULL,0,PreSmooth,0,PreSmoothMode,Price,shift);
      
      if(Order == 1) 
      {
      LowPass[shift] = (1-a)*LowPass[shift+1] + a*Smoother;
      Detrend = Smoother - LowPass[shift]; 
      Delta[shift] =  (1-a)*Delta[shift+1] + a*Detrend;
      }
      else
		if(Order == 2) 
		{
		LowPass[shift] = b*LowPass[shift+1] - a*a*LowPass[shift+2] + (1 - b + a*a)*Smoother;
		Detrend = Smoother - LowPass[shift]; 
		Delta[shift] = b*Delta[shift+1] - a*a*Delta[shift+2] + (1 - b + a*a)*Detrend;
		}
		else
		if(Order == 3) 
		{
		LowPass[shift] = (b+c)*LowPass[shift+1] - (c+b*c)*LowPass[shift+2] + c*c*LowPass[shift+3] + (1-b+c)*(1-c)*Smoother;
		Detrend = Smoother - LowPass[shift];
		Delta[shift] = (b+c)*Delta[shift+1] - (c+b*c)*Delta[shift+2] + c*c*Delta[shift+3] + (1-b+c)*(1-c)*Detrend;
		}
	Kalman[shift] = LowPass[shift] + Delta[shift]; 	
   }
   
   for(shift=limit; shift>=0; shift--)
   {
   double Sum=0;
      for(int j=Length-1;j>=0;j--)
      {
      Smoother = iMA(NULL,0,PreSmooth,0,PreSmoothMode,Price,shift+j);
      double del = Smoother - Kalman[shift+j]; 
      Sum += del*del;
      }
      
      if (Length-1 > 0 && Sum > 0)
      {
      if(Length < 32) double StdDev = MathSqrt(Sum/(Length-1));   
      else
      StdDev = MathSqrt(Sum/Length);       
      }
   UpBand[shift] = Kalman[shift] + K_Sigma*StdDev;      
   DnBand[shift] = Kalman[shift] - K_Sigma*StdDev;
   }
//---- done
   return(0);
}