FIR_MA_001
Download scriptThis script functions as a specialized technical analysis tool designed to smooth out price fluctuations on a trading chart. Unlike standard moving averages that treat every past price point with equal importance, this tool uses a sophisticated mathematical framework known as a Finite Impulse Response (FIR) filter.
How the Logic Works
The core objective of this script is to filter out "market noise"—the small, erratic price movements—to reveal the underlying trend more clearly. It achieves this through three main components:
1. Weighted Influence (Taps and Windows) Instead of simply averaging the last few candles, the script looks at a specific number of data points (defined as "Taps"). It then applies a "Windowing Function" to these points. Think of this like a spotlight: it ensures that price points closest to the current moment have a different level of influence than those further back. By choosing different window types (such as Hanning, Hamming, or Blackman), users can adjust how aggressively the tool smooths the data or how much it reacts to recent changes.
2. Bandwidth Control The logic includes a "Periods" parameter which acts as a filter bandwidth. This determines the "sharpness" of the filter. It essentially tells the script how much frequency to let through; a narrower focus helps isolate specific trends while suppressing more rapid, insignificant price vibrations.
3. The Calculation Process For every point on the chart, the script performs the following steps:
- It identifies the required number of historical price points.
- It calculates a unique weight for each of those points based on the selected Window and Period settings.
- It multiplies each price by its corresponding weight to create a "weighted sum."
- Finally, it normalizes this sum so that the resulting line accurately reflects the weighted average of the data.
Visual Representation
The final output is a solid red line displayed directly on your trading chart. Because this logic uses a weighted distribution rather than a simple linear average, the resulting line aims to provide a more refined and mathematically balanced view of price momentum compared to standard indicators.
//+------------------------------------------------------------------+
//| FIR_MA.mq4 |
//| v.1 09/04/2006 |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2
//Global constants
#define pi 3.141592653589793238462643383279502884197169399375105820974944592
//Input parameters
extern int Periods = 4; // 1/(2*Periods) sets the filter bandwidth
extern int Taps = 21; // must be an odd number
extern int Window = 4; // selects windowing function
//Indicator buffers
double FIRMA[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init()
{
ArrayInitialize(FIRMA, EMPTY_VALUE);
IndicatorBuffers(1);
SetIndexBuffer(0, FIRMA);
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
IndicatorShortName("FIRMA");
SetIndexShift(0, -(Taps - 1) / 2);
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double w[];
ArrayResize(w, Taps);
double wsum = 0.0;
for(int k = 0; k < Taps; k++)
{
switch(Window)
{
// Rectangular window
case 1: w[k] = 1.0; break;
// Hanning window
case 2: w[k] = 0.50 - 0.50*MathCos(2.0*pi*k / Taps); break;
// Hamming window
case 3: w[k] = 0.54 - 0.46*MathCos(2.0*pi*k / Taps); break;
//Blackman window
case 4: w[k] = 0.42 - 0.50*MathCos(2.0*pi*k / Taps) +
0.08*MathCos(4.0*pi*k / Taps); break;
//Blackman-Harris window
case 5: w[k] = 0.35875 - 0.48829*MathCos(2.0*pi*k / Taps) +
0.14128*MathCos(4.0*pi*k / Taps) -
0.01168*MathCos(6.0*pi*k / Taps); break;
//Rectangular window
default: w[k] = 1;break;
}
if(k != Taps / 2.0)
w[k] = w[k]*MathSin(pi*(k - Taps / 2.0) / Periods) / pi / (k - Taps / 2.0);
wsum+=w[k];
}
int i = Bars - 1 - IndicatorCounted();
while(i >= 0)
{
if(i <= Bars - Taps)
{
FIRMA[i] = 0.0;
for(k = 0; k < Taps; k++)
FIRMA[i] += Close[i+k]*w[k] / wsum;
}
i--;
}
return(0);
}
//+------------------------------------------------------------------+
Comments