NonLagDot71

Download script
Author: Copyright � 2007, TrendLaboratory
NonLagDot71

The NonLagDot script is a sophisticated technical analysis tool designed to track market trends with a focus on minimizing "lag"—the delay that typically occurs between a price movement and a moving average's reaction. While standard moving averages often smooth out data by looking backward, this script uses advanced mathematical weighting to stay as close to the current price action as possible while still filtering out insignificant noise.

Core Logic & Methodology

1. Advanced Weighting System Instead of treating all past prices equally, the script calculates a custom set of weights (coefficients). It uses trigonometric functions and specific multipliers to prioritize certain data points. This ensures that the indicator reacts quickly to new price changes but remains structurally stable enough to identify the overall direction of the market.

2. Trend Detection & Smoothing The script calculates a "Moving Average" value by summing the weighted prices. However, it goes beyond simple averaging by incorporating two distinct filtering layers:

  • Deviation Adjustment: It can slightly stretch or compress the calculated value based on a user-defined percentage to account for specific market volatilities.
  • Dynamic Filtering (Standard Deviation): If enabled, the script measures the "distance" between consecutive average points. By calculating the Standard Deviation of these movements, it determines what constitutes a significant trend change versus mere market "jitter." If a movement is smaller than this calculated threshold, the script ignores it, effectively "locking" the line in place until a meaningful move occurs.

3. Visual Trend Identification The indicator is designed to provide clear visual cues for trend direction:

  • Color/Trend Mode: When active, the script monitors whether the average is moving up or down relative to its previous position.
  • Buffer Separation: It uses different "buffers" (visual layers) to distinguish between upward and downward movements. If the market is trending up, it highlights that direction; if it turns down, it switches labels.
  • Look-back Logic: It includes a feature that can project or "bridge" colors back over previous bars, helping the user see the continuity of a trend more clearly on the chart.

How it Functions for the User

In practical terms, this script acts as a "smart" moving average. It seeks to eliminate the "choppiness" of the market by ignoring small price fluctuations (via the Standard Deviation filter) while ensuring that when a real trend shift happens, the indicator reflects it immediately without waiting for several candles to pass. The resulting visual output helps traders distinguish between a stagnant market and a developing trend with higher precision than standard tools.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
12 Views
1 Downloads
0 Favorites
NonLagDot71
//+------------------------------------------------------------------+
//|                                                NonLagMA_v7.1.mq4 |
//|                                Copyright © 2007, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//|                                                                  |
//| Modified to be NonLagDot71 by Robert                             |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, 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 RoyalBlue
#property indicator_width2 2
#property indicator_color3 Red
#property indicator_width3 2


//---- input parameters
extern int     Price          = 0;  //Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close) 
extern int     Length         = 20;  //Period of NonLagMA
extern int     Displace       = 0;  //DispLace or Shift 
extern double  PctFilter      = 0;  //Dynamic filter in decimal
extern int     Color          = 1;  //Switch of Color mode (1-color)  
extern int     ColorBarBack   = 0;  //Bar back for color mode
extern double  Deviation      = 0;  //Up/down deviation
extern int     Cycle          = 4;        
//---- indicator buffers
double MABuffer[];
double UpBuffer[];
double DnBuffer[];
double trend[];
double Del[];
double AvgDel[];

double alfa[];
int i, Phase, Len;
double Coeff, beta, t, Sum, Weight, g;
double pi = 3.1415926535;    

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   IndicatorBuffers(6);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexBuffer(0,MABuffer);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexBuffer(1,UpBuffer);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexBuffer(2,DnBuffer);
   SetIndexBuffer(3,trend);
   SetIndexBuffer(4,Del);
   SetIndexBuffer(5,AvgDel); 
   SetIndexArrow(0,159);
   SetIndexArrow(1,159);
   SetIndexArrow(2,159);
   string short_name;
//---- indicator line
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="NonLagDot("+Length+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"NLD");
   SetIndexLabel(1,"Up");
   SetIndexLabel(2,"Dn");
//----
   SetIndexShift(0,Displace);
   SetIndexShift(1,Displace);
   SetIndexShift(2,Displace);
   
   SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   SetIndexEmptyValue(2,EMPTY_VALUE);
   
   SetIndexDrawBegin(0,Length*Cycle+Length+1);
   SetIndexDrawBegin(1,Length*Cycle+Length+1);
   SetIndexDrawBegin(2,Length*Cycle+Length+1);
//----
   
   Coeff =  3*pi;
   Phase = Length-1;
   Len = Length*4 + Phase;  
   ArrayResize(alfa,Len);
   Weight=0;    
      
      for (i=0;i<Len-1;i++)
      {
      if (i<=Phase-1) t = 1.0*i/(Phase-1);
      else t = 1.0 + (i-Phase+1)*(2.0*Cycle-1.0)/(Cycle*Length-1.0); 
      beta = MathCos(pi*t);
      g = 1.0/(Coeff*t+1);   
      if (t <= 0.5 ) g = 1;
      alfa[i] = g * beta;
      Weight += alfa[i];
      }
 
   return(0);
  }

//+------------------------------------------------------------------+
//| NonLagMA_v7.1                                                    |
//+------------------------------------------------------------------+
int start()
{
   int    i,shift, counted_bars=IndicatorCounted(),limit;
   double price;      
   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--) 
   {	
      Sum = 0;
      for (i=0;i<=Len-1;i++)
	   { 
      price = iMA(NULL,0,1,0,3,Price,i+shift);      
      Sum += alfa[i]*price;
      
      }
   
	if (Weight > 0) MABuffer[shift] = (1.0+Deviation/100)*Sum/Weight;
   
      
      if (PctFilter>0)
      {
      Del[shift] = MathAbs(MABuffer[shift] - MABuffer[shift+1]);
   
      double sumdel=0;
      for (i=0;i<=Length-1;i++) sumdel = sumdel+Del[shift+i];
      AvgDel[shift] = sumdel/Length;
    
      double sumpow = 0;
      for (i=0;i<=Length-1;i++) sumpow+=MathPow(Del[shift+i]-AvgDel[shift+i],2);
      double StdDev = MathSqrt(sumpow/Length); 
     
      double Filter = PctFilter * StdDev;
     
      if( MathAbs(MABuffer[shift]-MABuffer[shift+1]) < Filter ) MABuffer[shift]=MABuffer[shift+1];
      }
      else
      Filter=0;
      
      if (Color>0)
      {
      trend[shift]=trend[shift+1];
      if (MABuffer[shift]-MABuffer[shift+1] > Filter) trend[shift]= 1; 
      if (MABuffer[shift+1]-MABuffer[shift] > Filter) 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