Concept 20 / 52·Trend·Alternative candles
‹ Hammer & Shooting StarHull Moving Average ›

Heikin-Ashi in Pine Script

Heiken Ashi · HA candles · average bar candles · Heikin-Ashi candles

Candles rebuilt from averaged prices so trends show as runs of one colour with small wicks, at the cost of a bar or two of lag and a close that is not a real price.

Reads best on: Trending markets on 1-hour to daily charts, where averaging removes bar-to-bar noise without hiding the trend; on 1 to 5-minute charts the lag costs more than the smoothing gives.
On this page: the Pine v6 script, checked, drawn on real NVDA bars. Backtest it free, costs charged, every trade on the chart.
01

Definition

Heikin-Ashi, Japanese for average bar, is a way of drawing candles from averaged prices instead of raw ones. Each candle's close is the average of the bar's open, high, low and close, and its open is the midpoint of the previous Heikin-Ashi candle. The high and low are the extremes of the real high, the real low and the two averaged values. It belongs to the family of alternative candle types, alongside Renko and range bars, which redraw price to make trends easier to read at the cost of hiding some detail.

Averaging removes the noise of a single bar. Because each open is anchored to the middle of the previous candle, the candles overlap and chain together, and a run of same-coloured candles appears wherever price drifts steadily in one direction. In a strong uptrend the open sits at the candle's low, so there is no lower wick, and the first lower wick to appear is the first sign that buyers are losing control. Shrinking bodies and wicks on both sides mark a pause. Heikin-Ashi turns the question of trend into a question of colour and wick.

The technique is Japanese in origin and has no parameters of its own, which is unusual for a trend tool. The choices are how many consecutive candles confirm a change of colour and whether to trade from the averaged chart or the real one. The library script keeps the real candles on the chart, colours them by the Heikin-Ashi direction, and draws the Heikin-Ashi open and close as lines. A long is marked after a set number of rising candles, 2 by default, and a short after the same number falling, so entries stay visible against real prices.

Expect lag. The Heikin-Ashi close is the average of the whole bar, so the colour changes a bar or two after real candles turn, and the averaged close is never a price you can trade at. Gaps disappear, because each open is a calculated value rather than the real open. In a trend the chart looks calm and the run of colour is long. In a range the colours alternate and the bodies are small, which is the clearest chop warning the chart offers. The smoothing also means a run ends late, and the last candles of a trend are usually given back.

02

Reading the chart

Read a Heikin-Ashi chart as runs, wicks and body size rather than as individual candles.

  1. Colour the candles by Heikin-Ashi or switch the candle type, but keep the real close visible for entries and stops. The library script overlays the averaged open and close on the real chart for this reason.
  2. Find the runs. A sequence of green candles with flat bottoms and no lower wicks is a strong uptrend; a sequence of red candles with flat tops and no upper wicks is a strong downtrend.
  3. Watch for the first wick against the trend. A lower wick appearing during a green run means the averaged open no longer sits at the low, and the trend is losing thrust.
  4. Note small bodies with wicks on both sides. These are the Heikin-Ashi dojis, and they mark pauses that resolve either as continuation or as the start of the opposite colour.
  5. Count the candles after a colour change. One candle is noise, and the script waits for a set run, 2 by default, before it marks a long or a short.
  6. Cross-check with the real chart before acting. Place the stop at a real swing low or high and take the entry at a real price, because the averaged values are not tradable levels.
03

The maths

Each Heikin-Ashi candle is built from the current bar's prices and the previous Heikin-Ashi candle, which is what makes the chain smooth.

HACloset=Ot+Ht+Lt+Ct4\mathrm{HAClose}_t = \frac{O_t + H_t + L_t + C_t}{4}
HAOpent=HAOpent−1+HACloset−12\mathrm{HAOpen}_t = \frac{\mathrm{HAOpen}_{t-1} + \mathrm{HAClose}_{t-1}}{2}
HAHight=max⁡(Ht, HAOpent, HACloset)\mathrm{HAHigh}_t = \max(H_t,\, \mathrm{HAOpen}_t,\, \mathrm{HAClose}_t)
HALowt=min⁡(Lt, HAOpent, HACloset)\mathrm{HALow}_t = \min(L_t,\, \mathrm{HAOpen}_t,\, \mathrm{HAClose}_t)
OtO_tthe real open of bar t
HtH_tthe real high of bar t
LtL_tthe real low of bar t
CtC_tthe real close of bar t
HACloset\mathrm{HAClose}_tthe Heikin-Ashi close of bar t
HAOpent\mathrm{HAOpen}_tthe Heikin-Ashi open of bar t
HAOpent−1\mathrm{HAOpen}_{t-1}the Heikin-Ashi open of the previous bar
HACloset−1\mathrm{HAClose}_{t-1}the Heikin-Ashi close of the previous bar
HAHight\mathrm{HAHigh}_tthe Heikin-Ashi high of bar t
HALowt\mathrm{HALow}_tthe Heikin-Ashi low of bar t

