Edgoro
Trend indicator

Heikin Ashi candles: formula, trend reading and Pine Script v6 code

How Heikin Ashi candles average price to smooth trends, how traders read candle colour and wicks, the backtesting trap, and a free Heikin Ashi trend script.

What it measures

Heikin Ashi, Japanese for "average bar", is a way of drawing candles that blends each bar with the one before it. The candles are smoother than regular ones: trends show up as long runs of one colour, often with no wick on one side, and small pullbacks that would print an opposite candle on a normal chart are absorbed.

It's a charting technique more than an indicator, but it's commonly used as one: the colour of the Heikin Ashi candle becomes a trend filter, and a change of colour becomes a signal.

The formula in words

  1. HA close = (open + high + low + close) ÷ 4 of the regular bar.
  2. HA open = (previous HA open + previous HA close) ÷ 2. The first bar uses (open + close) ÷ 2.
  3. HA high = the highest of the regular high, the HA open and the HA close.
  4. HA low = the lowest of the regular low, the HA open and the HA close.

Because the HA open depends on the previous HA candle, every candle carries some of the history before it. That's what smooths the chart, and it's also why the HA open and close are not prices that actually traded.

How traders read it

  • Colour runs. A series of green candles describes an uptrend, red a downtrend. Many traders hold longs only while candles stay green.
  • Missing wicks. A green candle with no lower wick (the HA low equals the HA open) is read as a strong up bar; a red candle with no upper wick, as a strong down bar.
  • Small bodies with long wicks. These look like dojis and suggest the trend is pausing or turning.
  • Confirmation. Waiting for two or three candles of the new colour cuts many one-bar colour changes. The script lets you set that number, and can count only the strong, wickless candles.

Common settings

SettingTypical valueNotes
Candles to confirm1 to 3More confirmation means later but fewer trend changes.
Strong candles onlyOffOn counts only candles with no opposite wick.
TimeframeAnyThe smoothing makes HA popular on intraday charts.

Heikin Ashi itself has no length parameter. The smoothing is fixed by the formula.

Pine Script v6 code

The complete, self-contained script is below. Copy it into a new indicator in TradingView's Pine Editor, or download the .pine file.

Pine Script v6
//@version=6
indicator("Heikin Ashi trend", shorttitle = "HA trend", overlay = false)

confirmBars = input.int(2, "Candles needed to confirm a new trend", minval = 1, maxval = 10, group = "Trend")
strongOnly  = input.bool(false, "Count only candles with no opposite wick", group = "Trend")
colorBars   = input.bool(true, "Colour the main chart's bars by trend", group = "Display")
upCol       = input.color(color.teal, "Bullish", group = "Display")
dnCol       = input.color(color.red, "Bearish", group = "Display")

// Heikin Ashi candles, computed from the regular OHLC so the trend logic uses real data.
haClose = (open + high + low + close) / 4
float haOpen = na
haOpen := na(haOpen[1]) ? (open + close) / 2 : (haOpen[1] + haClose[1]) / 2
haHigh = math.max(high, haOpen, haClose)
haLow  = math.min(low, haOpen, haClose)

// A strong bullish candle has no lower wick; a strong bearish candle has no upper wick.
bullCandle = haClose > haOpen and (not strongOnly or haLow >= haOpen)
bearCandle = haClose < haOpen and (not strongOnly or haHigh <= haOpen)
bullRun    = math.sum(bullCandle ? 1 : 0, confirmBars) == confirmBars
bearRun    = math.sum(bearCandle ? 1 : 0, confirmBars) == confirmBars

var int trend = 0
if bullRun
    trend := 1
else if bearRun
    trend := -1

turnsUp   = trend == 1 and trend[1] != 1
turnsDown = trend == -1 and trend[1] != -1

candleCol = haClose >= haOpen ? upCol : dnCol
plotcandle(haOpen, haHigh, haLow, haClose, "Heikin Ashi", color = candleCol, wickcolor = candleCol, bordercolor = candleCol)
plotshape(turnsUp, "Trend turns up", shape.triangleup, location.bottom, upCol, size = size.tiny)
plotshape(turnsDown, "Trend turns down", shape.triangledown, location.top, dnCol, size = size.tiny)
barcolor(colorBars and trend != 0 ? (trend == 1 ? upCol : dnCol) : na, title = "Trend bar colour")

alertcondition(turnsUp, "Heikin Ashi trend turns up", "Heikin Ashi trend turned up on {{ticker}} ({{interval}}) at {{close}}")
alertcondition(turnsDown, "Heikin Ashi trend turns down", "Heikin Ashi trend turned down on {{ticker}} ({{interval}}) at {{close}}")

The script calculates Heikin Ashi from the regular bars and draws the candles in their own pane, so the main chart keeps real prices. The trend changes after the chosen number of same-colour candles, triangles mark each change, and an option colours the main chart's bars by trend.

Because the logic runs on a regular chart, alerts and any strategy built from it later use real open, high, low and close values. Two alert conditions fire on trend changes; use Once per bar close.

Add it to your chart

  1. Open any chart on TradingView and open the Pine Editor.
  2. Create a new indicator, delete the template code and paste the script.
  3. Click Save, then Add to chart. Change lengths and levels from the indicator's settings.

Stuck on an error? See how to add a Pine Script indicator to TradingView.

Pitfalls

  • Backtesting on a Heikin Ashi chart. If you switch the chart type to Heikin Ashi and run a strategy, fills happen at the synthetic HA prices, which can make results look far better than reality. Run the logic on a regular chart, as this script does, and see backtesting mistakes that inflate results.
  • Lag. Averaging delays colour changes. A reversal can be several bars old before the candles turn.
  • Synthetic levels. Don't place stops or targets at HA open or close values; they aren't prices the market traded.
  • First-bar seed. The very first HA open is an approximation whose effect fades over the following bars, so values on the earliest bars of a chart can differ between platforms.

The script is an educational example: test it on your own markets before relying on it. Indicators describe the past; they don't predict the future. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Make it yours with Edgoro

Describe the change in plain English and Edgoro writes the Pine Script v6 for you, ready to paste. For example:

“Calculate Heikin Ashi candles on a regular chart, signal a trend change after two strong candles of the new colour, and alert once per bar close.”

Build your Heikin Ashi indicator