Skip to main content

Heikin-Ashi provides a graphical representation of market trends using a variation of the familiar Japanese candlestick charts. Essentially, Heikin-Ashi is a distinctive type of candlestick where the opening, high, low, and closing (OHLC) values are divergent from conventional candlesticks. The Japanese terms “Heikin” and “Ashi” translate to “average” and “pace”, respectively, thus Heikin-Ashi illustrates the average pace of price values. It crafts a composite candlestick by utilizing the open-close data from the previous span and the open-high-low-close data from the current span. This article unfolds a Heikin Ashi Trading System, coded in Amibroker AFL, that has been optimized for NSE:Banknifty but can be applicable to other instruments as well.

Examining price patterns using Heikin-Ashi charts is notably simpler and visually more compelling than it is with traditional candlestick charts. Refer to the following image for an illustration:


Determining Heikin-Ashi Prices

The OHLC values for Heikin-Ashi charts, which vary from conventional candlestick charts, are derived using the formula below:

  • HAClose = (Open + High + Low + Close) / 4 – Signifying the average price of the current bar
  • HAOpen = [HAOpen(Previous Bar) + HAClose(Previous Bar)] / 2 – Representing the midpoint of the preceding bar
  • HAHigh = Max(High, HAOpen, HAClose) – Denoting the peak value in the set
  • HALow = Min(Low, HAOpen, HAClose) – Indicating the minimal value in the set

Guidelines for Trading with Heikin-Ashi

The following encapsulates 5 critical rules (sourced from Investopedia) to adhere to when trading with the Heikin-Ashi method:

  • Green candles devoid of lower “shadows” signal a robust uptrend: consider letting your profits run
  • Green candles symbolize an uptrend: you might contemplate augmenting your long position, and abandoning short positions.
  • A single candle with a diminutive body flanked by upper and lower shadows conveys a potential trend shift: traders comfortable with risk might opt to buy or sell at this point, while others may await further confirmation before adjusting their positions.
  • Red candles flag a downtrend: consider enhancing your short position, and vacating long positions.
  • Red candles absent of upper shadows indicate a potent downtrend: remain short until a trend alteration materializes.

Proceed to the subsequent section to explore an AFL and backtest report for this Heikin Ashi Trading System. Explore here to grasp AFL coding and construct your own Trading systems.

Heikin Ashi Trading System – Overview of AFL

Preferred Time-frame
Indicators UsedEMA
Buy ConditionFormation of Heikin-Ashi green candle with Open=Low, and Close>40 period EMA of Close
Short ConditionFormation 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 Loss1%
TargetsNo fixed target
Position Size120 Quantities
Initial Equity200000
Brokerage50 per order

Heikin Ashi Trading System – AFL Code

//  Formula Name:    Heikin Ashi Trading System
//  Website:

_SECTION_BEGIN("Heikin Ashi Trading System");

SetOption( "InitialEquity", 200000);
SetTradeDelays( 1, 1, 1, 1 );
SetOption("FuturesMode" ,True);
SetOption( "AllowPositionShrinking", True );


__N(Title = StrFormat("{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
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 );  


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);

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



/* 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);


Preview of AFL

Heikin-Ashi AFL

Heikin Ashi Trading System – Backtesting Report

Fixed Position Size
Initial Capital200000
Final Capital1726506.44
Scrip NameNSE Banknifty
Backtest Period01-Mar-2000 to 09-Mar-2016
Net Profit %763.25%
Annual Return %13.94%
Number of Trades276
Winning Trade %25.36%
Average holding Period11.88 periods
Max consecutive losses18
Max system % drawdown-75.62%
Max Trade % drawdown-32.42%

It’s apparent that drawdowns are somewhat high. They could potentially be mitigated with apt Risk Management strategies. Download the exhaustive backtest report here.


Additional Amibroker Settings for Backtesting

Navigate to Symbol→Information, and specify the lot size and margin requirement. The following screenshot demonstrates a lot size of 40 and a margin requirement of 10% for NSE Banknifty:

Banknifty Symbol Information