Heikin-Ashi (which is correct?)

Townsend

Active member
VIP
The Heikin Ashi Formula consists of four calculations, which remap Open, Close, High, and Low:
Open = [Open (previous bar) + Close (previous bar)]/2.
Close = (Open+High+Low+Close)/4.
High = Max Price Reached.
Low = Max Low Price Reached.

The chart of the left is the system generated Heikin-Ashi colors.
The chart on the right is the code based Heikin-Ashi colors, derived from the standard formula, as described above.
Notice the difference, highlighted in gray.
pbzpH8q.png


This code will paint the Heikin-Ashi colors on any bar type, including Equivolume.
Code:
def haOpen = (open[1] + close[1])/2;
def haClose = (open + close + high + low)/4;

assignpriceColor(if haOpen < haClose then color.green else
if haOpen > haClose then color.red else color.white);

Which one is correct? Which one is better? Either my code is off, or TOS is using non-traditional inputs?
 

BenTen

Administrative
Staff
VIP
This is interesting. I also did a quick comparison between Heikin-Ashi candles from TradingView vs. ThinkorSwim. Didn't spot any differences except one or two mismatch.
 

Townsend

Active member
VIP
I also experimented with this code. Averaging the Heikin-Ashi values makes the overall trend more steady, but fails on the important task of spotting tops and bottoms, which the regular Heikin-Ashi is pretty good at. (If you don't mind a lot of whip-saws along the way.)
Code:
input length = 6;
def averageType = AverageType.wilders;

def haOpen = (open[1] + close[1])/2;
def haClose = (open + close + high + low)/4;

def avgOpen = MovingAverage(averageType, haOpen, length);
def avgClose = MovingAverage(averageType, haClose, length);

assignpriceColor(if avgOpen < avgClose then color.green else
if haOpen > haClose then color.red else color.white);
 

diazlaz

Well-known member
2019 Donor
VIP
Hi @Townsend,

not sure the date ranges to test, but here is what I use, not sure if it will render differently.

#Heikin-Ashi
def hc = (open + high + low + close ) / 4;
def ho = CompoundValue( 1, ( ho[1] + hc[1] ) / 2, hc );
def hh = Max(Max(high , ho), hc);
def hl = Min(Min(low , ho), hc);
def hh1r = if ho == hc then 0 else if ho < hc then 1 else -1;
AssignPriceColor ( if hh1r == 1 then COLOR.GREEN else COLOR.RED );
 

tomsk

Well-known member
VIP
@Townsend You typically use CompoundValue() to start something after some number of bars, or ito nitialize something with a specific value, or both. Usually you don't need it. Occasionally you may find something not initializing correctly, in that case use CompoundValue() to specify the start value.
 

Townsend

Active member
VIP
Great answer @tomsk! I actually have a use the CompoundValue() function. I notice some of my indicators start at zero on the first bar of the series, which always upsets the vertical scale of the chart, when that bar is displayed. Like when you double click to see the entire span of data requested.

Anyway... my Heikin-Ahi painting indicator is working fine now. Here's the end result. Thanks for your help guys.
Code:
input length = 1; # 1= normal HeikinAshi-Ashi
input averageType = AverageType.simple;

def haClose = (open + high + low + close ) / 4;
def haOpen = (haOpen[1] + haClose[1]) /2;

def avgOpen = MovingAverage(averageType, haOpen, length);
def avgClose = MovingAverage(averageType, haClose, length);

assignpriceColor(if avgOpen < avgClose then color.green else
if avgOpen > avgClose then color.red else color.white);
 

HighBredCloud

Well-known member
VIP
@diazlaz WOW...I've been looking for this for a long time...Thanks for posting this!

@Townsend whats the difference between your Heikin Ashi and the one posted by @diazlaz? They look identical when plotted...yours just has an option to change the MA's...sorry I am not a coder so I can't really tell by the code...
 

Townsend

Active member
VIP
@Townsend whats the difference between your Heikin Ashi and the one posted by @diazlaz? They look identical when plotted...yours just has an option to change the MA's...sorry I am not a coder so I can't really tell by the code...

They are identical! @diazlaz came up with the solution I was looking for. I used his code with my names.
 

grasshopper123

New member
Thanks so much @diazlaz and @Townsend. This really helps me very much

I am coming over from TC2000/freestockcharts.com so I'm used to a little more info in the HA candle. May I make a request? Is it possible to amend the code so that

a) the color of the bar is based on the net change from the day before: green if today's close > yesterday's close, red if today's close < yesterday's close

b) the fill of the bar is based on today's open vs today's close: unfilled if open<close, filled if open > close

Thanks in advance.
 

bitshift

New member
Hello everyone, first post. I am a software developer by trade so I understand coding in general.

Trying to use HA candle calculations in a scan to find matches on these criteria:
  • HA current candle is higher than HA previous candle (higher high)
  • HA previous candle of 2 bars ago was red (HA low of 2 bars ago was lower than HA low of 3 bars ago?)

I was starting to define seperate variables for the HA open/close/high/low to use in comparisons and then I stumbled across a built in function HeikinAshiDiff()
https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/G-L/HeikinAshiDiff
Which the HeikinAshiDiff()."HADiff" value seems to be ... what Im after?

Code:
HeikinAshiDiff()."HADiff" from 1 bars ago is less than HeikinAshiDiff()."HADiff"
and HeikinAshiDiff()."HADiff" from 2 bars ago is less than 0

Questions:
  • In TOS in general, what determines when a HA candle switches from red to green and vice/versa?
  • Can the HeikinAshiDiff()."HADiff" function be used in place of using seperate variables for the HA values?
 

Similar threads

Top