Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

ALMA Long Only Trading System – Amibroker AFL Code

1 min read

ALMA (Arnaud Legoux Moving Average) is a technical indicator designed to smooth out the price data. It reduces lag and improves smoothness compared to traditional moving averages. ALMA applies a Gaussian filter to give more weight to the middle of the price window, which can be adjusted to favor recent prices more heavily.

ALMA can be considered a type of FIR filter (Finite Impulse response filter) because it applies a series of weights to a set of data points to produce a smoothed value. The Gaussian distribution used in ALMA shares conceptual similarities with the coefficients used in FIR filters to determine the output.

ALMA Input Parameters

Parameters:

Window Size (N): Defines how many periods back the average will consider.

Sigma (σ): Determines the width of the Gaussian curve (how quickly the weights decrease).

Offset (M): Shifts the highest weight point from the center. It’s usually set to a value slightly less than (N−1)/2 to compensate for the non-symmetric nature of stock prices.

ALMA Long Only Trading Logic

Long Entry: If ALMA moving average changes from declining mode to inclining mode and RSI(14)>50 and Close Price should be above EMA 20.

Long Exit: If ALMA moving average changes from inclining mode to declining mode.

ALMA Trading System – Amibroker AFL Code

_SECTION_BEGIN("Alma Trend Reveral Trading System");

//input parameter controls
windowsize = param("Window Size",5, 5, 30 , 1);
sigma = param("Sigma",2,1,10,1);
offset = Param("Offset",0.85,0.05,1,0.5);



//calculating alma

//Declaring the Function
function alma(source, windowsize, offset, sigma)
{

	local m , im, s, coeff;
	m = floor(offset * (windowsize - 1));
	s = windowsize/sigma;
	
	
	for(i=0;i<Min(windowsize,BarCount);i++)
	{
	
	im = i-m;
	coeff[i] = exp(-(im*im)/(2*s*s));
		
	}

	result = FIR(source,coeff,windowsize);


	return result;

}

//calling the function
mov = alma(Close,windowsize,offset,sigma);

color = IIf(mov > Ref(mov,-1), colorGreen, colorRed);

//plotting alma
Plot(mov,"Alma",color,styleThick);


//trading logic
Buy = mov > Ref(mov,-1) AND Close > EMA(Close,20) AND RSI()>50;
Sell = mov < Ref(mov,-1);

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);


//execution logic
BuyPrice = ValueWhen(Buy,Close);
SellPrice = ValueWhen(Sell,Close);

SetPositionSize(1*RoundLotSize,spsShares);


//plotting trading signals

_SECTION_BEGIN("Trading Signals");


//Plot the trading signals

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);



_SECTION_END();


_SECTION_END();


_SECTION_BEGIN("Candlestick Charts with Date & Time Axis");

//Enable the Date & Time Axis
SetChartOptions(0, chartShowArrows | chartShowDates);

//Plotting Candlestick charts
Plot(Close,"Candle",colorDefault,styleCandle);


_SECTION_END();

Backtesting Metrics

Backtesting Period : Jan 2011 to Nov 2023

Trading Instrument: Nifty Futures

Position Size: 1 Lot

Trade Direction: Long Only

Timeframe : Daily

Equity Curve

Equity Curve

Profit Table

Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

One Reply to “ALMA Long Only Trading System – Amibroker AFL Code”

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More