Kalman filter

Download script
Author: iziogas@mail.com
Kalman filter

This tool is designed to smooth out market price data to help traders identify underlying trends with greater clarity. By applying a mathematical process known as a Kalman filter, it reduces the "noise" typically found in volatile price movements, making it easier to see the actual direction of the market.

How it Works

1. Data Selection (The Source) The script starts by determining which specific price point to analyze. Instead of just looking at one type of price, you can choose from several options, including:

  • Standard prices like the Open, High, Low, or Close.
  • Calculated averages such as the Median (the middle point between High and Low), the Typical Price, or a Weighted average that gives more significance to recent closing actions.

2. Tracking Movement (Velocity and Distance) The core logic works by looking at how far the current price has drifted from its previous position. It calculates two primary factors:

  • Distance: How far the price has moved away from the expected trend.
  • Velocity: The speed or momentum at which that price is moving.

By calculating these two elements simultaneously, the script can distinguish between a minor, temporary price flicker and a significant, sustained movement in a specific direction.

3. Dynamic Smoothing The tool uses these calculations to constantly adjust an "Error" value. It takes the current price and adjusts it based on how fast it is moving and how sharp you want the response to be (controlled by the "Sharpness" and "K" settings). This allows the indicator to react quickly to real trend changes while ignoring minor, insignificant fluctuations.

Visual Output

The script renders two distinct lines on your chart:

  • Upward Trend: When the calculated momentum is positive (moving up), it displays a specific color line representing the smoothed path of the price.
  • Downward Trend: When the momentum turns negative (moving down), the indicator switches to show a different colored line for the descending path.

Essentially, this tool acts like a high-tech "filter," stripping away the erratic jumps of the market to provide a cleaner, more visual representation of where the price is actually headed.

Price Data Components
Series array that contains close prices for each barSeries array that contains open prices of each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
Kalman filter
//+------------------------------------------------------------------+
//|                                                Kalman filter.mq4 |
//|                              Copyright © 2006, iziogas@mail.com. |
//+------------------------------------------------------------------+
#property copyright "iziogas@mail.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Turquoise
#property indicator_color2 Orange
//---- indicator parameters
//Mode description
// 0: Close
// 1: Open
// 2: High
// 3: Low
// 4: Median   (H+L/2)
// 5: Typical  (H+L+C/3)
// 6: Weighted (H+L+C+C/4)
extern int Mode=6;
extern double K=1;
extern double Sharpness=1;
extern int    draw_begin=500;

//---- indicator buffers
double ExtMapBufferUp[];
double ExtMapBufferDown[];

//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));

   SetIndexDrawBegin(0,draw_begin);
   SetIndexDrawBegin(1,draw_begin);

//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBufferUp);
   SetIndexBuffer(1,ExtMapBufferDown);

//---- initialization done
   return(0);
  }
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double iValue(int mode, int shift){
   switch (mode) {
      case 0:
         return (iClose(NULL,0,shift)); 
      case 1:
         return (iOpen(NULL,0,shift)); 
      case 2:
         return (iHigh(NULL,0,shift)); 
      case 3:
         return (iLow(NULL,0,shift)); 
      case 4:
         return ((iHigh(NULL,0,shift)+iLow(NULL,0,shift))/2); 
      case 5:
         return ((iHigh(NULL,0,shift)+iLow(NULL,0,shift)+iClose(NULL,0,shift))/3); 
      case 6:
         return ((iHigh(NULL,0,shift)+iLow(NULL,0,shift)+iClose(NULL,0,shift)+iClose(NULL,0,shift))/4); 
      default:
         return (0); 
      }   
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=draw_begin) return(0);


   int i;

   
   double Velocity=0;
   double Distance=0;
   double Error=0;
   double value = iValue(Mode,draw_begin+1);
   
   for(i=draw_begin;i>=0;i--) {
   
      Distance = iValue(Mode,i) - value;
      Error = value + Distance * MathSqrt(Sharpness*K/100);
      Velocity = Velocity + Distance*K/100;
      value = Error+Velocity;
      
      //color lines
      if (Velocity>0) {
         ExtMapBufferUp[i] = value; 
         //ExtMapBufferUp[i] = S; 
         ExtMapBufferDown[i] = EMPTY_VALUE;
         
         if (ExtMapBufferUp[i+1] == EMPTY_VALUE) ExtMapBufferUp[i+1] = ExtMapBufferDown[i+1]; 
      } else {
         ExtMapBufferUp[i] = EMPTY_VALUE; 
         ExtMapBufferDown[i] = value;
         //ExtMapBufferDown[i] = S;

         if (ExtMapBufferDown[i+1] == EMPTY_VALUE) ExtMapBufferDown[i+1] = ExtMapBufferUp[i+1]; 
      }
      
   }
//---- done
   return(0);
  }

Comments