Heikin-Ashi (which is correct?)

Townsend

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.


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

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

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);
 
D

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 );
 
Townsend

Townsend

Active member
VIP
Thanks @diazlaz!! That worked! Now they match up.

It even works without using the CompoundValue function.
Just by having the Heikin Open referencing the Heikin Close, instead of just a normal Close.

I read about the CompoundValue function. Still have no idea idea what it used for. (??)
 
T

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

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);
 
H

HighBredCloud

Well-known member
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 );
WOW...I've been looking for this for a long time...Thanks for posting this!
 
H

HighBredCloud

Well-known 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);
@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...
 
T

TrueDepth

Member
VIP
When I used Heiken-Ashi, I first used a code I got from the internet, then I figured out the ThinkorSwim official version was best.
 
H

HighBredCloud

Well-known member
VIP
When I used Heiken-Ashi, I first used a code I got from the internet, then I figured out the ThinkorSwim official version was best.
@TrueDepth both codes posted above by @diazlaz and @Townsend seem to resemble TOS version Heikin Ashi perfectly...
 
Townsend

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.
 
G

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.
 

Similar threads

Top