Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Heikin-Ashi Supertrend – Amibroker AFL code

2 min read

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.

Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

AllinOneAlerts – Amibroker Alerts Module for Amibroker Users

AllinOneAlerts is one of the most important and most requested modules to bring all sorts of alerts with no extra coding efforts. Now even...
Rajandran R
5 min read

How to improve your Amibroker Algo Trading Execution Speed

When comes to Automated Trading speed of execution plays a dominant role in executing at better prices. Here I would like to list three...
Rajandran R
1 min read

Introduction to QuantZilla 2.0

QuantZilla is a 75+ hours of immersive coding mentor-ship program on designing trading systems, converting trading ideas into indicators and trading strategies, automating the...
Rajandran R
4 min read

30 Replies to “Heikin-Ashi Supertrend – Amibroker AFL code”

  1. Hi Rajandran

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


          1. 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. I would like to download the HA supertrend AFL code on my meta trader platform.

  3. Sir

    Which is better ? Super trend indicator with normal candle or Heiken ashi Candle ?

    1. I prefer normal. Some parameters in Heikin Ashi outperforms. However there is no drastic change in performance compared to normal supertrend.

  4. 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

  5. Sir,

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


  6. 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!

  7. 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.

  8. Hello Sir,

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

    Ple let me know how to correct this syntax.

    thanks, Vivek

    1. 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

  9. Rajendran,

    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?

  10. Thanks Rajendran for the HA Supertrend AFL.
    This indicator is a nice way of confirming whether we are trading the direction of major trend.
    Thanks once again.

  11. i want to install mt4,of indian stocks.would u please help me to do so. Thanks.

  12. Hi Rajendran,
    I am trying to automate the HA Supertrend to buy/sell on signal. Please can you advise on in which section of the code should the order placing code should be written.

  13. I need Heiken Ashi indicator on my Deshboard bullish or bearish in diff time frame is it possible to devlop amibroker afl or mt4 indicator.

  14. Hello,

    Code has many errors from line 334 to 442.. It all starts with Variable Sig being not initialised… Many people have requested correction on the same.. I am using latest version of Amibroker…still the issues is persistent

Leave a Reply

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