NonLagMA_v5

Author: Copyright � 2006, TrendLaboratory
NonLagMA_v5

This script is a sophisticated technical analysis tool designed to track market trends with minimal delay. Most standard moving averages suffer from "lag," meaning they react to price changes only after the movement has already occurred. This specific indicator, the NonLagMA (Non-Lag Moving Average), uses a specialized mathematical approach to smooth out market noise while remaining highly responsive to current price action.

Core Logic and Methodology

The script functions by analyzing historical price data and applying a weighted calculation to determine the current trend. Here is how it achieves this:

1. Advanced Weighting System Unlike a simple moving average where every day has equal importance, or an exponential moving average that favors recent days heavily, this script uses a complex trigonometric weighting system. It calculates a specific "alpha" coefficient for different periods of time. This creates a balanced curve that prioritizes the most relevant price data to ensure the resulting line is smooth but not "late" to the trend.

2. Price Input Versatility The tool is flexible in what it monitors. It can be configured to track various types of price data, including the standard closing price, opening prices, highs, lows, or even median and weighted averages. This allows users to tailor the indicator's sensitivity based on their specific trading style.

3. Dynamic Trend Detection and Coloring One of the primary features of this script is its ability to identify the direction of the trend automatically:

  • Trend Tracking: The logic compares the current calculated value against the previous one. If the value moves up beyond a certain threshold, it identifies an upward trend; if it moves down, it identifies a downward trend.
  • Visual Differentiation: Based on user settings, the indicator can change colors dynamically. It distinguishes between "Up" movements and "Down" movements by splitting the data into different visual buffers, making it easier for a trader to see at a glance whether the market momentum is bullish or bearish.

4. Smoothing and Filtering Mechanisms To prevent the indicator from reacting to insignificant price "flickers" (market noise), the script includes two specific control mechanisms:

  • Static Filter: This ensures that the line only moves if the price change exceeds a specific number of points. If the movement is too small, the indicator holds its previous value, resulting in a cleaner, more stable line.
  • Deviation Adjustment: Users can apply a percentage-based deviation to slightly adjust the output, allowing for fine-tuning of the average's position relative to the actual price.

Summary of User Experience

For a trader, this script provides a visual "roadmap" of market momentum. By filtering out the erratic jumps of the market and focusing on a smoothed, weighted trend line, it helps identify the primary direction of price movement while attempting to eliminate the delay typically found in standard charting tools.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
NonLagMA_v5
//+------------------------------------------------------------------+
//|                                                  NonLagMA_v5.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 Orange
#property indicator_width1 2
#property indicator_color2 Blue
#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         = 9;  // Period of NonLagMA
extern int     Displace       = 0;  // DispLace or Shift 
extern int     Filter         = 0;  // Static filter in points
extern int     Color          = 1;  // Switch of Color mode (1-color)  
extern int     ColorBarBack   = 1;  // Bar back for color mode
extern double  Deviation      = 0;  // Up/down deviation        
//---- indicator buffers
double MABuffer[];
double UpBuffer[];
double DnBuffer[];
double trend[];
//----
double alfa[];
int i, Phase, Len, Cycle = 4;
double Coeff, beta, t, Sum, Weight, g;
double pi = 3.1415926535;    
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(4);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, MABuffer);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, UpBuffer);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(2, DnBuffer);
   SetIndexBuffer(3, trend);
   string short_name;
//---- name for DataWindow and indicator subwindow label
   short_name = "NonLagMA(" + Length + ")";
   IndicatorShortName(short_name);
   SetIndexLabel(0, "NonLagMA");
   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);
   SetIndexDrawBegin(1, Length*Cycle + Length);
   SetIndexDrawBegin(2, Length*Cycle + Length);
//---- 
   Coeff =  3*pi;
   Phase = Length-1;
   Len = Length*Cycle + 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_v5                                                      |
//+------------------------------------------------------------------+
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(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