{"id":11522,"date":"2026-08-24T09:24:46","date_gmt":"2026-08-24T05:54:46","guid":{"rendered":"https:\/\/algoyar.com\/?post_type=tutorials&#038;p=11522"},"modified":"2026-08-25T08:33:12","modified_gmt":"2026-08-25T05:03:12","slug":"heikinashi-chart","status":"publish","type":"tutorials","link":"https:\/\/algoyar.com\/en\/tutorials\/heikinashi-chart\/","title":{"rendered":"HeikinAshi Chart"},"content":{"rendered":"<h2 dir=\"ltr\">Properly Using Heikin-Ashi Candles in Pine Script<\/h2>\n<p dir=\"ltr\"><strong>Heikin-Ashi<\/strong> candles are a popular way to display price in TradingView, helping reduce market noise and make trend identification easier. Although these candles look similar to regular candlesticks, there is one very important difference:<\/p>\n<blockquote dir=\"ltr\"><p>The Open, High, Low, and Close values of Heikin-Ashi candles are not necessarily actual market prices.<\/p><\/blockquote>\n<p dir=\"ltr\">This distinction becomes especially important when developing indicators and, in particular, <strong>trading strategies and backtests<\/strong>. If a programmer uses Heikin-Ashi values in their code without taking this difference into account, the indicator or strategy may behave differently from what they expect.<\/p>\n<p dir=\"ltr\">In this tutorial, we first examine how Heikin-Ashi candles are calculated, then explain how to access this data in Pine Script, and finally discuss one of the most important topics: <strong>using Heikin-Ashi in trading strategies and backtesting<\/strong>.<\/p>\n<h3 dir=\"ltr\">Regular Candles vs. Heikin-Ashi<\/h3>\n<p dir=\"ltr\">In a standard candlestick chart, each candle has four main values:<\/p>\n<ul dir=\"ltr\">\n<li><strong>Open<\/strong>: Opening price<\/li>\n<li><strong>High<\/strong>: Highest price<\/li>\n<li><strong>Low<\/strong>: Lowest price<\/li>\n<li><strong>Close<\/strong>: Closing price<\/li>\n<\/ul>\n<p dir=\"ltr\">These values come directly from market data. So, for example, if the Open of a candle is $3,145, that represents the actual market price when that candle opened.<\/p>\n<p dir=\"ltr\">With Heikin-Ashi, however, things are different.<\/p>\n<p dir=\"ltr\">Heikin-Ashi is a type of non-standard price chart whose OHLC values are calculated using specific formulas based on actual market data and the Heikin-Ashi values of previous candles. Therefore, these values are <strong>synthetic prices<\/strong>, not prices at which the market necessarily traded. (<a href=\"https:\/\/www.tradingview.com\/pine-script-docs\/concepts\/non-standard-charts-data\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noopener\">TradingView<\/a>)<\/p>\n<p dir=\"ltr\">As a result, when we examine the same candle on a standard chart and a Heikin-Ashi chart, the Open, High, Low, and Close values may be different.<\/p>\n<hr dir=\"ltr\" \/>\n<h2 dir=\"ltr\">Heikin-Ashi Calculation Formula<\/h2>\n<p dir=\"ltr\">To properly understand how Heikin-Ashi behaves in Pine Script, it is useful to first understand how these candles are calculated.<\/p>\n<p dir=\"ltr\">We can represent the Heikin-Ashi values using <code>HA<\/code>. The formulas are as follows.<\/p>\n<h4 dir=\"ltr\">Close<\/h4>\n<p dir=\"ltr\">The Heikin-Ashi closing price is calculated as the average of the four actual prices of the same candle:<\/p>\n<pre dir=\"ltr\"><code class=\"language-text\">HA Close = (Open + High + Low + Close) \/ 4<\/code><\/pre>\n<p dir=\"ltr\">Therefore, calculating <code>HA Close<\/code> does not require data from the previous Heikin-Ashi candle.<\/p>\n<h4 dir=\"ltr\">Open<\/h4>\n<p dir=\"ltr\">The Open is calculated differently:<\/p>\n<pre dir=\"ltr\"><code class=\"language-text\">HA Open = (HA Open[1] + HA Close[1]) \/ 2<\/code><\/pre>\n<p dir=\"ltr\">In other words, the Open of the current Heikin-Ashi candle is calculated from the average of the Open and Close of the <strong>previous Heikin-Ashi candle<\/strong>.<\/p>\n<p dir=\"ltr\">Therefore, calculating the Open of each candle requires information from the previous candle.<\/p>\n<p dir=\"ltr\">This sequential dependency is an important consideration when implementing Heikin-Ashi in Pine Script.<\/p>\n<h4 dir=\"ltr\">High<\/h4>\n<p dir=\"ltr\">The High is calculated as follows:<\/p>\n<pre dir=\"ltr\"><code class=\"language-text\">HA High = max(High, HA Open, HA Close)<\/code><\/pre>\n<p dir=\"ltr\">In other words, the highest value among:<\/p>\n<ul dir=\"ltr\">\n<li>The actual High of the candle<\/li>\n<li>Heikin-Ashi Open<\/li>\n<li>Heikin-Ashi Close<\/li>\n<\/ul>\n<p dir=\"ltr\">is used as the High of the Heikin-Ashi candle.<\/p>\n<h4 dir=\"ltr\">Low<\/h4>\n<p dir=\"ltr\">Similarly:<\/p>\n<pre dir=\"ltr\"><code class=\"language-text\">HA Low = min(Low, HA Open, HA Close)<\/code><\/pre>\n<p dir=\"ltr\">The lowest value among the actual Low, Heikin-Ashi Open, and Heikin-Ashi Close is selected as the Low of the Heikin-Ashi candle.<\/p>\n<p dir=\"ltr\">Therefore, the formulas can be summarized as follows:<\/p>\n<pre dir=\"ltr\"><code class=\"language-text\">HA Open  = (HA Open[1] + HA Close[1]) \/ 2\r\nHA High  = max(High, HA Open, HA Close)\r\nHA Low   = min(Low, HA Open, HA Close)\r\nHA Close = (Open + High + Low + Close) \/ 4<\/code><\/pre>\n<p dir=\"ltr\">Of course, for the first candle, there is no previous candle, so an initial value for <code>HA Open<\/code> must be defined appropriately.<\/p>\n<hr dir=\"ltr\" \/>\n<h2 dir=\"ltr\">The First-Candle Problem in Heikin-Ashi Calculations<\/h2>\n<p dir=\"ltr\">As mentioned above, <code>HA Open<\/code> depends on <code>HA Open[1]<\/code> and <code>HA Close[1]<\/code>.<\/p>\n<p dir=\"ltr\">However, there is no previous candle when calculating the first candle.<\/p>\n<p dir=\"ltr\">Therefore, we need to define an initial value. There are several ways to do this. The important point is that once the calculation begins, the <code>HA Open<\/code> value of subsequent candles is calculated sequentially.<\/p>\n<p dir=\"ltr\">In Pine Script, we can use <code>barstate.isfirst<\/code> to identify the first candle or use a <code>var<\/code> variable to initialize a value only once.<\/p>\n<p dir=\"ltr\">For example, the general concept can be implemented as follows:<\/p>\n<pre dir=\"ltr\"><code class=\"language-pine\">haOpen = barstate.isfirst ? (open + close) \/ 2 : (haOpen[1] + haClose[1]) \/ 2<\/code><\/pre>\n<p dir=\"ltr\">Here, the average of the actual Open and Close of the first candle is used, and from the second candle onward, the standard Heikin-Ashi formula is applied.<\/p>\n<p dir=\"ltr\">An important point is that the initial value has only a very limited effect further along the series because <code>HA Open<\/code> is continuously recalculated based on subsequent candles.<\/p>\n<hr dir=\"ltr\" \/>\n<h2 dir=\"ltr\">Implementing Heikin-Ashi in Pine Script<\/h2>\n<p dir=\"ltr\">After defining the four OHLC values, we can use <code>plotcandle<\/code> to display the calculated candles on the chart.<\/p>\n<p dir=\"ltr\">The general structure can look like this:<\/p>\n<pre dir=\"ltr\"><code class=\"language-pine\">\/\/@version=6\r\nindicator(\"Custom Heikin-Ashi\", overlay = true)\r\n\r\nhaClose = (open + high + low + close) \/ 4\r\n\r\nvar float haOpen = na\r\n\r\nhaOpen := barstate.isfirst ? (open + close) \/ 2 : \r\n     (haOpen[1] + haClose[1]) \/ 2\r\n\r\nhaHigh = math.max(high, math.max(haOpen, haClose))\r\nhaLow  = math.min(low, math.min(haOpen, haClose))\r\n\r\nhaColor = haClose &gt;= haOpen ? color.green : color.red\r\n\r\nplotcandle(haOpen, haHigh, haLow, haClose, color = haColor)<\/code><\/pre>\n<p dir=\"ltr\">With this approach, we can calculate the Heikin-Ashi candles ourselves in Pine Script and display them on a standard chart without changing the chart type to Heikin-Ashi.<\/p>\n<p dir=\"ltr\">This provides an important advantage: <strong>the main chart remains a Standard Candles chart, while our calculations can still be based on Heikin-Ashi data.<\/strong><\/p>\n<hr dir=\"ltr\" \/>\n<h2 dir=\"ltr\">Using TradingView&#8217;s Built-in Functionality<\/h2>\n<p dir=\"ltr\">We do not always need to implement the Heikin-Ashi formulas ourselves in Pine Script.<\/p>\n<p dir=\"ltr\">TradingView provides a built-in function for accessing Heikin-Ashi data:<\/p>\n<pre dir=\"ltr\"><code class=\"language-pine\">ticker.heikinashi<\/code><\/pre>\n<p dir=\"ltr\">This function creates a special ticker identifier for Heikin-Ashi data, which can then be passed to <code>request.security<\/code> to retrieve Heikin-Ashi values. (<a href=\"https:\/\/www.tradingview.com\/pine-script-docs\/concepts\/non-standard-charts-data\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noopener\">TradingView<\/a>)<\/p>\n<p dir=\"ltr\">For example:<\/p>\n<pre dir=\"ltr\"><code class=\"language-pine\">haTicker = ticker.heikinashi(syminfo.tickerid)\r\n\r\nhaClose = request.security(haTicker, timeframe.period, close)<\/code><\/pre>\n<p dir=\"ltr\">Here:<\/p>\n<ol dir=\"ltr\">\n<li><code>ticker.heikinashi<\/code> creates a Heikin-Ashi ticker identifier for the current symbol.<\/li>\n<li><code>request.security<\/code> retrieves the corresponding data.<\/li>\n<li><code>close<\/code> returns the Heikin-Ashi Close value.<\/li>\n<\/ol>\n<p dir=\"ltr\">The Open, High, and Low can be retrieved in the same way.<\/p>\n<p dir=\"ltr\">For example:<\/p>\n<pre dir=\"ltr\"><code class=\"language-pine\">haOpen = request.security(haTicker, timeframe.period, open)\r\nhaHigh = request.security(haTicker, timeframe.period, high)\r\nhaLow  = request.security(haTicker, timeframe.period, low)\r\nhaClose = request.security(haTicker, timeframe.period, close)<\/code><\/pre>\n<p dir=\"ltr\">TradingView also allows all four OHLC values to be retrieved within a single <code>request.security<\/code> call using a tuple. (<a href=\"https:\/\/www.tradingview.com\/pine-script-docs\/concepts\/non-standard-charts-data\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noopener\">TradingView<\/a>)<\/p>\n<hr dir=\"ltr\" \/>\n<h2 dir=\"ltr\">Why Isn&#8217;t <code>request.security<\/code> Always the Best Choice?<\/h2>\n<p dir=\"ltr\"><code>request.security<\/code> is very useful, but in complex scripts, it is important to pay attention to the number of <code>request<\/code> calls being made.<\/p>\n<p dir=\"ltr\">Suppose that, in addition to Heikin-Ashi data, you also need data from several symbols and multiple timeframes. If you create several separate requests for each calculation, the number of required requests can quickly increase.<\/p>\n<p dir=\"ltr\">For example, if we use four separate requests to retrieve the OHLC values of Heikin-Ashi and repeat the same process for several symbols or timeframes, the total number of requests can increase significantly.<\/p>\n<p dir=\"ltr\">Therefore, if the goal is simply to calculate Heikin-Ashi using data from the same chart and timeframe, directly implementing the formulas at the beginning of the script can sometimes be a better choice.<\/p>\n<p dir=\"ltr\">On the other hand, when you actually need Heikin-Ashi data from another symbol or timeframe, <code>ticker.heikinashi<\/code> together with <code>request.security<\/code> is a suitable approach. <code>request.security<\/code> is specifically designed to retrieve data from different symbol and timeframe contexts. (<a href=\"https:\/\/www.tradingview.com\/pine-script-reference\/v6\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noopener\">TradingView<\/a>)<\/p>\n<hr dir=\"ltr\" \/>\n<h2 dir=\"ltr\">An Important Point About Indicators<\/h2>\n<p dir=\"ltr\">One important consideration when using Heikin-Ashi is its effect on indicators.<\/p>\n<p dir=\"ltr\">Suppose we calculate a 9-period Moving Average using <code>close<\/code>:<\/p>\n<pre dir=\"ltr\"><code class=\"language-pine\">ma = ta.sma(close, 9)<\/code><\/pre>\n<p dir=\"ltr\">If the chart is using standard candles, <code>close<\/code> represents the actual market Close.<\/p>\n<p dir=\"ltr\">However, if the chart is using Heikin-Ashi, <code>close<\/code> represents the synthetic Heikin-Ashi Close.<\/p>\n<p dir=\"ltr\">As a result: <strong>SMA on Standard Candles <\/strong><strong>\u2260 <\/strong><strong>SMA on Heikin-Ashi<\/strong><\/p>\n<p dir=\"ltr\">Even if both indicators use exactly the same formula and period.<\/p>\n<p dir=\"ltr\">This is not limited to Moving Averages. Any indicator that uses OHLC data may produce different results when the chart type is changed from Standard Candles to Heikin-Ashi.<\/p>\n<hr dir=\"ltr\" \/>\n<h2 dir=\"ltr\">A Better Approach: Analyze with Heikin-Ashi, Execute at Actual Market Prices<\/h2>\n<p dir=\"ltr\">In many projects, we may want <strong>signals to be generated based on Heikin-Ashi, while entry and exit prices remain based on actual market prices.<\/strong><\/p>\n<p dir=\"ltr\">In such cases, it is generally better to keep the chart on standard candles and retrieve or calculate Heikin-Ashi data inside the script.<\/p>\n<p dir=\"ltr\">For example:<\/p>\n<pre dir=\"ltr\"><code class=\"language-text\">Main chart \u2192 Standard Candles\r\n                 \u2193\r\nActual market price\r\n                 +\r\nHeikin-Ashi data\r\n                 \u2193\r\nIndicator calculations\r\n                 \u2193\r\nSignal generation\r\n                 \u2193\r\nEntry\/exit at real prices<\/code><\/pre>\n<p dir=\"ltr\">This approach is particularly important for strategies.<\/p>\n<p dir=\"ltr\">TradingView also recommends exercising caution when backtesting on non-standard charts such as Heikin-Ashi, because the prices represented by these candles are not actual market prices, while real-world orders are executed at actual market prices. (<a href=\"https:\/\/www.tradingview.com\/pine-script-docs\/concepts\/non-standard-charts-data\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noopener\">TradingView<\/a>)<\/p>\n<hr dir=\"ltr\" \/>\n<h2 dir=\"ltr\">Why Can Direct Backtesting on Heikin-Ashi Be Problematic?<\/h2>\n<p dir=\"ltr\">Suppose our strategy enters a long position whenever two Moving Averages cross upward.<\/p>\n<p dir=\"ltr\">If we run the strategy on a standard chart, the order is simulated using actual market prices.<\/p>\n<p dir=\"ltr\">However, if we switch the chart to Heikin-Ashi, the Open, High, Low, and Close values of the candles are no longer necessarily actual market prices.<\/p>\n<p dir=\"ltr\">As a result, the strategy may assume that a trade was executed at a price that was never actually traded in the market.<\/p>\n<p dir=\"ltr\">This can affect:<\/p>\n<ul dir=\"ltr\">\n<li>Entry price<\/li>\n<li>Exit price<\/li>\n<li>Stop loss<\/li>\n<li>Take profit<\/li>\n<li>Profit and loss<\/li>\n<li>Number of trades<\/li>\n<li>Risk-to-reward ratio<\/li>\n<li>Overall backtest results<\/li>\n<\/ul>\n<p dir=\"ltr\">Therefore, a strategy may show very attractive performance on a Heikin-Ashi chart but fail to produce the same results when actually executed using real market prices.<\/p>\n<p dir=\"ltr\">TradingView also explicitly explains that Heikin-Ashi OHLC values are synthetic and are not suitable for backtesting or automated trading because orders are executed at actual market prices, not Heikin-Ashi prices. (<a href=\"https:\/\/www.tradingview.com\/pine-script-docs\/concepts\/non-standard-charts-data\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noopener\">TradingView<\/a>)<\/p>\n<hr dir=\"ltr\" \/>\n<h2 dir=\"ltr\">Recommended Approach for Heikin-Ashi Strategies<\/h2>\n<p dir=\"ltr\">If your strategy is based on Heikin-Ashi, one suitable approach is to:<\/p>\n<p dir=\"ltr\"><strong>1. Keep the chart on Standard Candles.<\/strong><\/p>\n<p dir=\"ltr\"><strong>2. Calculate the Heikin-Ashi values directly in Pine Script or retrieve them using<\/strong> <strong><code>ticker.heikinashi<\/code><\/strong>.<\/p>\n<p dir=\"ltr\"><strong>3. Calculate your indicators and entry\/exit logic using the Heikin-Ashi data.<\/strong><\/p>\n<p dir=\"ltr\"><strong>4. Simulate the orders in the standard chart environment using actual market prices.<\/strong><\/p>\n<p dir=\"ltr\">For example, if two EMAs are supposed to be calculated based on the Heikin-Ashi Close, instead of changing the chart type, we can first obtain the Heikin-Ashi Close in the script:<\/p>\n<pre dir=\"ltr\"><code class=\"language-pine\">haClose = ...<\/code><\/pre>\n<p dir=\"ltr\">and then:<\/p>\n<pre dir=\"ltr\"><code class=\"language-pine\">ema1 = ta.ema(haClose, 9)\r\nema2 = ta.ema(haClose, 50)<\/code><\/pre>\n<p dir=\"ltr\">In this case, the analysis logic is based on Heikin-Ashi, while the main chart remains a standard chart.<\/p>\n<hr dir=\"ltr\" \/>\n<h2 dir=\"ltr\">Conclusion<\/h2>\n<p dir=\"ltr\">Heikin-Ashi is not simply a different visual representation of regular candles. It is a set of OHLC values calculated using specific formulas based on actual and historical data. Therefore, the prices displayed by a Heikin-Ashi candle are not necessarily actual market prices.<\/p>\n<p dir=\"ltr\">This distinction is particularly important when programming in Pine Script.<\/p>\n<p dir=\"ltr\">If the goal is simply to <strong>make trends easier to identify and reduce market noise<\/strong>, using a Heikin-Ashi chart can be useful. However, if we want to develop an indicator or, especially, a trading strategy based on Heikin-Ashi, we need to distinguish between the <strong>data used for analysis<\/strong> and the <strong>actual prices used for trading<\/strong>.<\/p>\n<p dir=\"ltr\">There are two main approaches to working with Heikin-Ashi in Pine Script:<\/p>\n<h4 dir=\"ltr\">Method 1: Calculate Heikin-Ashi Directly<\/h4>\n<p dir=\"ltr\">We can implement the Open, High, Low, and Close formulas ourselves in Pine Script.<\/p>\n<p dir=\"ltr\">The advantage of this approach is that we have full control over the data. When we only need Heikin-Ashi data for the same symbol and timeframe, we can also avoid creating multiple <code>request.security<\/code> calls.<\/p>\n<h4 dir=\"ltr\">Method 2: Use TradingView&#8217;s Built-in Functionality<\/h4>\n<p dir=\"ltr\">Using:<\/p>\n<pre dir=\"ltr\"><code class=\"language-pine\">ticker.heikinashi<\/code><\/pre>\n<p dir=\"ltr\">and:<\/p>\n<pre dir=\"ltr\"><code class=\"language-pine\">request.security<\/code><\/pre>\n<p dir=\"ltr\">we can retrieve Heikin-Ashi data directly from TradingView. This approach is particularly useful when we need Heikin-Ashi data for another symbol or timeframe. (<a href=\"https:\/\/www.tradingview.com\/pine-script-docs\/concepts\/non-standard-charts-data\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noopener\">TradingView<\/a>)<\/p>\n<p dir=\"ltr\">But the most important point is:<\/p>\n<blockquote dir=\"ltr\"><p><strong>If your analysis is based on Heikin-Ashi, you do not necessarily need to change the chart itself to Heikin-Ashi.<\/strong><\/p><\/blockquote>\n<p dir=\"ltr\">For many strategies, it is better to keep the chart on <strong>Standard Candles<\/strong>, calculate the Heikin-Ashi data inside the script, and perform the analysis using those values. This way, the signals can be based on Heikin-Ashi while entry and exit prices remain tied to actual market prices.<\/p>\n<p dir=\"ltr\">This distinction between <strong>analytical data<\/strong> and <strong>actual trading prices<\/strong> is one of the key considerations when developing Pine Script strategies and avoiding misleading backtest results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Heikin-Ashi candles can reduce market noise and make trend identification and chart analysis easier, but using them correctly in indicators and strategies requires some important considerations.<\/p>\n<p>In this tutorial, you\u2019ll learn the key differences between Heikin-Ashi and regular candles, as well as how to use Heikin-Ashi properly in Pine Script to avoid misleading results, especially when backtesting strategies.<\/p>\n","protected":false},"featured_media":11523,"comment_status":"open","ping_status":"closed","template":"","class_list":["post-11522","tutorials","type-tutorials","status-publish","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/algoyar.com\/en\/wp-json\/wp\/v2\/tutorials\/11522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/algoyar.com\/en\/wp-json\/wp\/v2\/tutorials"}],"about":[{"href":"https:\/\/algoyar.com\/en\/wp-json\/wp\/v2\/types\/tutorials"}],"replies":[{"embeddable":true,"href":"https:\/\/algoyar.com\/en\/wp-json\/wp\/v2\/comments?post=11522"}],"version-history":[{"count":1,"href":"https:\/\/algoyar.com\/en\/wp-json\/wp\/v2\/tutorials\/11522\/revisions"}],"predecessor-version":[{"id":11618,"href":"https:\/\/algoyar.com\/en\/wp-json\/wp\/v2\/tutorials\/11522\/revisions\/11618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/algoyar.com\/en\/wp-json\/wp\/v2\/media\/11523"}],"wp:attachment":[{"href":"https:\/\/algoyar.com\/en\/wp-json\/wp\/v2\/media?parent=11522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}