Alert when HA candle turns color after "X" number of candles

Canyon135

New member
I saw this post which shows when a Heiken Ashi Candle changes color.
https://usethinkscript.com/threads/completed-heikin_ashi-indicator.5251/page-12#post-56187

I would like to alert on a change of color only after a "count" of previous candles has exceeded. In other words:
1) I would like to pass in a count (e.g 5) for this study
2) When a series of HA green candles exceeds the count and then a red HA candle appears, I would like to alert on that. And vice versa

Thank you!
 
I saw this post which shows when a Heiken Ashi Candle changes color.
https://usethinkscript.com/threads/completed-heikin_ashi-indicator.5251/page-12#post-56187

I would like to alert on a change of color only after a "count" of previous candles has exceeded. In other words:
1) I would like to pass in a count (e.g 5) for this study
2) When a series of HA green candles exceeds the count and then a red HA candle appears, I would like to alert on that. And vice versa

Thank you!
Add the following code to the bottom of your study:
Ruby:
# Ashi Bar Count
# @MerryDay 2/18/2022
input HowMany = 5 ;

def direction = if trendUp then 1 else if trendDown then 2 else direction[1] ;
def Green_count = if trendUp
            then 1 else
                  if direction == 1
            then Green_count[1] + 1 else Green_count[1] ;
def Red_count = if trendDown
            then 1 else
                  if direction == 2
            then Red_count[1] + 1 else Red_count[1] ;

AddLabel(Green_count > HowMany or Red_count > HowMany,
if direction==2 then Green_count +" green bars exceeded " +HowMany else
if direction==1 then Red_count +" red bars exceeded " +HowMany else " ",
if direction==2 then color.green else
if direction==1 then color.red else
color.violet);

def AlertOnRed   = trendDown and Green_count > HowMany ;
def AlertOnGreen = trendUp and Red_count > HowMany ;
Alert(AlertOnRed, "Count exceeded", Alert.BAR, Sound.BELL);
Alert(AlertOnGreen, "Count exceeded", Alert.BAR, Sound.BELL);
 
Add the following code to the bottom of your study:
Ruby:
# Ashi Bar Count
# @MerryDay 2/18/2022
input HowMany = 5 ;

def direction = if trendUp then 1 else if trendDown then 2 else direction[1] ;
def Green_count = if trendUp
            then 1 else
                  if direction == 1
            then Green_count[1] + 1 else Green_count[1] ;
def Red_count = if trendDown
            then 1 else
                  if direction == 2
            then Red_count[1] + 1 else Red_count[1] ;

AddLabel(Green_count > HowMany or Red_count > HowMany,
if direction==2 then Green_count +" green bars exceeded " +HowMany else
if direction==1 then Red_count +" red bars exceeded " +HowMany else " ",
if direction==2 then color.green else
if direction==1 then color.red else
color.violet);

def AlertOnRed   = trendDown and Green_count > HowMany ;
def AlertOnGreen = trendUp and Red_count > HowMany ;
Alert(AlertOnRed, "Count exceeded", Alert.BAR, Sound.BELL);
Alert(AlertOnGreen, "Count exceeded", Alert.BAR, Sound.BELL);
@MerryDay thanks for this! What would be needed to add to this code to be able to then create a TOS study alert with it as a condition? That is I would like to use Canyon135's study as a condition to then create custom Audio alerts for the green to red and red to green conditions. thanks!
 
Thank you so much, @MerryDay - Works beautifully. Made very minor tweaks - If anyone wants the full script, please see below:


----
def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def haColor = haClose > haOpen;

#plot trendUp = haColor and !haColor[1];
#trendUp.SetDefaultColor(Color.CYAN);
#trendUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#plot trendDown = !haColor and haColor[1];
#trendDown.SetDefaultColor(Color.MAGENTA);
#trendDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
def trendUp = haColor and !haColor[1];
def trendDown = !haColor and haColor[1];
#Alert(trendUp, "Trend Up", Alert.BAR, Sound.RING);
#Alert(trendDown, "Trend Down", Alert.BAR, Sound.RING);

# Ashi Bar Count
# @MerryDay 2/18/2022
input HowMany = 5 ;

def direction = if trendUp then 1 else if trendDown then 2 else direction[1] ;
def Green_count = if trendUp
then 1 else
if direction == 1
then Green_count[1] + 1 else Green_count[1] ;
def Red_count = if trendDown
then 1 else
if direction == 2
then Red_count[1] + 1 else Red_count[1] ;

AddLabel(Green_count > HowMany or Red_count > HowMany,
if direction==2 then Green_count +" green bars exceeded " +HowMany else
if direction==1 then Red_count +" red bars exceeded " +HowMany else " ",
if direction==2 then color.green else
if direction==1 then color.red else
color.violet);

def AlertOnRed = trendDown and Green_count > HowMany ;
def AlertOnGreen = trendUp and Red_count > HowMany ;
Alert(AlertOnRed, "Count exceeded - Green to Red", Alert.BAR, Sound.BELL);
Alert(AlertOnGreen, "Count exceeded - Red to Green", Alert.BAR, Sound.BELL);
 
Last edited:

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