Heiken Ashi Scan

darkelvis

Member
Is it possible to create a scanner for ThinkorSwim that will find green Heikin Ashi candles within the last 2 bars or would that be impossible? Thanks in advance.
 
Last edited by a moderator:
Scan for a 5 Bar Bullish Heikin Ash Trend change

Code:
# Heikin Ashi 5 Bar Bearish Trend Change _JQ

# v01 dated 9.23.2018
# Scans for a 5 Bar change in the Heikin Ashi Trend to Bull from Bear
# relies on RHouser HA candle description code


def xClose  = ( open + high + low + close ) / 4;
rec xOpen   = compoundValue( 1, ( xOpen[1] + xClose[1] ) / 2, xClose );
def Bullish = if ( xClose - xOpen ) >= 0 then yes else no;
def Bearish = if ( xClose - xOpen ) <= 0 then yes else no;
def BullishChange = if ( xClose - xOpen ) >= 0 and ( xClose[1] - xOpen[1] ) <= 0 then yes else no;
def BearishChange = if ( xClose - xOpen ) <= 0 and ( xClose[1] - xOpen[1] ) >= 0 then yes else no;
def BullishChange2Bar = if ( xClose - xOpen ) >= 0 and ( xClose[1] - xOpen[1] ) >= 0 and ( xClose[2] - xOpen[2] ) <= 0 then yes else no;
def BearishChange2Bar = if ( xClose - xOpen ) <= 0 and ( xClose[1] - xOpen[1] ) <= 0 and ( xClose[2] - xOpen[2] ) >= 0 then yes else no;

def BullishChange5Bar = if ( xClose - xOpen ) >= 0
    and ( xClose[1] - xOpen[1] ) >= 0
    and ( xClose[2] - xOpen[2] ) >= 0
    and ( xClose[3] - xOpen[3] ) >= 0
    and ( xClose[4] - xOpen[4] ) >= 0
    and ( xClose[5] - xOpen[5] ) <= 0
        then yes else no;

def BearishChange5Bar = if ( xClose - xOpen ) <= 0
    and ( xClose[1] - xOpen[1] ) <= 0
    and ( xClose[2] - xOpen[2] ) <= 0
    and ( xClose[3] - xOpen[3] ) <= 0
    and ( xClose[4] - xOpen[4] ) <= 0
    and ( xClose[5] - xOpen[5] ) >= 0
        then yes else no;

##    Plot Section.  Choose one plot only
#plot Scan   = Bullish;
#plot Scan   = Bearish;
#plot Scan   = BullishChange;
#plot Scan   = BearishChange;
#plot Scan   = BullishChange2Bar;
#plot Scan   = BearishChange2Bar;
plot Scan   = BullishChange5Bar;
#plot Scan   = BearishChange5Bar;
 
Try this...comment out which one you're not using...I think BullishChange and BearishChange scan for stocks that just switched from from Bullish to Bearish and vice versa...than Bullish or Bearish which scan for stocks already in the specific condition.

Code:
# Heikin Ashi Bullish/Bearish Trend Change _JQ

# v01 dated 9.23.2018

# Scans for a change in the Heikin Ashi Trend to Bull from Bear

# relies on RHouser HA candle description code





def xClose = ( open + high + low + close ) / 4;

rec xOpen = compoundValue( 1, ( xOpen[1] + xClose[1] ) / 2, xClose );

def Bullish = if ( xClose - xOpen ) >= 0 then yes else no;

def Bearish = if ( xClose - xOpen ) <= 0 then yes else no;

def BullishChange = if ( xClose - xOpen ) >= 0 and ( xClose[1] - xOpen[1] ) <= 0 then yes else no;

def BearishChange = if ( xClose - xOpen ) <= 0 and ( xClose[1] - xOpen[1] ) >= 0 then yes else no;



#plot Scan = Bullish;

#plot Scan = Bearish;

plot Scan = BullishChange;

#plot Scan = BearishChange;
 
I found the following in think_Script Cloud OneNote.

HA Bullish/Bearish scan

