sinc_MA_001
Download scriptThis script is a specialized technical analysis tool designed to smooth out price fluctuations on a trading chart. Unlike standard moving averages that simply average the last few prices, this tool uses advanced mathematical signal processing—specifically a technique involving "wavelets"—to separate the underlying trend of the market from the "noise" of short-term volatility.
How it Works
The logic follows a sophisticated multi-step process to clean the data:
1. Data Initialization and Scaling When you load the script, it first analyzes the amount of historical data available on your chart. It automatically calculates a specific range (based on powers of two) to ensure that the mathematical calculations are performed accurately and efficiently across the timeframe you are viewing.
2. Trend Baseline Extraction The script begins by establishing a basic "fit" for the price action. It looks at the difference between the current price and older prices to create an initial skeleton of the market's movement. By identifying these differences, the tool can begin to isolate what part of the price movement is a steady trend and what part is just erratic jumping.
3. Multi-Level Wavelet Filtering (The Smoothing Process) This is the core "engine" of the script. It performs several rounds of refinement (determined by the "wavelets" settings):
- It breaks the price data down into different frequencies.
- It applies a mathematical "sinc function"—a specialized wave pattern—to the differences in price.
- In each pass, it identifies the high-frequency noise (the tiny, jagged ups and downs) and subtracts it from the total price action.
- By repeating this process multiple times, it progressively peels away the "jitter" until only the smooth, primary trend remains.
4. Dynamic Smoothing Adjustment The tool includes a specific parameter (Q) that controls the "damping" of these waves. This allows the script to balance how much detail it keeps versus how much it smooths out; a higher influence here can result in more ripples, while a lower influence creates a much flatter, cleaner trend line.
The Final Result
The end product displayed on your chart is a solid blue line that represents the "true" direction of the market. Because it filters out noise through wavelet decomposition rather than simple averaging, it aims to provide a clearer view of the price momentum, helping traders see the broader path the market is taking without being distracted by minor, momentary fluctuations.
//+------------------------------------------------------------------+
//| sinc_MA.mq4 |
//| Copyright © 2006, gpwr. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, gpwr."
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
#property indicator_width1 2
//Global constants
#define pi 3.141592653589793238462643383279502884197169399375105820974944592
//Input parameters
extern int N =2560; // Number of last bars. Must be the power of 2. 0: all data.
extern int htrun =3; // Number of the last wavelets to truncate.
// Damping factor. Higher Q gives more ripple. Q=1 gives sinc function.
extern double Q =0.5;
//Global variables
int n;
int pwrMax;
//Indicator buffers
double sMA[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init()
{
if(N <= 0)
N = Bars;
n = 1;
int pwr = 0;
while(n <= N)
{
pwr++;
n *= 2;
}
pwr--;
n = MathPow(2, pwr);
Print("pwr=", pwr);
if(htrun <= 0)
htrun = 0;
if(htrun >= pwr)
htrun = pwr;
pwrMax = pwr - htrun;
IndicatorBuffers(1);
SetIndexBuffer(0, sMA);
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
IndicatorShortName("sincMA");
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//START--------------------------------------------------------------+
int start()
{
ArrayInitialize(sMA, EMPTY_VALUE);
double Fit[], Dif[];
ArrayResize(Fit, n+1);
ArrayResize(Dif, n+1);
for(int i = 0; i <= n; i++)
{
Fit[i] = (Close[n] - Close[0])*i / n + Close[0];
Dif[i] = Close[i] - Fit[i];
sMA[i] = Fit[i];
}
int ss = 2*n; // sampling step
for(int p = 1; p <= pwrMax; p++)
{
ss = ss / 2;
for(i = 0; i <= n; i++)
{
Fit[i] = 0.0;
for(int j = 1; j <= n / ss; j++)
Fit[i] += Dif[j*ss - ss / 2]*wave(2*j - 1 - 2.0*i / ss, Q);
}
for(i = 0; i <= n; i++)
{
Dif[i] -= Fit[i];
sMA[i] += Fit[i];
}
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//sinc function------------------------------------------------------+
double wave(double x, double Q)
{
if(x == 0.0)
return(1.0);
else
return(MathSin(pi*x) / (pi*x) / (x*x + 1)*(Q*x*x + 1));
}
//+------------------------------------------------------------------+
Comments