OPEN-SOURCE SCRIPT

Smoothed Heiken Ashi - SamX

Updated
This is my version of the Smoothed Heiken Ashi indicator. While there are a few versions of these out there (most notably "Modified Smoothed Heiken Ashi" by badshah_e_alam and "Smoothed Heiken Ashi Candles v1" by jackvmk), none matched what I was looking for.

1. All had hard-coded the smoothing to use the EMA calculation - in this indicator, these are user-configurable
2. Both applied the double-smoothing principle (Take a moving-average of price, apply the Heiken Ashi conversion, then apply a second moving average to the smoothed Heiken Ashi candles to calculate new candles). While this is the most common approach, I had a few issues with this:
  • Even if setting both moving average lengths to 1, the indicator did not correctly calculate what should be the actual corresponding Heiken Ashi candle values (as compared to the built-in Heiken Ashi chart on Tradingview)
  • They were inconsistent in operating on different chart types - indicator values for the same candle can differ between using a base Heiken Ashi or Renko chart vs a standard Candle or Bar chart.
  • I wanted the ability to easily enable/disable the second smoothing (which is now a configuration option)
  • I wanted the ability to configure different moving-average calculations for each smoothing iteration (e.g. EMA for the initial smoothing, and Smoothed Moving Average for the second smoothing)

3. Many of the inputs were not clearly or properly defined in the settings window - this script has far more refined user input settings, put in logical groupings, and with relevant help text.
4. Enhanced some visual styling (and added to the Settings) to make it easier for the user to enable/disable printing candle wicks, as well as customizing the bullish / bearish bar colors
5. Added alerts for bar color change to help users catch potential trend reversals


Reference paper on the original Smoothed HA formulas: researchgate.net/publication/328811639_SMOOTHED_HEIKIN-ASHI_ALGORITHMS_OPTIMIZED_FOR_AUTOMATED_TRADING_SYSTEMS
Reference on the more common implementation formulas (using double-smoothing): sierrachart.com/index.php?page=doc/StudiesReference.php&ID=314&Name=Heikin-Ashi_Smoothed
Release Notes
Found and fixed a minor bug in a couple calculations that could occasionally result in a minor numerical inaccuracy. While this didn't affect the actual printed drawing output on the chart, I still felt it was worth correcting to better support the ability to use this indicator in bar-replay mode for back-testing strategies based on HA candles and any Indicator on Indicator usage that relies on the raw OHLC values.

On that note, here is how this indicator can be used to perform bar-replay of Heiken Ashi candles (since this functionality is not available in TradingView natively):
  • Open a new chart layout with the Candles type of price display
  • Open the "Chart Settings" and select the "Symbol" tab
  • Uncheck all of the following to remove them from being printed on the chart: Body, Borders, and Wick. At this point, you should have an effectively blank chart.
  • Add this indicator to the current chart (do not change the chart type, leave that as set above).
  • Update this indicator's settings as follows:
    Timeframe for HA candle calculation: Current (default)
    Show Wicks: Checked (default)
    HA Price Input Smoothing Length: 1
    Moving Average Calculation: Exponential (default)
    Enable double-smoothing: Unchecked


Disclaimer: Before relying on this to do back-testing of HA candles in bar-replay mode, I highly recommend verifying the accuracy of the HA candles yourself in bar replay mode to help instill confidence in the accuracy of this backtesting method. For a suggestion on how to do this, here is what I did:
  • Open a new chart layout with the 2-chart option (I used the stacked layout) and remove any indicators that may be on by default
  • Open the same security and timeframe on both charts (e.g. EURUSD 1h)
  • On the upper chart: Select the Heiken Ashi chart type, then open the Chart Settings -> Status line and make sure "OHLC Values" is checked (this will help significantly in comparing the indicator computed HA candles to the actual HA candles)
  • On the lower chart: Follow the instructions listed above on preparing a chart for HA Bar Replay, then open the Chart Settings -> Status line and make sure "Indicator Values" is checked
  • Enter Bar-Replay mode on the lower chart at any point you see fit.
  • As you step forward in bar-reply mode, check the values next to the Smoothed HA indicator line on the lower chart to the OHLC values on the upper Heiken Ashi chart for the same candle. All 4 values should match exactly.
Release Notes
No changes to any functional code, I just wanted to post a quick clarification to the note about using this to back-test with HA candles in bar replay mode...

As the base chart type will still technically be normal candles, any additional indicators that you add to the chart will calculate based on normal candle values for open, high, low, and close instead of the Heiken Ashi values!

Let's use the EMA as an example...
On a normal Heiken Ashi chart using the built-in Moving Average Exponential indicator with a price source of "close", the EMA calculation would be performed using the HA value for "close" (which is the average of the actual open, high, low, and close of the candle) instead of the actual close value for that time period. However, if you change the chart type to "Candle", the EMA calculation would shift to using the actual candle closing price value instead of the Heiken Ashi close value, resulting in a new and possibly different EMA line.

This is due to how the default Pinescript variables for "open", "high", "low", and "close" are set by TradingView. The values for these variables are determined by the actual on-screen chart type and corresponding candle values. For a "normal" chart like Bars, Candles, Hollow Candles, etc., the values for these default variables will be the actual open, high, low, and close prices. However, if you change to a different chart type like Heiken Ahsi, Renko, etc., the values for these same variables will change to whatever the post-transformation plotted values on the chart are.

While in most cases this is pretty intuitive and desired behavior, it can present some challenges in cases where you want to reference true price in an indicator when viewing a Heiken Ashi chart, and vice versa.

In this case, since the "bar reply of Heiken Ashi candles" is technically using a true-price bar/candle chart, any additional indicators added to the chart in that mode will be based on true-price values and not Heiken Ashi values. This means that the same additional indicator and settings may display differently than the same indicator and settings on a native Heiken Ashi real-time chart.
ashiheikenheiken-ashiMoving AveragessmoothedTrend Analysis

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer