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

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?

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.

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

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

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. (??)

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

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

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

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.

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

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

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?

What am I doing wrong?

def haClose = (open+high+low+close)/4;
def haOpen = (open[1]+high[1]+low[1]+close[1])/4;
def haHigh = Max(high[0], haOpen[0], haClose[0]);
def haLow = Min(low[0], haOpen[0], haClose[0]);

Last edited by a moderator:
# Indicator for doji candles on an Heiken Ashi Chart
####################################################################
declare upper;

input changeCandleColor = yes;
input dojiBodyRatio = 0.05;

def hc = OHLC4;
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 hRange = (hh-hl);
def doji_data = AbsValue(ho - hc) <= (hRange) * dojiBodyRatio;

plot doji_green = if doji_data and (hc > ho) then 1 else Double.NaN;
doji_green.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
doji_green.SetDefaultColor(Color.CYAN);

plot doji_red = if doji_data and (ho > hc) then 1 else Double.NaN;
doji_red.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
doji_red.SetDefaultColor(Color.CYAN);

AssignPriceColor(if changeCandleColor and doji_data then Color.WHITE else Color.CURRENT);

Last edited:
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);``````

Chart Label for this... Does anyone know if it is possible to make? Indicating whether there is a continuation?
i.e. if the previous 5 candles were green, the label would maybe say "Bullish 5 Candle Heikin Continuation"
or if the previous 3 candles were red, the label could say "Bearish 3 Candle Heikin Continuation"

Does anyone have a scanner for this to identify Hollow vs Solid candles and Red vs green candles? I would find this very useful as things leave demand and supply. Thank you all.

question,
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 );
question: although similar, how does this formula compares to Valcu and Vervoort formulas? or are these primarily for smoothing?

Also, how are hh and hl relevant. they don't seem to be used in the result?

thanks.

Last edited:
question,

question: although similar, how does this formula compares to Valuc and Vervoort formulas? or are these primarily for smoothing?
It sounds as if you would like to contrast and compare different scripts?
But that is not possible as you didn't provide any scripts to contrast and compare to.

The standard method for comparing indicators, is to put them on your chart.
Be aware that you will need to extensively play with the settings in order to compare apples-to-apples.

If you are still interested in having a discussion, comparing various scripts.
1. Please post chart images where all the studies are applied.
2. Mark up any confluences that you have questions about.
3. Make sure that you include a shared chart link, so members can see what you are seeing.
How to create a shared chart link:

Also, how are hh and hl relevant. they don't seem to be used in the result?

thanks.
The Heiken Ashi hh hl definitions are used to PLOT the HA candles. As this script does not PLOT the candles, those definitions are not used.

Last edited:

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
404 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• Add-ons and resources
• 1 full year of unlimited support

### Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?