Heikin Ashi Trading System : Amibroker AFL

Heikin-Ashi is a trend visualization technique based on Japanese candlestick charting. In-fact, Heikin-Ashi is also a type of candlestick, whose OHLC value differs from the traditional candlesticks. In Japanese, Heikin means “average” and “ashi” means “pace” . Taken together, Heikin-Ashi represents the average-pace of prices. Heikin-Ashi Candlesticks use the open-close data from the prior period and the open-high-low-close data from the current period to create a combo candlestick. In this post we would reveal a Heikin Ashi Trading System coded in Amibroker AFL. This system is optimized for NSE:Banknifty but should work well for other instruments too.

Analyzing price patterns in Heikin-Ashi charts is far more simpler and visually appealing as compared to traditional candlestick charts. See the below image:

Comparison

Image Source- Dan Valcu:CFTe

Calculating Heikin-Ashi prices

As discussed above, OHLC value for Heikin-Ashi charts differs from the traditional candlestick charts. It is calculated using the below formula:

  • HAClose = (Open+High+Low+Close)/4
    o Average price of the current bar
  • HAOpen = [HAOpen(Previous Bar) + HAClose(Previous Bar)]/2
    o Midpoint of the previous bar
  • HAHigh = Max(High, HAOpen, HAClose)
    o Highest value in the set
  • HALow = Min(Low, HAOpen, HAClose)
    o Lowest value in the set

Heikin-Ashi Trading rules

Below are the 5 important rules (from Investopedia) which should be followed while trading with Heikin-Ashi technique:

  • Green candles with no lower “shadows” indicate a strong uptrend: let your profits ride
  • Green candles signify an uptrend: you might want to add to your long position, and exit short positions.
  • One candle with a small body surrounded by upper and lower shadows indicates a trend change: risk-loving traders might buy or sell here, while others will wait for confirmation before going short or long.
  • Red candles indicate a downtrend: you might want to add to your short position, and exit long positions.
  • Red candles with no higher shadows identify a strong downtrend: stay short until there’s a change in trend.

In the next section,  we’ll go through an AFL and backtest report for this Heikin Ashi Trading System. Read our article on AFL tutorial here.

Heikin Ashi Trading System – AFL Overview

Parameter Value
Preferred Time-frame
Daily
Indicators Used EMA
Buy Condition Formation of Heikin-Ashi green candle with Open=Low, and Close>40 period EMA of Close
Short Condition Formation of Heikin-Ashi red candle with Open=High, and Close<40 period EMA of Close
Sell Condition
  • Same as Short
  • Stop Loss Hit
  • Close<40 period EMA of Close
Cover Condition
  • Same as Buy
  • Stop Loss Hit
  • Close>40 period EMA of Close
Stop Loss 1%
Targets No fixed target
Position Size 120 Quantities
Initial Equity 200000
Brokerage 50 per order
Margin 10%

Heikin Ashi Trading System – AFL Code

//------------------------------------------------------
//
//  Formula Name:    Heikin Ashi Trading System
//  Author/Uploader: Trading Tuitions
//  E-mail:          support@tradingtuitions.com
//  Website:         www.tradingtuitions.com
//------------------------------------------------------


_SECTION_BEGIN("Heikin Ashi Trading System");

SetOption( "InitialEquity", 200000);
SetTradeDelays( 1, 1, 1, 1 );
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",50);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(120,spsShares);
SetOption( "AllowPositionShrinking", True );

BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
 
HaClose = (O + H + L + C)/4; 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) ); 

barcolor = IIf(HaClose >= HaOpen,colorGreen,colorRed);

PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", barcolor, styleCandle );

printf("\nHaOpen : " + HaOpen );  
printf("\nHaHigh : " + HaHigh );  
printf("\nHaLow : " + HaLow );  
printf("\nHaClose : " + HaClose );  

Candles=param("Candles",1,1,5,1);
periods=param("periods",40,10,200,10);

Buy = Sum(HaClose >= HaOpen,Candles)==Candles AND HaOpen==HaLow AND C>EMA(C,periods);
Short= Sum(HaClose <= HaOpen,Candles)==Candles AND HaOpen=HaHigh AND C<EMA(C,periods);
Sell=Short OR C<EMA(C,periods);
Cover=Buy OR C>EMA(C,periods);

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);

printf("\nBuy : " + Buy );  
printf("\nSell : " + Sell );  
printf("\nShort : " + Short );  
printf("\nCover : " + Cover );  

Stoploss=param("SL",1,1,5,1);

ApplyStop(Type=0,Mode=1,Amount=StopLoss);


/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);


_SECTION_END();

AFL Screenshot

Heikin Ashi AFL

Heikin Ashi Trading System – Backtest Report

Paramter Value
Fixed Position Size
Initial Capital 200000
Final Capital 1726506.44
Scrip Name NSE Banknifty
Backtest Period 01-Mar-2000 to 09-Mar-2016
Timeframe Daily
Net Profit % 763.25%
Annual Return % 13.94%
Number of Trades 276
Winning Trade % 25.36%
Average holding Period 11.88 periods
Max consecutive losses 18
Max system % drawdown -75.62%
Max Trade % drawdown -32.42%

Clearly, drawdown is on a higher side. It could be overcome with proper Risk Management strategies. Download the detailed backtest report here.

Equity Curve

Heikin Ashi Equity Curve

Additional Amibroker settings for backtesting

Goto Symbol–>Information, and specify the lot size and margin requirement. The below screenshot shows lot size of 40 and margin requirement of 10% for NSE Banknifty:

Banknifty Symbol Information

Disclaimer:

All the AFL’s posted in this section are for learning purpose. Trading Tuitions does not necessarily own these AFL’s and we don’t have any intellectual property rights on them. We might copy useful AFL’s from public forums and post it in this section in a presentable format. The intent is not to copy anybody’s work but to share knowledge. If you find any misleading or non-reproducible content then please inform us at support@tradingtuitions.com

14 Comments

    • Hi Saurabh,

      Sorry this setup is exclusively for Amibroker. You may use Pi to Ami bridge to automate this strategy. Also you may post this question at http://tradingqna.com/ to get advice from Zerodha experts.

  1. Thanks for this system. Please post some other system for bank nifty intraday with low drawdown.

  2. Dear Sir,
    Many thanks & this Japanes techqnique explained very well.
    Theory & Explanation on how to use removed lots of confusion on HA approach. Your website & Your team’s positive spirit is also very good

  3. Please explain me these:
    Max system % drawdown -75.62%
    Max Trade % drawdown -32.42%

    Thanks

  4. Dear Sir, I have seen many of your postings, what i noticed is none of your afl giving winning percentage of 50% and above! Please clarify me, is it worth to follow a strategy which is giving 70 losses and 30 wins?

    And also please clarify what is the maximum winning percentage one can expect from a strategy?

    Thanks.

  5. As far as practical examples go, this site is EASILY one of the best sources of AFL knowledge on the web. I’m only a novice trader (I’m brazilian and trading local stocks) but the effort put into this blog has certainly helped immensely to surmount the steep learning curve existing in trading system development and to grasp the basics of backtesting. Congratulations and keep up the fantastic effort!

Leave a Reply

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