Author: www.forex-tsd.com
NonLagALMA

This script is designed to provide a smooth, responsive trend line on your trading chart, specifically engineered to balance two often-conflicting goals in technical analysis: reducing market "noise" while minimizing the time delay (lag) that usually accompanies smoothing.

Core Logic and Functionality

The indicator works by calculating a specialized moving average known as an ALMA (Adjusted Moving Average). Unlike standard moving averages that treat all past data points with equal weight or simple linear weighting, this script uses a sophisticated mathematical approach to determine which price points are the most relevant for predicting current movement.

1. Weight Distribution (The Gaussian Curve) Instead of looking at the price history as a flat line, the script applies a "weighting" system. It treats the most recent data points as highly significant and gradually reduces the influence of older data as it moves further back in time. This is achieved using a mathematical formula that creates a bell-shaped distribution (similar to a Gaussian curve). This ensures that the indicator reacts quickly to new price action while remaining shielded from insignificant, minor fluctuations.

2. Customization Parameters The logic relies on four main "knobs" that allow you to fine-tune how the indicator behaves:

  • Period: Determines the total window of time (number of bars) the indicator looks back at to calculate its value.
  • Sigma: Controls the "smoothness." A higher sigma spreads out the weight, making the line more fluid but potentially slower; a lower sigma makes it more reactive but potentially "choppier."
  • Sample: Adjusts the center of the weight distribution, influencing how aggressively the indicator prioritizes the very latest price movements.
  • Price Type: Allows you to choose which specific price data (such as the closing price) should be used as the foundation for the calculation.

3. Multi-Layered Processing The script is built to be highly precise because it doesn't just pull raw price data directly. It interacts with another specialized component (a "NonLagMA" calculation) to pre-filter the data before applying its own smoothing logic. This "layered" approach means the final line you see on your chart has been filtered twice: first to remove instant erratic noise, and second to ensure the resulting trend line follows the price movement with as little delay as possible.

Summary for the Trader

In practical terms, this tool is designed to show you the "true" direction of the market. By filtering out the distractions of small price wiggles and using a weighted curve to stay updated on new information, it provides a steady visual guide that helps you identify trends more clearly than a standard moving average might.

16 Views
0 Downloads
0 Favorites
NonLagALMA
//+------------------------------------------------------------------+
//|                                                   NonLagALMA.mq4 |
//| upgraded by STS                              converted by mladen |
//|                                               mladenfx@gmail.com |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 DeepSkyBlue  
#property indicator_width1 2

extern int    AlmaPeriod = 9;
extern double AlmaSigma  = 6.0;
extern double AlmaSample = 0.1618;
extern int    AlmaPrice  = PRICE_CLOSE;

double Alma[];
double coeffs[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
{
   SetIndexBuffer(0,Alma);
   
      AlmaPeriod = MathMax(AlmaPeriod,1);
      ArrayResize(coeffs,AlmaPeriod);
      
         double m = MathFloor(AlmaSample * (AlmaPeriod - 1.0));
         double s = AlmaPeriod/AlmaSigma;
         for (int i=0; i<AlmaPeriod; i++)
            coeffs[i] = MathExp(-((i-m)*(i-m))/(2.0*s*s));

   IndicatorShortName("Alma ("+AlmaPeriod+","+DoubleToStr(AlmaSigma,2)+","+DoubleToStr(AlmaSample,2)+")");
   return(0);
}
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars = IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);

	for(i = limit; i >= 0; i--)
   {
      double sum=0;
      double div=0;
      for (int k=0; k<AlmaPeriod && (k+i)<Bars; k++)
      {
         //sum += coeffs[k]*iMA(NULL,0,1,0,MODE_SMA,AlmaPrice,i+k);
         sum += coeffs[k]*iCustom(NULL,0,"NonLagMA_v7.1",AlmaPrice,1,0,0.0,0,1,0.0,0,0,0,i+k);
         div += coeffs[k];
      }
      
      if (div!=0)
            Alma[i] = sum/div;
      else  Alma[i] = 0;       
   }
   return(0);
}

Comments