In Pine Script, you get Heikin Ashi data for an indicator by calculating the four Heikin Ashi values yourself from regular chart candles, storing them in variables with var, updating them on each bar, and passing those values into your indicator's calculations. A built-in ta.heikinashi() function returns a Heikin Ashi symbol so you can request its data through request.security(), but in most indicator code the manual formula is simpler, faster, and avoids repainting surprises on real-time bars. The right choice depends on whether your indicator needs Heikin Ashi as a signal source, as a smoothed substitute for price, or as a separate reference series.
This article walks through both approaches on TradingView's Pine Script v6, the current version as of late 2026, with complete examples, expected outputs, and the failure points that trip people up.
What "Heikin Ashi data" actually means inside an indicator
Heikin Ashi is not a different market; it's a smoothed transformation of the same open, high, low, and close (OHLC) data your chart already has. Each Heikin Ashi bar is computed from the previous Heikin Ashi bar and the current regular bar, which is why the series has memory and why your script must carry values forward between bars.
The standard formulas are:
Heikin Ashi close = (regular open + regular high + regular low + regular close) / 4 Heikin Ashi open = (previous Heikin Ashi open + previous Heikin Ashi close) / 2 Heikin Ashi high = the highest of the regular high, the current Heikin Ashi open, and the current Heikin Ashi close Heikin Ashi low = the lowest of the regular low, the current Heikin Ashi open, and the current Heikin Ashi close
The first bar has no previous Heikin Ashi values, so scripts seed the initial Heikin Ashi open and close from the first regular bar's open and close. Every later bar builds on that seed. This is the mechanism that makes Heikin Ashi trends look smoother and lag slightly compared with raw candles, and it's also why a naive recalculation that forgets to persist previous values produces garbage after the first bar.
Two things follow from the formulas. First, Heikin Ashi open and close are averages, so they don't correspond to actual traded prices; an indicator that plots Heikin Ashi close is plotting a synthetic level. Second, Heikin Ashi high and low are clamped to include the synthetic open and close, so ranges can differ from the real bar's range. If your indicator does volume-profile or gap analysis, those distortions matter.
Prerequisites before you touch the code
You need a TradingView account with the Pine Editor open and a chart loaded, ideally on a liquid symbol like a major index or large-cap stock so bar data is clean. You should know whether your indicator runs on the chart's own symbol or pulls data from another symbol via request.security(), because that decision changes which method you use. You also need to decide whether you want Heikin Ashi values to drive the indicator's signals, or simply to be plotted alongside regular-price signals.
Finally, confirm your script's declared version. The examples here use //@version=6. Pine Script v5 code differs mainly in namespacing (ta. prefixes) and some function signatures, so if you're maintaining older code, adapt names accordingly rather than pasting v6 snippets wholesale.
Method 1: Calculate Heikin Ashi OHLC manually in your indicator
This is the method I'd reach for first for most indicators. It's transparent, runs on the chart's own data without an extra security call, and gives you direct access to each Heikin Ashi component.
Step-by-step build
1: Declare the script with indicator(), giving it a title and overlay=false if you're plotting oscillators, or overlay=true if you want the Heikin Ashi values drawn on price. 2: Create persistent variables for the previous Heikin Ashi open and close using var float haOpen = na and var float haClose = na. The var keyword is essential; without it, the values reset every bar and the recursion collapses. 3: Compute the current Heikin Ashi close as the average of the regular open, high, low, and close. 4: Compute the current Heikin Ashi open as the average of the previous Heikin Ashi open and previous Heikin Ashi close, but only after the first bar. On bar zero, fall back to the regular open (or the average of regular open and close). 5: Derive Heikin Ashi high and low using math.max and math.min across the regular high or low and the two synthetic values. 6: Write the current values back into your persistent variables at the end of the bar so the next bar can use them. 7: Feed the resulting series into your indicator logic, such as an EMA of Heikin Ashi close or a crossover of Heikin Ashi open and close.
A compact working example looks like this:
What to expect and how to check it
Plot the four Heikin Ashi values temporarily with plotcandle(haOpenNow, haHighNow, haLowNow, haCloseNow) and compare against TradingView's built-in Heikin Ashi chart type. The candles should match bar for bar on historical data. If your candles drift after the first few bars, the usual cause is missing the var declaration or writing the previous values after the calculation instead of before the next bar's use. A quick numeric check: on any bar, your Heikin Ashi close should equal the simple average of that bar's regular OHLC to within floating-point rounding.
One subtlety: assignment with := inside the global scope executes in script order, so update haOpen and haClose only after computing this bar's synthetic values. If you update them first, the high and low calculations will use stale data.
Where the manual method breaks down
If you request data from another symbol with request.security() and try to compute Heikin Ashi inside that call, you'll hit a well-known Pine limitation: functions that rely on historical series state, including recursive assignments, don't behave reliably inside request.security() expressions. The workaround is to fetch raw OHLC from the other symbol, then compute Heikin Ashi in the main script's scope using those fetched values. That keeps the recursion in a context where it's supported.
Method 2: Use ta.heikinashi() with request.security()
Pine Script provides a built-in function, ta.heikinashi(symbol), that returns a Heikin Ashi version of a symbol string. You can't read its open, high, low, and close directly as a tuple; instead you request it as a symbol through request.security(). The pattern is:
The upside is brevity and the fact that TradingView's own aggregation handles the recursion and higher-timeframe alignment. The trade-offs are real, though. Each request.security() call adds a data request, and TradingView caps how many you can make in one script, so requesting all four Heikin Ashi values separately is wasteful. Requesting fewer values and deriving the rest is often impossible because Heikin Ashi open depends on the prior bar's open and close, which you can't reconstruct from a single returned series. In practice, many developers request Heikin Ashi close and Heikin Ashi open only, which covers most crossover and trend indicators.
There's also a repainting consideration. On the live, unclosed bar, request.security() returns values that can change as the bar develops, and Heikin Ashi compounds that because the synthetic close moves with the forming regular close. For alert-based strategies, evaluate signals on barstate.isconfirmed or use the previous bar's values to avoid intrabar flicker. Historical bars are stable once closed, so backtest results and live signals can diverge if you ignore this.
Choosing between the two methods
The comparison below holds the script version (v6), timeframe, and data source constant so the differences reflect method rather than setup.
| Criterion | Manual calculation | ta.heikinashi() + request.security() |
|---|---|---|
| Access to all four HA values | Direct, as local variables | Requires multiple requests, or accept partial data |
| Works on chart's own symbol | Yes | Yes |
| Works for another symbol or higher timeframe | Yes, fetch raw OHLC then compute | Yes, but recursion inside security calls is unreliable |
| Request budget cost | None | One per requested series |
| Repainting risk on live bar | Standard, tied to regular close | Higher, tied to security call plus synthetic close |
| Code length | Longer | Shorter |
| Best for | Custom indicators, multi-symbol logic, HA-derived oscillators | Quick reference series, single-symbol studies |
If your indicator needs Heikin Ashi open, high, low, and close together, or needs to combine them with regular-price logic, manual calculation wins on both control and request economy. If you only need a Heikin Ashi close line for a single symbol and want minimal code, the built-in route is fine, provided you handle confirmation.
Feeding Heikin Ashi into common indicator types
Once you have Heikin Ashi series, most indicator math applies unchanged, but the interpretation shifts because you're averaging averages. An EMA of Heikin Ashi close lags regular-price EMA by roughly the same amount Heikin Ashi itself lags the candle, which can make crossovers look cleaner in trends and later at turning points. RSI computed on Heikin Ashi close tends to compress toward the middle of its range because the synthetic close is smoother; oversold and overbought thresholds calibrated on regular RSI may fire less often. ATR on Heikin Ashi high and low reflects the synthetic range, not the traded range, so position-sizing that uses it will size differently than ATR on real candles.
For a worked illustration: suppose a stock's regular bar has open 100, high 104, low 99, close 103. Heikin Ashi close is (100 + 104 + 99 + 103) / 4 = 101.5. If the prior Heikin Ashi open and close were 100.8 and 101.9, the new Heikin Ashi open is (100.8 + 101.9) / 2 = 101.35. The Heikin Ashi high is the max of 104, 101.35, and 101.5, which is 104; the Heikin Ashi low is the min of 99, 101.35, and 101.5, which is 99. Notice the synthetic close (101.5) sits well below the real close (103), a common pattern in uptrends that makes Heikin Ashi candles look persistently "weaker" than price. Your indicator reads that smoothed value, not the 103 print.
Handling higher timeframes and multi-symbol requests correctly
When you want Heikin Ashi from a higher timeframe, the safe pattern is to request raw OHLC from that timeframe and compute Heikin Ashi on the main chart's bar-by-bar sequence. Requesting the Heikin Ashi symbol at the higher timeframe directly can look right on historical bars but behaves inconsistently in real time and can misalign because the security call returns one aggregated bar per higher-timeframe period while your chart advances every lower-timeframe bar.
Use barmerge.gaps_off and barmerge.lookahead_off in request.security() for historical consistency, and reference the previous completed higher-timeframe value with the offset when your logic must not see the future. For multi-symbol indicators, request regular open, high, low, and close from each symbol, then run the manual Heikin Ashi recursion per symbol with separate persistent variable sets. This multiplies your code but keeps behavior predictable and stays within request limits if you use one request.security() call returning a tuple.
Troubleshooting the failures you'll actually hit
Symptom: Heikin Ashi candles match at first and then diverge wildly. Cause: missing var persistence or assignment order. Fix: confirm the previous-value variables are declared with var and updated after computing the current bar.
Symptom: "Cannot use 'request.security' in local scope" or recursion-related errors. Cause: Heikin Ashi logic placed inside the security expression. Fix: move the recursion into main scope and pass raw OHLC through the security call.
Symptom: signals flicker or alerts fire then vanish. Cause: live-bar repainting from unconfirmed closes. Fix: gate logic on barstate.isconfirmed or shift inputs by one bar.
Symptom: indicator plots flat or zero. Cause: initial na values propagating because the first-bar seed was never set. Fix: seed Heikin Ashi open and close from the first regular bar explicitly.
If your script throws runtime errors tied to request limits or unsupported nested calls, and you can't reduce the request count or restructure the recursion, that's the point to simplify the design or consult TradingView's Pine reference rather than force a workaround that misbehaves live.
Practical limits worth knowing
Heikin Ashi data is derived, not exchanged, so no provider hands you a "true" Heikin Ashi feed to cross-check against; every implementation follows the same recursive definition, and small differences appear only in seeding and live-bar handling. Manual and built-in methods can legitimately disagree on the forming bar for that reason. Also, because Heikin Ashi averages the prior synthetic bar forward, long histories accumulate slight differences if your seed bar differs from the platform's, though closed-bar values converge under the standard formulas. None of this changes the core recipe: persist previous Heikin Ashi open and close, compute the four values from regular OHLC, and feed them into your indicator.