Skip to main content

Arnaud Legoux Moving Average for Amibroker (AFL)

vivekjain over 15 years ago Amibroker (AFL)

  • Rating:
    4 / 5 (Votes 4)
  • Tags:
    amibroker, moving average

ALMA (Arnaud Legoux Moving Average) is an Free Open Source Zero-phase Digital filter formula which has been created by Arnaud Legoux and Dimitrios Kouzis-Loukas. It uses a Gaussian distribution shifted with an offset so that it’s not evenly centered on the window but biased towards the more recent days.

The ALMA is a Moving Average which performs better than the HMA. ALMA is inspired by the Gaussian Filters and it attacks a fundamental assumption of the Moving Averages we described before.

Indicator / Formula

Copy & Paste Friendly
function ALMA(priceField, windowSize, sigma, Offset)
{
	m = floor(Offset * (windowSize - 1));
	s = windowSize / sigma;

	w = 0;
	wSum = 0;

	for(i = 1; i < windowSize; i++)
	{
		w[i] = exp(-((i-m)*(i-m))/(s*s)); // 2 should be there?
		wSum += w[i];
	}

	for(i = 1; i < windowSize; i++)
	{
		w[i] = w[i] / wSum;
	}

	outalma = Null;

	for(j = 0; j < BarCount; j++)
	{
		alSum = 0;

		if(j < windowSize)
		{
			outalma[j] = Null;
		}
		else
		{
			for(i = 1; i < windowSize; i++)
			{
				alSum += priceField[j - (windowSize - 1 - i)] * w[i];
			}

			outalma[j] = alSum;
		}
	}
	
	return outalma;
}

p = ParamField("Price Field");
windowSize = Param("Window Size", 9, 5, 201, 2);
sigma = Param("Sigma", 6, 1, 20);
Offset = Param("Offset", 0.85, 0.05, 1.0, 0.05);

Plot(ALMA(p, windowSize, sigma, Offset), "ALMA", ParamColor("Color",
colorBlue), ParamStyle("Style"));

5 comments

1. rmike
over 15 years ago

vivekjain,

It says a lot about your ethics that you did not feel the need to acknowledge the original coder of this AFL. However, this particular implementation of ALMA in amibroker is not quite accurate. The correct accurate implementation of ALMA in conformance with Ninja Trader and Meta Trader releases by the original developers of ALMA i.e Arnaud & Legoux has been submitted by me so that amibroker users can also benefit.

about 15 years ago

ALMA can be coded using the FIR function. By accident I bumped into this writing:

http://www.amibroker.com/guide/afl/fir.html

which does not seem to pop up when you look in the manual. FIR allows you to do a convolution of an input array with a some smaller function like a Gaussian window type function. resulting code:

windowSize=Param("Window Size",9,5,201,2);
sigma=Param("Sigma",6,1,20);
Offset=Param("Offset",0.85,0.05,1.0,0.05);
 
function ALMA_AFL(input,range,Offset,sigma) 
{ 
	local m,im,s,Coeff; 
	m=floor(Offset*(range-1));
	s=range/sigma; 
	
	for(i=0;i<Min(range,BarCount); i++ )
	{ 
		im=i-m;
		Coeff[i]=exp(-(im*im)/(2*s*s));
	} 
	return FIR(input,Coeff,range); 
}

rr=ALMA_AFL(C,windowSize,Offset,sigma);

SetChartOptions(0, chartShowDates);
Plot(C, "Close",colorLightGrey,styleCandle);
Plot(rr,"",colorBlue,1);

about 15 years ago

in fact it can be programmed using:

ws=Param("Window Size",9,5,201,2);
sigma=Param("Sigma", 6, 1, 20,1);
Offset=Param("Offset",0.85,0,1.0,0.05);
bi=BarIndex();

m=floor(Offset*(ws-1));
s=ws/sigma;
window=IIf(bi<ws,(Cum(1)-1)-m,0);
window=IIf(bi<ws,exp(-(window^2)/(2*s^2)),0);
rr=FIR (C,window,ws); 

SetChartOptions(0, chartShowDates);
Plot(C,"Close",colorLightGrey,styleCandle);
Plot(rr,"ALMA",colorBlue,1);

Leave Comment

Please login here to leave a comment.