Amibroker AFL- Bank Nifty Support and Resistance

This article shows a step-wise example about how to design a strategy for trading in support and resistance levels.


Step 1: The Trading Idea

We pick the idea from a previous article:

Here is you first trading strategy trading strategy! Buy in a uptrend when prices retraces to a support level

Step 2: Selecting Technical Indicators

The trading idea requires quantifying key concepts: uptrend, support and lower time frame. There can be a million ways to quantify, and similarly, each way has different impact on the profitability of strategy. For this example we will use most common indicators:

Chart Time Frame: 15 minutes

Uptrend: defined when EMA(20) is above EMA(200). This is because most people use Moving Average Crossover for analysis/trading. 20 period EMA is used for short-term trend, while 200 period EMA is used for long-term trend. Downtrend is defined as opposite of uptrend.

Support: EMA with 200 periods is used to find dynamic support/resistance level. This is because most people use 200/100/50/20 period moving averages for trade decisions. More clearly, confirmation of support level is defined as candle close above EMA(200) from below. Resistance is defined as opposite of support.

StopLoss: Lowest value of the last five closing candles when Buy is triggered.

Other AFLs to calculate Support & Resistance:

Step 3: Defining Clear Strategy Rules

Buy: when prices are in uptrend and crossing above support

Sell: when prices go in downtrend or stop loss condition is met

Step 4: AFL Coding Guide

Once the logic is clear, we code the AFL in same manner. Since AFL is an array based language, it is best practice to code the Buy/Sell conditions in a loop. To find out the profits in number of points, we use the function SetPositionSize(1,spsShares);

strategy backtest- support in uptrend

Step 5: The Backtest

Thankfully and intuitively, the strategy gives a profit of Rs. 4,67,301 on Bank Nifty one lot (first month futures).  It however has a high percentage of losing trades (76%) because a large number of trades are stopped at initial small stop loss.  Related Article: Basics in Strategy Backtesting

  • All trades executed at Close price of the bar on which signal is triggered
  • Brokerage: 0.01% of Trade Value
  • Time Frame: 15 minute
  • Data History: 01-01-2014 to 31-12-2015 (two years)
  • Strategy Optimization: None

Step 6: Further Improvement

We leave it to the readers to suggest any improvement  in Step 2 and Step 3 which can increase the profitability. Particularly by modifying the stop loss condition, we can drastically improve the percentage of profitable trades.

Download Amibroker AFL  Strategy

Click here to download the editable Amibroker AFL Strategy