Gaussian filter
Download scriptThis indicator is designed to provide a refined view of market trends by applying a specialized mathematical process known as Gaussian filtering to price data. Unlike standard moving averages that can sometimes be "noisy" or react too slowly to significant shifts, this tool aims to smooth out price fluctuations while maintaining a clear representation of the underlying momentum.
Core Logic and Mechanics
The tool operates through three primary stages:
1. Data Input and Scaling The script begins by selecting specific price data (typically the closing price) and applying an initial smoothing pass. It establishes a base moving average, which serves as the foundation for all subsequent calculations. This ensures that the indicator is grounded in recent historical price action.
2. Gaussian Weighting System The heart of the script lies in its "Gaussian" logic. In mathematics, a Gaussian distribution (or bell curve) is used to give more importance to data points that are central and less importance to outliers. The script calculates a series of complex coefficients based on three user-defined parameters:
- Length: Determines the timeframe of data considered.
- Order: Controls the "smoothness" or the degree of the filter.
- Price: Defines which price points are being analyzed.
By using these coefficients, the indicator calculates a weighted sum where each preceding price point contributes to the current value based on its mathematical relationship to the curve. This helps in filtering out "market noise"—those small, erratic price movements that don't represent a true change in trend.
3. Recursive Smoothing To achieve a high level of precision, the script performs a recursive calculation. It doesn't just look at one previous point; it looks back across multiple intervals (determined by the "Order" setting), alternating the mathematical signs to balance the data. This prevents the indicator from lagging too far behind the current price while ensuring that the line remains fluid and consistent.
Visual Representation
The result is a smooth, red line displayed directly on your main chart window. Because it uses a Gaussian filter, the line behaves more organically than a standard moving average. It is designed to help traders identify the dominant direction of the market by stripping away minor distractions, providing a cleaner visual path of where the price is heading.
//+------------------------------------------------------------------+
//| mladen |
//| gaussian filter |
//+------------------------------------------------------------------+
#property copyright "Copyleft © 2008, mladen"
#property link "mladenfx@gmail.com"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_level1 0.0
#property indicator_levelcolor DarkSlateGray
//
//
//
//
//
extern int Length = 20;
extern int Order = 2;
extern int Price = PRICE_CLOSE;
//
//
//
//
//
double buffer[];
double coeffs[][3];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int init()
{
IndicatorDigits(15);
Length = MathMax(Length,2);
Order = MathMax(MathMin(Order,12),1);
InitCoeffs(Length,Order);
//
//
//
//
//
SetIndexBuffer(0,buffer); SetIndexDrawBegin(0,Length);
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int start()
{
int counted_bars=IndicatorCounted();
int i,limit;
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit = Bars-counted_bars;
//
//
//
//
//
for(i = limit; i >= 0; i--)
{
buffer[i] = iMA(NULL,0,1,0,MODE_SMA,Price,i)*coeffs[Order][1];
double sign = 1;
for (int r=1; r <= Order; r++, sign *= -1.0)
buffer[i] += sign*coeffs[r][0]*coeffs[r][2]*buffer[i+r];
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
#define Pi 3.141592653589793238462643
void InitCoeffs(int length, int n)
{
double b = (1.0 - MathCos(2.0*Pi/length))/(MathPow(MathSqrt(2.0),2.0/n) - 1.0);
double a = -b + MathSqrt(b*b + 2.0*b);
ArrayResize(coeffs,n+1);
for(int r=0; r<=n; r++)
{
coeffs[r][0] = fact(n)/(fact(n-r)*fact(r));
coeffs[r][1] = MathPow( a,r);
coeffs[r][2] = MathPow(1.0-a,r);
}
}
//
//
//
//
//
int fact(int n)
{
if(n==1||n==0)
return(1);
else return(n*fact(n-1));
}
Comments