NonLagMA_2 colors
Download scriptThis script is a sophisticated technical analysis tool designed to provide a "Non-Lagging" Moving Average. In standard trading indicators, moving averages often suffer from a "lag" effect—meaning they react to price changes only after the movement has already occurred. This specific tool is engineered to smooth out market noise while minimizing that delay, offering a more responsive view of price trends.
How the Logic Works
The script processes price data using a unique mathematical weighting system rather than simply averaging the last few candles equally. Here is the breakdown of its internal logic:
1. Dynamic Weighting Architecture Instead of treating every previous price point with equal importance, the script calculates a custom "weight" for each piece of data within its look-back period. It uses a combination of trigonometric functions (sine waves) and specific coefficients to decide which price points should influence the current average more heavily than others.
2. The "Non-Lag" Smoothing Mechanism The core objective of the script is to balance two competing needs: smoothness and responsiveness. It achieves this by:
- Applying a specific curve (determined by the "Cycle" and "Phase" parameters) that allows the indicator to follow the momentum of the market.
- Using a leveling mechanism that prevents the indicator from overreacting to minor, insignificant price fluctuations (noise).
- Ensuring that as the market trends up or down, the indicator stays as close to the current price action as possible without creating "choppy" movements.
3. Trend Detection & Visualization The script doesn't just calculate a single line; it interprets the direction of the movement to provide visual clarity:
- Upward Momentum: When the calculated average is moving higher than its previous position, the script identifies an upward trend.
- Downward Momentum: When the average is moving lower, it identifies a downward trend.
- Visual Output: It distinguishes these two movements using two different colors (Sea Green and Orange). This allows a trader to see instantly whether the momentum is shifting bullish or bearish by observing which color line is currently active on the chart.
Summary for the User
Essentially, this tool acts as a "smart" filter. It looks at historical price data, applies a complex mathematical formula to prioritize the most relevant information, and draws a smoothed line on your chart. Because it is designed to be "Non-Lagging," it aims to tell you where the trend is going faster than a standard moving average would, while still providing a clean, readable path for your analysis.
//+------------------------------------------------------------------+
//| NonLagMA_v2.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 2
#property indicator_color1 SeaGreen
#property indicator_color2 Orange
//---- input parameters
extern int Price4ma = 0;
extern int Len = 39;
extern double Cycle = 4;
extern int Phase = 2;
extern double Level = 0.3;
extern double Coeff = 7;
//---- indicator buffers
double MAB[],MABuffer0[],MABuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicator line
SetIndexStyle(0,DRAW_LINE,EMPTY,2);
SetIndexStyle(1,DRAW_LINE,EMPTY,2);
SetIndexEmptyValue(0,EMPTY_VALUE);
SetIndexEmptyValue(1,EMPTY_VALUE);
SetIndexBuffer(0,MABuffer0);
SetIndexBuffer(1,MABuffer1);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
short_name="NonLagMA("+Len+")";
IndicatorShortName(short_name);
SetIndexLabel(0,"NLMA");
//----
SetIndexDrawBegin(0,Len);
//----
return(0);
}
//+------------------------------------------------------------------+
//| NonLagMA_v2 |
//+------------------------------------------------------------------+
int start()
{
int i,shift, counted_bars=IndicatorCounted(),limit, num;
double alfa, beta, t, AvgRange,Weight, step,step1,g;
double pi = 3.1415926535;
if ( counted_bars > 0 ) limit=Bars-counted_bars;
if ( counted_bars < 0 ) return(0);
if ( counted_bars ==0 ) limit=Bars-Len-1;
ArrayResize(MAB,Bars);
step = 2.0*Cycle/(Len-1);
num = MathRound((Len-1)/(2.0*Cycle))+Phase;
for(shift=limit;shift>=0;shift--)
{
Weight=0;AvgRange=0;
for (i=0;i<=Len-1;i++)
{
if ( i <= num )
step = 1.0/num;
else
step = (2.0*Cycle-1)/(Len-1-num);
t = i*step;
g = 1.0/(Coeff*t+1);
if (g > Level ) g = 1;
beta = MathSin(pi*(t + 0.5));
alfa = g * beta;
AvgRange = AvgRange + alfa*iMA(NULL,0,1,0,MODE_SMA,Price4ma,shift+i);
Weight =Weight + alfa;
}
if (Weight > 0) MAB[shift]=AvgRange/Weight;
if (MAB[shift+1]<=MAB[shift]) MABuffer0[shift]=MAB[shift]; else MABuffer0[shift]=0;
if (MAB[shift+1]>MAB[shift]) MABuffer1[shift]=MAB[shift]; else MABuffer1[shift]=0;
}
return(0);
}
Comments