Heikin-Ashi Supertrend – Amibroker AFL code

Heikin Ashi Supertrend is a Volatility based trend following indicator which uses Heinkin Ashi + Supertrend method to plot the indicator. Heikin means “average” and “ashi” means “pace”. Heikin-Ashi represents the average-pace of prices. Heikin-Ashi candlesticks is a japanese technique and quite different from the traditional candlesticks. Instead of using the open-high-low-close (OHLC) bars like standard candlestick charts, the Heikin-Ashi technique uses a modified formula:

Close = (Open+High+Low+Close)/4
Open = [Open (previous bar) + Close (previous bar)]/2
High = Max (High,Open,Close)
Low = Min (Low,Open, Close)

Traditional supertrend uses a moving (statistical) median of the arithmetic mean (High + Low)/2 of the bars instead of a moving average. The (statistical) median is known to be more robust than any average. whereas Heinkin Ashi Supertrend uses a moving (statistical) median of the arithmetic mean (HaHigh + HaLow)/2 of the bars. Sample Heinkin ashi supertrend charts are shown below.

Nifty May Futures 5min Charts
HAS Nifty

10 things you need to know about HA Supertrend

1)HA Supertrend is a carry forward strategy which involves overnight gapup/gapdown risk and also weekend carry forward risk. It is not a pure intraday strategy. And Heinkin Candlesticks are different from Normal Candlesticks.
2)HA Supertrend normally makes more profit when the volatility increases and makes lesser losses/lesser profits when the market shows compressed volatility.
3)Most of the profits are made in gap up and gap down openings compared to the losses made in gapup/gapdown
4)Should works good with High Beta Stocks and BankNifty scrip.
5)HA Supertrend has two input factors Multiplier and ATR. default value is 6 and 6. Increasing the multiplier will increase the per trade risk and decrease the profits. (Tested across all the timeframes). And lower multiplier values will results in more number of trades which means more commissions and more slippages.to pay.
6)Heinkin Ashi Trailing Stop Loss levels displayed in the Dashboard is not a real trade stop loss as Heinkin Ashi Candles are different from normal Candlestick. So One should Buy only if there is a Buy Signal and Sell only if there is a sell signal.
7)Winning ratio is more or less between 42-50% across all the timeframes. (doesn’t includes brokerages and slippages). However most of the times higher time frame involves high risk in trading supertrend indicator.
8)HA Supertrend Strategy results will be equally comparable with normal Supertrend Strategy and in very few cases HA supertrend outperform Normal Supertrend in terms of profits and risk.
9)HA Supertrend V2.0 Strategy works only with amibroker version 5.4 and higher. If you are using older version of Amibroker consider upgrading to New Version.
10)It is advisable to trade one or two symbols at a time. Avoid jumping from one stock to another which could sometimes lead to consecutive losses and even missing most profitable signals.

BankNifty May Futures 5min Charts
HAS Banknifty

How to Install the HA Supertrend Indicator
1)Download HA Supertrend AFL code.
2)Unzip HA Supertrend to local folder
3)Copy HA Supertrend AFL code ver2.afl code to c:\program files\amibroker\formulas\basic folder
5)Open Amibroker and Open a New Blank Chart
6)Goto Charts->Basic Charts and apply/drag-and-drop the HA Supertrend AFL code code into the blank chart
7)Bingo you are done. Now you will be able to see the HA Supertrend indicator with Buy and Sell signals.

Nifty and BankNifty HA Supertrend Backtest Results since March 2009

Tested with Initial Captial of Rs2,00,000 with 2 lots of Nifty and BankNifty(given seperately) and Broker Commission of Rs100 per trade leg( i.e Rs200 per Buy and Sell Transaction).

Nifty HA Supertrend backtest results

BankNifty HA Supertrend backtest results

You can Explore your own ideas with HA supertrend and Feeback invited.

Related Readings and Observations

  • AFL to Switch between Heikin-Ashi and Normal Candle A small funtionality has been added to the NMA Swing Explorer charts to switch between Heikin-Ashi Candle and Normal Candle. Now Just right click on the charts and select Parameter […]
  • Things You Need to Know About VWAP In finance terms, volume-weighted average price (VWAP) is defined as the ratio of the value traded to total volume traded over a particular time horizon (usually one day). It is a measure […]
  • Rotation Factor – Amibroker AFL Code Rotation Factor is a sentimental indicator used in Market Profile to Indicate who is control (Buyers/Sellers) in the market for the day. If the rotational factor prints positive values […]
  • Full Gap Open Range Strategy – Amibroker AFL Code Here is a simple Full Gap Open Range Strategy for intraday traders to trade the big gap ups and gap downs using open range breakout. And more important strategy is back-testable.


  1. Ajay says

    Hi Rajandran

    This code is producing error when code testing is done.. it starts from line 334 to 442 ..
    please check ASAP.


          • SK says

            ln334 Clr = IIf(sig == “BUY”, colorLime, colorRed);
            variable sig used without having being initialized

            Clr = IIf(sig == “BUY”, colorLime, colorRed);
            operation not allowed.operand mistmatch

            ln 336 ssl = IIf(bars == BarCount-1, TrendSL[BarCount-1], Ref(TrendSL, -1));
            variable bars used without being initialised

            similarly all the cariables, tar, tar1,tar2,tar3, bar, bar1 are used without being initialized..
            one thing..you can see the chart and you can see the buy and sell…but when I have to do backtest , it doesnt and points the errors

  2. John says

    Hi Rajendran,

    I am curious if choosing Heiken Ashi over Moving Average adds any benefit to your indicator? More winners, less drawdown etc. What is your experience.
    Thanks again for all your hard work

  3. Sachin L says


    this code is producing errors as SK has said above. I am using 5.60 version of Amibroker. Please help.


  4. Detonator says

    Works best for me on H1, the target code gets errors during optimization of Factor and ATR period, so I had to remove it, aside from that great job!

  5. Karan says

    Rajendran Sir,
    How to do Backtest to check the Past performance.
    In my Amibroker, when I do the Back Test, it shows 0 rows and no Trades.
    Kindly help and guide the required settings to do the Back Testing.

  6. Vivek says

    Hello Sir,

    Getting the following error ” Variable ‘sig’ used without being initialised”

    Ple let me know how to correct this syntax.

    thanks, Vivek

    • says

      Hi try to use amibroker 5.5 above and also make sure that you have enough back fill

      for 5min charts min backfill should be atleast 10-15 days of backfill and for hourly you need atleast 2 month of backfill data if you are missing
      out anything it results in error

      Rajandran R

  7. Say says


    Just wanted to say thanks for all the time and effort you are taking to educate the viewers. I have been reading your website and it takes a heart of gold for a man to disclose such trading systems.

    I have one small clarification regarding the Heikein Ashi AFL. I kindly request you to answer:

    According to HA Super Trend, the trailing S/L is mere display and one should only buy when there is buy signal and sell when there is sell signal.

    This is what i understand:

    Say, we took a position, example: Long and market went against us and when signal reverts to sell, we close the positions and shift to sell.

    For profit booking, do we book one lot at 1st target line and second lot at subsequent signal (or) both the lots at the subsequent signal?

Leave a Reply

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