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 member
Staff
VIP
Lifetime
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
@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?
 

Scoopy

New member
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:

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • 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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Top