NonLagMA_v4_v2
Download scriptThis script is a sophisticated technical analysis tool designed to identify market trends with high precision. Its primary objective is to provide a "Non-Lagging" Moving Average, which solves a common problem in trading: the delay between a price change and the indicator's reaction to that change.
Core Logic and Functionality
1. Advanced Smoothing Technique Unlike standard moving averages that simply average past prices (often resulting in a sluggish response), this script utilizes a complex mathematical approach involving trigonometric functions (cosine waves) and weighted distributions. It calculates a specific "weight" for each price point based on its position in time. By applying these weights, the indicator can follow the price action more closely while still filtering out insignificant market noise.
2. Trend Detection & Dynamic Coloring The script does not just provide a single line; it intelligently interprets the direction of the market:
- Upward Momentum: When the moving average begins to trend upward with sufficient strength, the tool identifies an "Up" trend.
- Downward Momentum: Conversely, when the average turns downward, it marks a "Down" trend.
- Visual Clarity: The script uses these calculations to color the data lines (SkyBlue for up-trends and Tomato for down-trends), allowing traders to see the current market bias at a glance without manual interpretation.
3. Noise Filtering & Stability To prevent the indicator from flickering or reacting too aggressively to tiny, meaningless price fluctuations, the script includes a "Filter" mechanism. This ensures that the trend only changes when the movement is significant enough to be considered a meaningful shift in market direction. It acts as a stabilizer, keeping the lines smooth and reliable for long-term analysis.
4. Customization for User Needs The logic is highly adjustable through several parameters:
- Length: Determines how much historical data is considered (balancing sensitivity vs. stability).
- Deviation: Allows for a slight mathematical adjustment to the calculated average.
- Color Settings: Provides options to customize how the trend shifts are visually displayed on the chart.
Summary for the Trader
Essentially, this script acts as an "intelligent" moving average. It is designed to react faster than traditional tools to new price movements while remaining smooth enough to avoid "false signals." By visualizing the trend in different colors and applying a weighted mathematical formula, it helps traders identify the prevailing direction of the market with greater clarity and less delay.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| NonLagMA_v4.mq4 |
//| Copyright © 2006, TrendLaboratory |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//| E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, TrendLaboratory"
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_width1 2
#property indicator_color2 SkyBlue
#property indicator_width2 2
#property indicator_color3 Tomato
#property indicator_width3 2
//---- input parameters
extern int Price = 0;
extern int Length = 9;
extern int Displace = 0;
extern int Filter = 0;
extern int Color = 0;
extern int ColorBarBack = 1;
extern double Deviation = 0;
double Cycle = 4;
//---- indicator buffers
double MABuffer[];
double UpBuffer[];
double DnBuffer[];
double price[];
double trend[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
int ft=0;
string short_name;
//---- indicator line
IndicatorBuffers(5);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,MABuffer);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,UpBuffer);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,DnBuffer);
SetIndexBuffer(3,price);
SetIndexBuffer(4,trend);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
short_name="NonLagMA("+Length+")";
IndicatorShortName(short_name);
SetIndexLabel(0,"NLMA");
SetIndexLabel(1,"Up");
SetIndexLabel(2,"Dn");
//----
SetIndexShift(0,Displace);
SetIndexShift(1,Displace);
SetIndexShift(2,Displace);
SetIndexDrawBegin(0,Length*Cycle+Length);
SetIndexDrawBegin(1,Length*Cycle+Length);
SetIndexDrawBegin(2,Length*Cycle+Length);
//----
return(0);
}
//+------------------------------------------------------------------+
//| NonLagMA_v4 |
//+------------------------------------------------------------------+
int start()
{
int i,shift, counted_bars=IndicatorCounted(),limit;
double alfa, beta, t, Sum, Weight, step,g;
double pi = 3.1415926535;
double Coeff = 3*pi;
int Phase = Length-1;
double Len = Length*Cycle + Phase;
if ( counted_bars > 0 ) limit=Bars-counted_bars;
if ( counted_bars < 0 ) return(0);
if ( counted_bars ==0 ) limit=Bars-Len-1;
if ( counted_bars < 1 )
for(i=1;i<Length*Cycle+Length;i++)
{
MABuffer[Bars-i]=0;
UpBuffer[Bars-i]=0;
DnBuffer[Bars-i]=0;
}
for(shift=limit;shift>=0;shift--)
{
Weight=0; Sum=0; t=0;
for (i=0;i<=Len-1;i++)
{
g = 1.0/(Coeff*t+1);
if (t <= 0.5 ) g = 1;
beta = MathCos(pi*t);
alfa = g * beta;
//if (shift>=1) price[i] = iMA(NULL,0,Per,Displace,Mode,Price,shift+i);
//else
price[i] = iMA(NULL,0,1,0,MODE_SMA,Price,shift+i);
Sum += alfa*price[i];
Weight += alfa;
if ( t < 1 ) t += 1.0/(Phase-1);
else if ( t < Len-1 ) t += (2*Cycle-1)/(Cycle*Length-1);
}
if (Weight > 0) MABuffer[shift] = (1.0+Deviation/100)*Sum/Weight;
if (Filter>0)
{
if( MathAbs(MABuffer[shift]-MABuffer[shift+1]) < Filter*Point ) MABuffer[shift]=MABuffer[shift+1];
}
if (Color>0)
{
trend[shift]=trend[shift+1];
if (MABuffer[shift]-MABuffer[shift+1] > Filter*Point) trend[shift]= 1;
if (MABuffer[shift+1]-MABuffer[shift] > Filter*Point) trend[shift]=-1;
if (trend[shift]>0)
{
UpBuffer[shift] = MABuffer[shift];
if (trend[shift+ColorBarBack]<0) UpBuffer[shift+ColorBarBack]=MABuffer[shift+ColorBarBack];
DnBuffer[shift] = EMPTY_VALUE;
}
if (trend[shift]<0)
{
DnBuffer[shift] = MABuffer[shift];
if (trend[shift+ColorBarBack]>0) DnBuffer[shift+ColorBarBack]=MABuffer[shift+ColorBarBack];
UpBuffer[shift] = EMPTY_VALUE;
}
}
}
return(0);
}
Comments