NonLagMA_v2
Download scriptThis script is designed to provide traders with a specialized moving average that addresses one of the most common challenges in technical analysis: "lag." In standard moving averages, the indicator reacts to price changes only after they have already occurred, which can lead to delayed signals. This tool, the NonLagMA, uses a unique mathematical approach to smooth out price data while staying as close to current market action as possible.
How the Logic Works
The core functionality of this script relies on a sophisticated weighting system rather than a simple average of past prices. Here is a breakdown of the logic:
1. Dynamic Weight Distribution Instead of giving equal importance to every price point over a set period, the script assigns specific "weights" to different pieces of historical data. It calculates these weights using a combination of trigonometric functions and custom coefficients. This allows the indicator to prioritize more relevant price movements while still incorporating enough historical context to remain smooth.
2. Curve Smoothing (The Cycle and Phase) The script utilizes parameters like "Cycle" and "Phase" to shape the curve of the moving average. By applying these calculations, the tool creates a more fluid, organic line on the chart. This helps traders identify the underlying trend by filtering out the "noise" of minor, insignificant price fluctuations without waiting for the trend to be fully established before the indicator reacts.
3. The Lag Reduction Mechanism To solve the delay issue, the logic applies a specific coefficient that adjusts how quickly the moving average adjusts to new information. It essentially tries to predict the "flow" of the price. By calculating a weighted average where the weights are dynamically adjusted based on their distance from the current price, the indicator can react more rapidly to sudden shifts in market direction.
4. Real-Time Calculation Every time the market moves, the script re-evaluates the data points within its defined length. It sums up the weighted prices and divides them by the total weight to produce a single, clean line on your chart. The result is an orange trend line that aims to provide a more "instant" visual representation of the market's direction compared to standard moving averages.
Summary for the User
In plain terms, this script acts as a high-performance filter. It looks at the recent history of the market, decides which parts of that history are most important based on complex mathematical curves, and draws a smooth line that tries to stay ahead of the curve. For a trader, this means identifying trends faster and with less of the "delayed" visual information found in standard tools.
//+------------------------------------------------------------------+
//| 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 1
#property indicator_color1 Orange
//---- input parameters
extern int Price = 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 MABuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,MABuffer);
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;
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,Price,shift+i);
Weight =Weight + alfa;
}
if (Weight > 0) MABuffer[shift]=AvgRange/Weight;
}
return(0);
}
Comments