Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and 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. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

Simple AFL code for 5 EMA(High-Low) – Weekly Trading System

1 min read

This is a simple 5 EMA High-Low weekly trading system to identify the medium-term trend to follow till the end of the trend. Helps users to manage their positional medium-term trend bias.

Trading Rules

Timeframe : Weekly Timeframe

Buy when the candle closes above 5-EMA high period
Exit when the candle closes below 5-EMA low period

Buy and Sell Trading Logic is achieved in Amibroker with the following code block.

//Trading Logic
Buy = Cross(Close,EMA( High , par ));
Sell = Cross(EMA( Low , par ),Close);

//Remove excessive signals
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Amibroker’s AFL code had been designed to implement this system with Green Arrows in the charts indicating Buy Signal and the Red downward arrows indicating a sell signal.

/* 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);

Both the weekly and Daily charts are shown with buy/sell signals. Just like any other trend-following system 5 EMA(High-Low) also works badly when the trend is sideways and produces more mixed signals where there is a strong trend the signals are good and could generate good potential returns.

A Simple 5 EMA High-Low Weekly Trading System – Amibroker AFL Code

_SECTION_BEGIN("5 EMA High-Low");
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 ) ) ));
Plot( Close, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

par = Param("Length",5,1,200,1);


//Trading Logic
Buy = Cross(Close,EMA( High , par ));
Sell = Cross(EMA( Low , par ),Close);

//Remove excessive signals
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);


/* 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);


//Plot EMA High-Low Lines
Plot( EMA( Low,5), "EMA-LOW",ParamColor("Color1", colorBlue ),styleThick);
Plot( EMA( High,5), "EMA-High",ParamColor("Color2", colorBlue ),styleThick);
_SECTION_END();
Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and 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. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

[Webinar] Understanding and Mitigating Curve Fitting in System Trading

"Understanding and Mitigating Curve Fitting in System Trading"! This dynamic session aims to equip novice to intermediate traders, quantitative analysts, and financial engineers with...
Rajandran R
1 min read

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

This tutorial provides an overview of the P-Signal reversal strategy, a quantitative trading strategy that utilizes statistical parameters and error functions to generate probabilistic...
Rajandran R
2 min read

16 Replies to “Simple AFL code for 5 EMA(High-Low) – Weekly Trading…”

  1. Dear RajI have just modified your afl i.e.Buy = Cross(Close,EMA( Low , 5 ));Sell = Cross(EMA( High , 5 ),Close);and found more meaningful. Please do comment on this modification.

  2. Its adds more value to the trading systemin minimizing the losses and producing avalueable system.

  3. One more modification is required. Just open daily Nifty Chart and just watch 16-12-2009. it has given false signal. I do not know how to omit it but this type of false signal can be omitted.If possible then it will be great trading system.Thanks

  4. More modification done in Buy sell.Buy = Cross(Close,EMA( Low , 5 ))OR Cross(Close,EMA( High , 5 )); Sell = Cross(EMA( High , 5 ),Close)OR Cross(EMA( Low , 5 ),Close);It works fine in hourly and daily chart. I have added your earlier Buy Sell signal conditionally because if turn around happened within the band then this conditional extension is required.

  5. @ShyamWe should not try to create more buy/sell signals.As this would create a mess. The scope of creating a buy/sell signal is to identify turns earlier and to generate potential profits. If you include 5 EMA signals also then it would probably create a mess and finally ends in confusion(i.e should we have to believe this signal or not)

  6. hi thanks for afl. pls explain how to trade with help of this afl. on which scripts gives good results?

  7. Sir,

    This code is not working. Giving the error at 1st & Last line. Error 31. Syntax error, expecting ‘)’ or’,’

    Regards,

    Dipak

  8. Hello Rajendran,

    Please paste again full AFL with modification suggested by above members.

    Or I should use below code.

    =======================================================================
    _SECTION_BEGIN(“5 EMA High-Low”);
    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 ) ) ));
    Plot( Close, “Close”, ParamColor(“Color”, colorBlack ), styleNoTitle | ParamStyle(“Style”) | GetPriceStyle() );

    Buy = Cross(Close,EMA( Low , 5 ));
    Sell = Cross(EMA( High , 5 ),Close);

    PlotShapes(IIf(Sell==1, shapeDownArrow, shapeNone), colorRed, 0,High, Offset=-20);
    PlotShapes(IIf(Buy==1, shapeUpArrow , shapeNone), colorGreen, 0,Low, Offset=-20);
    Plot( EMA( Low,5), “5 EMA LOW”,ParamColor(“Color”, colorBlue ),styleNoRescale);
    Plot( EMA( High,5), “5 EMA High”,ParamColor(“Color”, colorBlue ),styleNoRescale);
    _SECTION_END();
    =======================================================================

    Thanks,
    Aliasgar

  9. Hello Rajendran,

    I want plot buy/sell signal for current day only , i dont’t want signals for previous days.

    pls help me to plot buy/sell signal only for current day.

    Awaiting for your response.

Leave a Reply

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