Skip to main content

This profitable trading strategy utilizes the power of MACD, RSI, Stochastic, and Exponential Moving Average (EMA). The combination of these indicators aims to secure consistent returns while minimizing market fluctuations. In the chart, upward trends are depicted in blue, while downward trends are shown in red.

For an in-depth understanding of coding and the opportunity to create your personalized trading systems, please explore here!

Also Read: Decoding Amibroker Backtest Reports

AFL Overview

ParameterValue
Preferred TimeframeDaily, Hourly
Indicators UsedMACD, Stochastic, RSI, EMA
Buy ConditionRSI period 3 is greater than 50, the difference between MACD and MACD signal line is greater than 0 and also greater than that of the previous candle, the Stochastic %D line is less than 80 and greater than that of the previous candle, and Closing price greater than EMA period 100.
Sell ConditionRSI period 3 is less than 50, the difference between MACD and MACD signal line is less than 0 and also less than that of the previous candle, the Stochastic %D line is greater than 20 and less than that of the previous candle, Closing price less than EMA period 100.
Stop Loss2.5%
TargetsNo fixed target, Stop and reverse when AFL gives the opposite signal
Position Size300 (fixed)
Initial Equity$200,000
Brokerage$50 per order
Margin10%

AFL Code

				
					//  Formula Name:    Trend following trading System
//  Website:         zerobrokerageclub.com
//------------------------------------------------------

_SECTION_BEGIN("Trend following Trading system");

SetTradeDelays( 1, 1, 1, 1 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",50);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(300,spsShares);
SetOption( "AllowPositionShrinking", True );
BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

SD = StochD(8, 3, 3);
MH = MACD(8, 21) - Signal(8, 21, 5);
trendup = IIf(MH > 0 OR (MH > 0 AND MH > Ref(MH, -1)) AND RSI(3) >50 AND SD < 80 AND SD > Ref(SD, -1) AND ValueWhen(C,O<C), colorBlue, colorWhite);
trendcolor = IIf(MH < 0 OR (MH < 0 AND MH < Ref(MH, -1)) AND RSI(3) <50 AND SD > 20 AND SD < Ref(SD, -1) AND ValueWhen(C,O>C), colorRed, trendup);
EMA20=EMA(Close,100);

Buy=MH > 0 OR (MH > 0 AND MH > Ref(MH, -1)) AND RSI(3) >50 AND SD < 80 AND SD > Ref(SD, -1) AND ValueWhen(C,O<C); 
Sell=MH < 0 OR (MH < 0 AND MH < Ref(MH, -1)) AND RSI(3) <50 AND SD > 20 AND SD < Ref(SD, -1) AND ValueWhen(C,O>C);
Buy = Buy AND Close>EMA20;
Sell=Sell AND Close<EMA20;
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short = Sell;
Cover = Buy;

StopLoss=2.5;
ApplyStop(Type=0,Mode=1,Amount=StopLoss);

/* 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(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, 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);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);


Plot( C, "Close", trendcolor, styleCandle | styleThick );
Plot(EMA20,"EMA",colorBlue);

_SECTION_END();

				
			

Trading Performance Summary

Trend Following AFL

ParameterValue
AssetNSE BankNifty
Initial Capital$200,000
Final Capital$5,370,094.50
Backtest PeriodJune 9, 2000 to February 26, 2016
TimeframeDaily
Net Profit Percentage2585.05%
Annual Return Percentage22.58%
Number of Trades110
Winning Trade Percentage28.18%
Average Holding Period30.73 periods
Max Consecutive Losses12
Max System Percentage Drawdown-56.04%
Max Trade Percentage Drawdown-62.51%

You can download the detailed backtest report here.

Please note that if you allow for the compounding of your returns, you may achieve even better results.

Equity Curve Trend Following AFL

Also Read: Excel-Based Moving Average Trading System

Additional Amibroker Settings for Backtesting

To specify lot size and margin requirements, navigate to Symbol → Information in Amibroker. Below is a screenshot illustrating a lot size of 30 and a margin requirement of 10% for Bank Nifty:

Symbol Info_Banknifty

Leave a Reply