The first candle has no predecessor, so its open is seeded from the real open and close of the first bar, (O + C) / 2. The library script does the same.

The open is recursive: every previous candle feeds into it. Two charts with different history lengths differ slightly at the start and converge within a few dozen bars.

The Heikin-Ashi close is not a real price. Fills, stops and targets belong on the real chart, and a backtest that fills at Heikin-Ashi prices overstates every result.

Some platforms smooth further by running a moving average over the Heikin-Ashi values. The library keeps the raw definition.

04

Inputs that matter

InputTypicalWhat changing it does
Candles to confirm a flip21 marks every colour change and whipsaws in chop; 3 or more waits for a real run and gives up more of each move before the mark.
Chart timeframe1 hour to dailyHigher timeframes make each averaged candle cover more trading, so the smoothing is stronger and the runs longer; lower timeframes add lag without removing much noise.
05

In practice

  • As a trend-holding tool: the hold rule stays in a trade while the candles keep their colour and their flat side, and ignores the single real candles against the position.
  • As an exit trigger: the first candle of the opposite colour after a long run, or the first wick against the trend on a shrinking body, is a rules-based exit.
  • As a filter for another entry: the filter keeps longs from a pullback pattern only while the Heikin-Ashi run is green, and shorts only while it is red, which keeps entries with the drift.
  • As a chop detector: alternating colours and small bodies with wicks on both sides say the market is ranging, and trend entries are switched off until a run forms.
  • As a re-entry trigger: after a pause, the run count starting again in the trend's direction is the point the library script marks, and it is where add rules trigger.
  • As a higher-timeframe read: a weekly or daily Heikin-Ashi chart gives the regime in one glance, and intraday trades are then taken in that colour.
06

Where it goes wrong

  • Placing stops at Heikin-Ashi levels. The averaged open and close are not prices the market trades at; put stops at real swing points and check the real close.
  • Backtesting fills on Heikin-Ashi prices. Entries at the averaged close look better than any real fill; run the test on real candles with the Heikin-Ashi colour as the condition.
  • Reading one candle as a signal. A single colour change is noise more often than not; wait for the run the script counts.
  • Expecting Heikin-Ashi to be early. It confirms trends that already exist and it flips late; use it to hold and to exit, and something else to enter.
07

Compared with

Moving Average. A moving average smooths the close into a single line; Heikin-Ashi smooths the candle itself, keeping an open, high, low and close. The candle carries wick and body information about each bar that a single line drops.

Moving Average Slope. A slope read gives a number for the drift of an average against a threshold. Heikin-Ashi gives the same drift as colour and wicks, without a threshold, and it turns a little sooner because it averages the bar rather than a window.

SuperTrend. SuperTrend is a single ATR-based line that flips once per trend and doubles as a stop. Heikin-Ashi has no stop level and flips on colour, so the two are often paired: colour for the read, line for the exit.

Engulfing Candle. Candle patterns are defined on real opens and closes. On a Heikin-Ashi chart the open is a calculated midpoint, so engulfing, hammer and inside-bar rules do not apply, and pattern traders read the real chart.

08

Build it in Quant

Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.

Open Heikin-Ashi on the workbench chart
Drawn on real data; then audit, backtest and refine in conversation.
Open in Quant →
09

Questions

  • Why does the Heikin-Ashi close differ from the real close?

    Because it is the average of the bar's open, high, low and close, not the last trade. On a wide-range bar the averaged close can sit far from the real close, and the averaged open sits at the midpoint of the previous candle. That is the source of the smoothness and also the reason the values cannot be used as fills or stops.

  • Can I backtest a strategy on Heikin-Ashi candles?

    Test the logic on Heikin-Ashi and the fills on real prices. A backtest that fills orders at Heikin-Ashi values assumes trades at prices that never existed, and results inflate badly. The library script computes the Heikin-Ashi values itself on a real chart, so its marks, and any strategy built from them, fill at real prices.

  • What does a candle with no lower wick mean?

    In an uptrend it means the averaged open sat at the candle's low, so price never dipped below the midpoint of the previous candle. That is the signature of a strong, one-sided move. When lower wicks start to appear on green candles, buyers are no longer holding every dip, and the trend is entering its late stage.

  • How much lag does Heikin-Ashi add?

    Typically 1 to 2 bars at a turn, and more when the previous run was long, because the recursive open carries the old direction. The lag is the price of the smoothing. It is why Heikin-Ashi suits holding and exiting more than entering, and why traders pair it with a faster tool for entries.

  • How many candles should confirm a change of colour?

    2 is the common default and the library's setting. 1 catches every turn but whipsaws in ranges, while 3 or more filters most noise and gives up more of each move. Match the number to the timeframe: intraday charts usually need more confirmation than daily ones.

  • Does Heikin-Ashi work on 1-minute charts?

    It draws, but the lag costs more than the smoothing gives. Each candle covers so little trading that the averaging removes little noise, while the recursive open still delays every turn. From 15 minutes upward the trade-off improves, and on 1-hour to daily charts the runs are long enough to be useful.