RE: [TOS_thinkscript] Re: Heikin Ashi Candle Charts - Scan for Bullish and Bearish Shift By R. Houser

Actually, I used the calculations for haClose contained on page 55 of Sylvain Vervoort's book, "Capturing Profit with Technical Analysis," 2009, Marketplace Books, Inc.

A careful analysis of TOS Heikin-Ashi bars shows that on their charts they use this formula for calculating haClose, "def xClose = round( ( open + high + low + close ) / 4, 2 );"

If you feel more comfortable using the alternate calculation for the scan, here's the script.

Code:
######### scan ###################
rec HAopen = compoundValue(1, (HAopen[1] + ohlc4) / 2, ohlc4);
def HAclose = (HAopen + Max(high, HAopen) + Min(low, HAopen) + ohlc4) / 4;
def Bullish = if ( HAclose - HAopen ) >= 0 then yes else no;
plot Scan   = Bullish;
########### EOC ############

Though the alternate calculation produces a somewhat smoother plot than the accepted formula, used in the scan it returns the same result set as the my original script, the difference is, apparently, not material in practice.

My own philosophy is that given the choice, in scans and such, I'll use 1st) TOS actual values and 2nd) when that's not available I'll use calculations that produce results that match TOS's. In charts, particularly in other's studies, I'll use the calculation of the author as they usually have a reason for deviating from accepted formula.
 
Hi Guys, love the site! I’m working on my thinkscript skills, but must admit I’m still a novice.

I’m trying to build a simple screener on TOS:
A scan that searches for stocks with last candle (Heikin Ashi) has turned from Red to Green on a 1000 tick chart.

Should this be easy? Sorry I’m a boomer that struggles getting out of the starting blocks trying to script sometimes ;)
 
@LuckyPlayer2 Please also keep in mind that the scanner can only use specific timeframes (1 min, 3 min, 5 min, 10 min, 15 min, 20 min, 30 min, 1 hr, 2 hr, 4 hr, Day, Week, and Month); it does'nt support tick charts or range bars. Scans also update only once every three minutes at best, so even if it was possible to scan using tick aggregations, the scanner would miss most trades anyway.
 
@roy123 I haven't gotten to the Scan yet but here is my version of the Heikin_Ashi_Trend_Dots lower indicator, complete with alerts and user-selectable AlertType and alert Sounds...

For anyone who hasn't watched the video or isn't familiar with Heikin Ashi I will provide a description of what this indicator does... Heikin Ashi being averaged trending candles, they show the current trend of price without displaying the normal Japanese Candlesticks... What this indicator does is to track the current Heikin Ashi trend and when there is a potential trend reversal, usually indicated by an opposite colored candle... With this indicator that initial dot would be Yellow, meaning to hold until reversal is confirmed...If the following candle is not the opposite color then you can choose to remain in the trade if other indicators aren't screaming reversal... If, however, the following dot is the opposite color then you may choose to exit the trade... A series of Yellow dots indicates that the price is ranging and in an indecisive trend... In such a case you can choose to remain in a trade or to exit and wait for the market to establish trend...

I have included images of D, 30m, 10m, 5m, 2m for comparison... Scan to come next...

Edit: Added option to color Heikin Ashi candles the same as the trend dots.

Ruby:
# Heikin_Ashi_Trend_Dots
# Sounds alerts on trend reversal while eliminating most false signals.
# Paints Yellow dots to signal potential trend reversals while Green and Red dots indicate trend direction.
# Created by rad14733 for usethinkscript.com
# v1.0 : 2021-08-12 : Initial release
# v1.1 : 2021-08-12 : Added option to color Heikin Ashi candles the same as the trend dots.

declare lower;

input useAlerts = yes;
input alertType = Alert.BAR;
input buySound = Sound.DING;
input sellSound = Sound.DING;
input colorBars = yes;

def haClose = ohlc4;
def haOpen = (haOpen[1] + haClose[1]) / 2;
def haTrendUp = if haClose >= haOpen then 1 else 0;
def haTrendDn = if haClose < haOpen then 1 else 0;
def haSignal = 
  if haTrendUp == 1 and haTrendUp[1] == 1 then 1 
  else if haTrendDn == 1 and haTrendDn[1] == 1 then -1 
  else 0
;


plot haDot = if !IsNan(haClose) then 0 else Double.NaN;
haDot.DefineColor("UpTrend", Color.GREEN);
haDot.DefineColor("DnTrend", Color.RED);
haDot.DefineColor("Transition", Color.YELLOW);
haDot.SetPaintingStrategy(PaintingStrategy.POINTS);
haDot.SetLineWeight(5);
haDot.AssignValueColor(
  if haSignal == 1 then haDot.Color("UpTrend") 
  else if haSignal == -1 then haDot.Color("DnTrend") 
  else haDot.Color("Transition")
);

Alert(useAlerts and haSignal[1] == 0 and haSignal == 1, "HA Buy Signal", alertType, buySound);
Alert(useAlerts and haSignal[1] == 0 and haSignal == -1, "HA Sell Signal", alertType, sellSound);

AssignPriceColor(if colorBars then
  if haSignal == 1 then haDot.Color("UpTrend") 
  else if haSignal == -1 then haDot.Color("DnTrend") 
  else haDot.Color("Transition")
  else Color.CURRENT
);

# END - Heikin_Ashi_Trend_Dots

R3R2b4f.png


qg5gVTF.png


wfbZIoq.png


zxtO2L0.png
 
Last edited:
Here is the scan code for this indicator... I recommend saving two separate scans, one for Longs and one for Shorts... I have tested against my main Watchlist using various timeframes with successful results... Enjoy...

UPDATES:

# v1.0 : 2021-08-13 : Initial release.
# v1.1 : 2021-08-17 : Added ability to scan for "unconfirmed" (Yellow) signals.
# v1.2 : 2021-08-25 : Fixed logic for proper scan results and added more usage comments.

Ruby:
# Heikin_Ashi_Trend_Dots_Scan
# Finds potential trend reversals based on Heikin Ashi trend transition direction.
# Created by rad14733 for usethinkscript.com
# v1.0 : 2021-08-13 : Initial release.
# v1.1 : 2021-08-17 : Added ability to scan for "unconfirmed" (Yellow) signals.
# v1.2 : 2021-08-25 : Fixed logic for proper scan results and added more usage comments.

def haClose = ohlc4;
def haOpen = (haOpen[1] + haClose[1]) / 2;
def haTrendUp = if haClose >= haOpen then 1 else 0;
def haTrendDn = if haClose < haOpen then 1 else 0;
def haSignal = 
  if haTrendUp == 1 and haTrendUp[1] == 1 then 1 
  else if haTrendDn == 1 and haTrendDn[1] == 1 then -1 
  else 0
;

# NOTE: ONLY ONE PLOT ALLOWED PER SCAN.
# When Pasting into the TOS Scanners you must comment out the unwanted plots and Un-Comment the desired plot.
# When referencing the Study from within the TOS Scanners, simply select the desired plot.

# Long trend transition (GREEN)
plot long = haSignal == 1;

# Short trend transition (RED)
plot short = haSignal == -1;

# Unconfirmed trend transition (YELLOW)
plot unconfirmed = haSignal == 0;

# END - Heikin_Ashi_Trend_Dots_Scan
 
Last edited:
If I am understanding this right then the dots indicator change to yellow for 1st color change from green to red or vice versa and confirmation to that color happens with second Heikin Ashi candle of same color. Is this understanding correct?
 
If I am understanding this right then the dots indicator change to yellow for 1st color change from green to red or vice versa and confirmation to that color happens with second Heikin Ashi candle of same color. Is this understanding correct?

The Yellow dot indicates potential trend reversal... The next dot confirms whether the trend has changed or not... A series of Yellow dots indicates ranging/consolidation... You can ride through it or take profit and wait for a better setup to get back in...
 
Here is the Heikin_Ashi_Trend_Dashboard which can be added to charts to show the Heikin Ashi trend reversals... It also contains code for potential trend reversal alerts based on the yellow bars, includes colorbar code to color the chart candles, and the dashboard can also be disabled...

yRTUvxj.png


UPDATES:

# v1.0 : 2021-08-17 : Initial release
# v1.1 : 2021-08-18 : Modified to 10 bars from 5
# v1.2 : 2021-08-27 : Added option to hide Dashboard

Ruby:
# Heikin_Ashi_Trend_Dashboard
# Paints Yellow bar to signal potential trend reversals while Green and Red bars indicate trend direction.
# Optionally sounds alerts on trend reversal while eliminating most false signals.
# Optionally paints candles based on HA trend
# Created by rad14733 for usethinkscript.com
# v1.0 : 2021-08-17 : Initial release
# v1.1 : 2021-08-18 : Modified to 10 bars from 5
# v1.2 : 2021-08-27 : Added option to hide Dashboard

input useAlerts = no;
input alertType = Alert.BAR;
input buySound = Sound.DING;
input sellSound = Sound.BELL;
input colorBars = no;
input hideDashboard = no;

def haClose = ohlc4;
def haOpen = (haOpen[1] + haClose[1]) / 2;
def haTrendUp = if haClose >= haOpen then 1 else 0;
def haTrendDn = if haClose < haOpen then 1 else 0;
def haSignal =
  if haTrendUp == 1 and haTrendUp[1] == 1 then 1
  else if haTrendDn == 1 and haTrendDn[1] == 1 then -1
  else 0
;

DefineGlobalColor("Label", Color.ORANGE);
DefineGlobalColor("UpTrend", Color.GREEN);
DefineGlobalColor("DnTrend", Color.RED);
DefineGlobalColor("Transition", Color.YELLOW);

AddLabel(!hideDashboard, "HA Trend:", GlobalColor("Label"));

AddLabel(!hideDashboard, " ",
  if haSignal[9] == 1 then GlobalColor("UpTrend")
  else if haSignal[9] == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
);

AddLabel(!hideDashboard, " ",
  if haSignal[8] == 1 then GlobalColor("UpTrend")
  else if haSignal[8] == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
);

AddLabel(!hideDashboard, " ",
  if haSignal[7] == 1 then GlobalColor("UpTrend")
  else if haSignal[7] == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
);

AddLabel(!hideDashboard, " ",
  if haSignal[6] == 1 then GlobalColor("UpTrend")
  else if haSignal[6] == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
);

AddLabel(!hideDashboard, " ",
  if haSignal[5] == 1 then GlobalColor("UpTrend")
  else if haSignal[5] == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
);

AddLabel(!hideDashboard, " ",
  if haSignal[4] == 1 then GlobalColor("UpTrend")
  else if haSignal[4] == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
);

AddLabel(!hideDashboard, " ",
  if haSignal[3] == 1 then GlobalColor("UpTrend")
  else if haSignal[3] == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
);

AddLabel(!hideDashboard, " ",
  if haSignal[2] == 1 then GlobalColor("UpTrend")
  else if haSignal[2] == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
);

AddLabel(!hideDashboard, " ",
  if haSignal[1] == 1 then GlobalColor("UpTrend")
  else if haSignal[1] == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
);

AddLabel(!hideDashboard, " ",
  if haSignal == 1 then GlobalColor("UpTrend")
  else if haSignal == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
);

Alert(useAlerts and haSignal[1] == 0 and haSignal == 1, "HA Buy Signal", alertType, buySound);
Alert(useAlerts and haSignal[1] == 0 and haSignal == -1, "HA Sell Signal", alertType, sellSound);

AssignPriceColor(if colorBars then
  if haSignal == 1 then GlobalColor("UpTrend")
  else if haSignal == -1 then GlobalColor("DnTrend")
  else GlobalColor("Transition")
  else Color.CURRENT
);

# END - Heikin_Ashi_Trend_Dashboard
 
Last edited:

Update

I have updated the Heikin_Ashi_Trend_Dots_Scan in Post #4 to include unconfirmed, Yellow Dot, trend reversals... This will allow you to create a scan that can send alerts before the actual reversal so you can potentially be prepared for actual reversals before they happen... Please note that none of the plots have been commented out so the script can be saved as a Study and then referenced from within the Stock Hacker Scanner, which is the method I prefer... Alternatively, you can comment out the unwanted plots and save each individual scan as an entire script using an identifying scan name... So the scan can now notify of potential Long, Short, or Unconfirmed reversals...

@jacqshen
 
Here is the scan code for this indicator... I recommend saving two separate scans, one for Longs and one for Shorts... I have tested against my main Watchlist using various timeframes with successful results... Enjoy...

Ruby:
# Heikin_Ashi_Trend_Dots_Scan
# Finds potential trend reversals based on Heikin Ashi trend transition direction.
# Created by rad14733 for usethinkscript.com
# v1.0 : 2021-08-13 : Initial release
# v1.1 " 2021-08-17 : Added ability to scan for "unconfirmed" (Yellow) signals

def haClose = ohlc4;
def haOpen = (haOpen[1] + haClose[1]) / 2;
def haTrendUp = if haClose >= haOpen then 1 else 0;
def haTrendDn = if haClose < haOpen then 1 else 0;
def haSignal =
  if haTrendUp == 1 and haTrendUp[1] == 1 then 1
  else if haTrendDn == 1 and haTrendDn[1] == 1 then -1
  else 0
;

# NOTE: ONLY ONE PLOT ALLOWED PER SCAN.
# Comment out the unwanted plots and Un-Comment the desired plot

# Long trend transition
plot long = haSignal[1] == 0 and haSignal == 1;

# Short trend transition
plot short = haSignal[1] == 0 and haSignal == -1;

# Unconfirmed trend transition
plot unconfirmed = if (haTrendUp == 1 and haTrendUp[1] == 0) or (haTrendDn == 1 and haTrendDn[1] == 0) and haSignal != haSignal[1] then 1 else Double.NaN;

# END - Heikin_Ashi_Trend_Dots_Scan
Hi, How do I exactly set this scan up in TOS--thanks
 
Hi rad14733 amazing indicator! I have a quick question for you, and hope it's something that's quite easy (I just have no idea where to start). Is there a way to show the current day's dot color in a watch list? For example, as I go through my watchlist, I note all the days that end with a yellow dot. I want to throw those in a watchlist, and be able to see what color the dot is when the market opens the next day. Is that something easily scripted?
After playing around, I did get something to work.. The column shows the color of the dot - but just shows 0.0 instead of the dot. I can make due with it! The code I tinkered with/used is:
Code:
declare lower;

def haClose = ohlc4;
def haOpen = (haOpen[1] + haClose[1]) / 2;
def haTrendUp = if haClose >= haOpen then 1 else 0;
def haTrendDn = if haClose < haOpen then 1 else 0;
def haSignal =
if haTrendUp == 1 and haTrendUp[1] == 1 then 1
else if haTrendDn == 1 and haTrendDn[1] == 1 then -1
else 0
;


plot haDot = if !IsNan(haClose) then 0 else Double.NaN;
haDot.DefineColor("UpTrend", Color.GREEN);
haDot.DefineColor("DnTrend", Color.RED);
haDot.DefineColor("Transition", Color.YELLOW);
haDot.SetPaintingStrategy(PaintingStrategy.POINTS);
haDot.SetLineWeight(5);
haDot.AssignValueColor(
if haSignal == 1 then haDot.Color("UpTrend")
else if haSignal == -1 then haDot.Color("DnTrend")
else haDot.Color("Transition")
);


# END - Heikin_Ashi_Trend_Dots
 
@rad14733 For some reason the update isn't pulling up anything for me. I saved it as a study then created a new scan using the study as usual but nothing comes up. Maybe I'm doing something wrong?

Which plot did you select... That exact code is working on my system right now... Scanning for long returned no results for my short watchlist due to todays down market, but it returned for unconfirmed and short...

gPrTJ8K.png


gwScI7x.png
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
278 Online
Create Post

Similar threads

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