Alma
Download scriptThis script is designed to create a visual "trend-following" line on your trading charts. It utilizes the Arnaud Legoux-Thévenot Adaptive Moving Average (ALMA), which is engineered to provide a smoother representation of price movement while significantly reducing the "lag" typically found in standard moving averages.
How the Logic Works
To understand how this tool functions, it helps to think of it as a sophisticated filtering system that balances three main objectives: smoothness, responsiveness, and trend accuracy.
1. The Weighting System (The "Gaussian" Influence) Unlike a basic moving average where every price point in a period carries equal weight, this script uses a Gaussian distribution logic. It assigns higher importance to the most relevant data points and less importance to those further away. This ensures that the resulting line doesn't jump erratically due to minor market noise but remains anchored to the core price action.
2. Dynamic Adaptability The script calculates its values based on three primary user-adjustable parameters:
- Period: Determines how far back into the past the calculation looks to identify the trend.
- Sigma: Controls the "smoothness" of the line. A higher value results in a curvier, more stable line that ignores small price flickers, while a lower value makes it more sensitive to immediate price changes.
- Sample Size: This dictates how much the average "leans" toward the most recent data. It helps the indicator stay close to the current price without becoming too jagged.
3. The Calculation Process Every time the chart updates, the script performs the following steps:
- It looks at the Simple Moving Average (SMA) of your chosen price type (e.g., Closing Price).
- It applies a specific set of "coefficients" (mathematical weights) to those averages based on the Period and Sigma settings.
- It sums these weighted values together and divides them by a total divisor to produce a single, clean coordinate point.
- This process repeats for every bar on your chart, creating a continuous, flowing line that highlights the underlying momentum of the market.
Summary for Traders
The primary goal of this logic is to help you visualize the "true" trend of an asset. By filtering out the visual noise of minor fluctuations through weighted averages, it allows traders to see the direction of the market more clearly, making it easier to identify whether a price movement is a significant shift in trend or just temporary volatility.
//+------------------------------------------------------------------+
//| alma.mq4 |
//| 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 = 14;
extern double AlmaSigma = 6.0;
extern double AlmaSample = 0.5;
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);
div += coeffs[k];
}
//
//
//
//
//
if (div!=0)
Alma[i] = sum/div;
else Alma[i] = 0;
}
return(0);
}
Comments