AFL of the week: Support Resistance based Trading System

This is a breakout strategy where buy/sell signal is generated when price crosses a dynamic support/resistance. The calculation of support/resistance is based on highest high value and lowest low value for a particular lookback period. This strategy seems to work well in Trending markets.

Read our article on AFL tutorial here.

AFL Overview

Paramter Value
Preferred Timeframe Daily, Hourly
Indicators Used None
Buy Condition Price crosses above dynamic resistance.
Sell Condition Price crosses below dynamic support.
Stop Loss 2%
Targets No fixed target, Stop and reverse when AFL gives opposite signal
Position Size 150 (fixed)
Initial Equity 200000
Brokerage 50 per order
Margin 10%

AFL Code

//------------------------------------------------------
//
//  Formula Name:    Support Resistance based Trading System
//  Author/Uploader: Trading Tuitions
//  E-mail:          support@tradingtuitions.com
//  Website:         www.tradingtuitions.com
//-------------------

_SECTION_BEGIN("Dynamic Support Resistance");

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(150,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 ));

Plot( Close, "Price", colorWhite, styleCandle );

period=Optimize("Swing",3,1,25,1);
res=HHV(H,period); //resistance
sup=LLV(L,period); //support
DynamicPivot=IIf(ValueWhen(IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0))!=0,IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0)),1)==1,sup,res); 
//If current close breaks the resistance then dynamic pivot level would be last support, if close breaks the support then dynamic pivot level will be last resistance
Plot(DynamicPivot, "DynamicPivot", colorBlue, styleStaircase);
Buy = Cross(C,DynamicPivot) ;
Sell = Cross(DynamicPivot,C)  ;
Short = Sell;
Cover = Buy;

StopLoss=2;

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


_SECTION_END();

AFL Screenshot

Dynamic Support Resistance

 

Backtest Report

 Paramter Value
  NSE Nifty
Initial Capital  200000
Final Capital 1275626.60
Backtest Period 01-Jan-2007 to 19-02-2016
Timeframe Daily
Net Profit %  537.81%
Annual Return % 22.48%
Number of Trades 243
Winning Trade % 41.56%
Average holding Period 8.93 periods
Max consecutive losses 6
Max system % drawdown -28.78%
Max Trade % drawdown -60.73%

Download the detailed backtest report here.

Please note that you can expect even better results if you allow compounding of your returns.

Equity Curve

EquityCurve_DynamicSupRes

 

Additional Amibroker settings for backtesting

Goto Symbol–>Information, and specify the lot size and margin requirement. The below screenshot shows lot size of 75 and margin requirement of 10% for NSE Nifty:

Symbol Info_Nifty

Disclaimer:

All the AFL’s posted in this section are for learning purpose. Trading Tuitions does not necessarily own these AFL’s and we don’t have any intellectual property rights on them. We might copy useful AFL’s from public forums and post it in this section in a presentable format. The intent is not to copy anybody’s work but to share knowledge. If you find any misleading or non-reproducible content then please inform us at support@tradingtuitions.com

6 Comments

  1. Dear Sir,
    Error showing in line 36, 38, 39 & 40.
    Kindly have a look.
    And thanks for the efforts.

      • Thanks a lot. Back testing in daily & hourly tf showing great results.
        Bye the way, “The calculation of support/resistance is based on highest high value and lowest low value for a particular lookback period. ”

        What is period here? Can we change it to suit lower tf?

        • Hi Gautam,
          The default lookback period is 3 as per the below code:
          period=Optimize(“Swing”,3,1,25,1);
          You can definitely change it to suit different timeframes and symbols. For ex: Lookback period of 4 works great in BankNifty

Leave a Reply

Your email address will not be published. Required fields are marked *