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

Canyon135

New member
VIP
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!
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
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);
 

Canyon135

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

Similar threads

Top