Gamestudio Links
Zorro Links
Newest Posts
ZorroGPT
by TipmyPip. 10/25/26 03:42
Chat Room for Zorro Programmers
by Haitao_Ren. 10/25/26 03:14
Trading Journey
by Haitao_Ren. 10/25/26 03:14
Algorithmic trading course with Zorro
by Haitao_Ren. 10/25/26 03:12
Zorro tutorial ideas?
by Haitao_Ren. 10/25/26 02:47
Zorro plugin for Hyperliquid.xyz
by mayarik. 10/24/26 09:14
Tradestation missing in table of contents
by AndrewAMD. 10/23/26 14:35
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
3 registered members (TipmyPip, JeyKey II, 3run), 7,722 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Haitao_Ren, JimSimons, julienp, julienper, KelleyAbbey
19247 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
ALMA, Arnaud Legoux Moving Average for Zorro #424934
06/24/13 14:55
06/24/13 14:55

A
acidburn OP
Unregistered
acidburn OP
Unregistered
A



Two key features of moving averages are smoothness and responsiveness. Unfortunately those are conflicting requirements. EMA is more responsive than SMA, but less smooth. HMA (Hull MA) is even faster, but even less smooth and also has the problem of overshooting.

Below you can find the implementation of the Arnaud Legoux Moving Average aka ALMA. It is inspired by the Gaussian Filters, and has even better characteristics than HMA, because it has no overshoot and reacts faster at the most important moments, when trend is about to change. You can see this characteristics easily on the attached chart that compares these 4 popular MA's. If MA's are part of your trading strategies, I hope you'll be pleasanty surprised with ALMA.

jcl, it would be nice to see ALMA included in the next Zorro version. You have my permission to include it, and remove copyright notices so as not to clutter the includes (if it ends there). Just a few remarks:

- values 6 & 0.85 for sigma and offset parameters respectively, are recommended by the original ALMA inventor and work well, IMHO. It would be nice if those parameters could be optional and then if not provided by the scripts writer, that those defaults are used. If you know how to program it, I'm not yet at that stage. wink

- weight factors are recalculated on every bar, although they're the same for every function call (if parameters stay the same). I guess if more performant ALMA is desired in the future, that could be done, but the source wouldn't be so clean and short. And Zorro is plenty fast, anyway, right? wink

- put somewhere in the documentation that the Period should be an odd number for the best results

- I wouldn't mind code inspection and commentary, 'cause I'm still learning programming in Lite-C & Zorro



Code:
#define CYAN 0x00FFFF

// ALMA, Arnaud Legoux Moving Average for Zorro Trader
// © 2013 acidburn @ Zorro Trader Forum
// v1.0 - 2013-06-24 - initial release
var ALMA(var *Data, int Period, int sigma, var offset)
{
	var m = floor(offset * (Period - 1));
	var s = Period / sigma;
	var alma, wSum;
	int i;

	for (i = 0; i < Period; i++) {
		var w = exp(-((i - m) * (i - m)) / (2 * s * s));
		alma += Data[Period - 1 - i] * w;
		wSum += w;
	}
	
	return alma / wSum;
}

// compare various moving averages
function run()
{
	set(PLOTNOW|PLOTPRICE);
	StartDate = 20130101;
	EndDate = 20130615;
	BarPeriod = 1440;
	LookBack = 100;

	vars Close = series(priceClose());
	vars MA0 = series(SMA(Close, 21));
	vars MA1 = series(EMA(Close, 21));
	vars MA2 = series(HMA(Close, 21));
	vars MA3 = series(ALMA(Close, 21, 6, 0.85));

	plot("SMA21", MA0[0], 0, RED);
	plot("EMA21", MA1[0], 0, GREEN);
	plot("HMA21", MA2[0], 0, BLUE);
	plot("ALMA21", MA3[0], 0, CYAN);
}



Original documentation: http://www.perltrader.com/zorro/ALMA-Arnaud-Legoux-Moving-Average.pdf

Last edited by acidburn; 06/24/13 14:59. Reason: added link to PDF
Re: ALMA, Arnaud Legoux Moving Average for Zorro [Re: ] #424937
06/24/13 15:09
06/24/13 15:09
Joined: Jul 2000
Posts: 28,142
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,142
Frankfurt
Thanks! I'll look through it and will add it to the next Zorro version. Default values can be realized with an overloaded function:

Code:
var ALMA(var *Data, int Period, int sigma, var offset)
{
  if(sigma == 0) sigma = 6;
  if(offset == 0.) offset = 0.85;
  ...
}

var ALMA(var *Data, int Period) { return ALMA(Data,Period,0,0.); }


Re: ALMA, Arnaud Legoux Moving Average for Zorro [Re: jcl] #424943
06/24/13 15:56
06/24/13 15:56

A
acidburn OP
Unregistered
acidburn OP
Unregistered
A



Originally Posted By: jcl
Thanks! I'll look through it and will add it to the next Zorro version. Default values can be realized with an overloaded function:

Code:
var ALMA(var *Data, int Period, int sigma, var offset)
{
  if(sigma == 0) sigma = 6;
  if(offset == 0.) offset = 0.85;
  ...
}

var ALMA(var *Data, int Period) { return ALMA(Data,Period,0,0.); }



Oh, it's really very simple. Thanks!


Moderated by  Petra 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1