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

Comments

  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.

    regards
    SK

          • 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
    John

  3. Sachin L says

    Sir,

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

    Regards

  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.

Leave a Reply

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