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

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

How to Install the HA Supertrend Indicator
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.

What is QuantZilla?

Quantzilla is 75+ hours of code mentoring program designed for noncoders who want to learn the coding program right from designing indicators, scanners, trading...

Things to Consider While Building a Bracket Order Execution…

Building a Bracket Order Strategy is the most demanded request we got from most of the algomojo traders. This article explains the list of...

PlaceOrder – Multi Broker Exectuion Code Snippets for Amibroker

This tutorial explains how to build a button trading right from scratch and with a click of a button how you can send orders...

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

1. Yogesh says:

Dear sir , Can I have for mt4 if so send it to my mail i d

1. Hi,

Iam planning to release HA Supertrend in MT4. Will take quite some time. Stay tuned!

2. Ajay says:

Hi Rajandran

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

regards
SK

1. SK says:

I am using 5.64 .,Is that ok??

1. Then it should work and couple of people who tested are able to see the charts. What error you are getting?

1. 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. SK says:

Hi,,

3. Fernand Bernard says:

Thank.
Fernand

1. Hi,

The code done here is for Amibroker and you can expect Metatrader codes in the upcoming post. Stay tuned!

4. BHAILALBHAI says:

Sir

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

5. Amol Vaidya says:

Rajandran sir does this repaint ….. also which is better Normal or HS

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

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

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

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

1. While performing Optimization Low Level Graphic elements will create error. It is better to remove.

9. shabbir says:

can we have a scanner for HA super trend in amibroker platform

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

1. Hi,

Make sure you are familiar with testing future mode. As it is different from traditional backtesting.

11. Karan says:

Rajendran Sir,

Kindly guide as how to test the future mode.
Thanks & Regards

12. Dharmesh says:

please provide heiken ashi super trend code for mt4
thanks

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

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

14. Say says:

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?

15. Gopikrishna says:

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.

16. srijnan says:

17. Parag says:

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.

18. Jitendra Ashara says:

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.

19. Hk says:

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