Candle Count to GoldenCross/DeathCross in Chart Label

IHopeToLearn

New member
Hi, Is there a way to count back the amount of candles when there is a GoldenCross or DeathCross and add it to the Chart Label in the following code? In any Timeframe.

Currently I have the chart label as 10<50 DC or 10>50 GC. I would like one space then the number of candles back. So it would look like 10>50 GC 3.

My thinking is if I see 10>50 GC 50 it's too late to trade, but, if I see 10>50 GC 2 now I found my Breakout in time

*OPTIONAL: To be more convenient the user should be able to change the EMA's into SMA and the length in the study panel.
- THANKS

Here's the code:
def EMA10 = ExpAverage(close, 10);
def EMA50 = ExpAverage(close, 50);
def EMA100 = ExpAverage(close, 100);

def bullishStart = EMA10 > EMA50;
def bearishStart = EMA10 < EMA50;

AddLabel(yes, if bullishStart then “10>50 GC” else if bearishStart then “10<50 DC” Else “WAIT”, if bullishStart then color.light_green else if bearishStart then color.dark_orange else color.LIGHT_GRAY);

*FYI: In doing some research I found that the Death Cross and Golden Cross is based off the 50 ma crossing the 200 ma. So if I REALLY want exactly that I would have to use those parameters. However this can still work for any length one uses. Thanks
 
Last edited:
Solution
Hi, Is there a way to count back the amount of candles when there is a GoldenCross or DeathCross and add it to the Chart Label in the following code? In any Timeframe.

Currently I have the chart label as 10<50 DC or 10>50 GC. I would like one space then the number of candles back. So it would look like 10>50 GC 3.

My thinking is if I see 10>50 GC 50 it's too late to trade, but, if I see 10>50 GC 2 now I found my Breakout in time

*OPTIONAL: To be more convenient the user should be able to change the EMA's into SMA and the length in the study panel.
- THANKS

Here's the code:
def EMA10 = ExpAverage(close, 10);
def EMA50 = ExpAverage(close, 50);
def EMA100 = ExpAverage(close, 100);

def bullishStart = EMA10 > EMA50;
def bearishStart...
Hi, Is there a way to count back the amount of candles when there is a GoldenCross or DeathCross and add it to the Chart Label in the following code? In any Timeframe.

Currently I have the chart label as 10<50 DC or 10>50 GC. I would like one space then the number of candles back. So it would look like 10>50 GC 3.

My thinking is if I see 10>50 GC 50 it's too late to trade, but, if I see 10>50 GC 2 now I found my Breakout in time

*OPTIONAL: To be more convenient the user should be able to change the EMA's into SMA and the length in the study panel.
- THANKS

Here's the code:
def EMA10 = ExpAverage(close, 10);
def EMA50 = ExpAverage(close, 50);
def EMA100 = ExpAverage(close, 100);

def bullishStart = EMA10 > EMA50;
def bearishStart = EMA10 < EMA50;

AddLabel(yes, if bullishStart then “10>50 GC” else if bearishStart then “10<50 DC” Else “WAIT”, if bullishStart then color.light_green else if bearishStart then color.dark_orange else color.LIGHT_GRAY);

*FYI: In doing some research I found that the Death Cross and Golden Cross is based off the 50 ma crossing the 200 ma. So if I REALLY want exactly that I would have to use those parameters. However this can still work for any length one uses. Thanks


Code:
# barsback_to_golden_cross

#https://usethinkscript.com/threads/candle-count-to-goldencross-deathcross-in-chart-label.15823/
#Candle Count to GoldenCross/DeathCross in Chart Label

#---------------------------

def na = double.nan;
def bn = barnumber();

def price = close;

input ma1_len = 10;
input ma1_type =  AverageType.EXPONENTIAL;
def EMA10 = MovingAverage(ma1_type, price, ma1_len);

input ma2_len = 50;
input ma2_type =  AverageType.EXPONENTIAL;
def EMA50 = MovingAverage(ma2_type, price, ma2_len);

#input ma3_len = 100;
#input ma3_type =  AverageType.EXPONENTIAL;
#def EMA100 = MovingAverage(ma3_type, price, ma3_len);


def bullishStart = EMA10 crosses above EMA50;
def bearishStart = EMA10 crosses below EMA50;

def n = 1000;
def bullcnt = fold i1 = 0 to n
with p1
while !getvalue(bullishStart, i1)
do p1 + 1;

def bearcnt = fold i2 = 0 to n
with p2
while !getvalue(bearishStart, i2)
do p2 + 1;


addlabel(1, " ", color.black);
AddLabel(1, if bullishStart then “10>50 GC” else if bearishStart then “10<50 DC” Else “WAIT”, if bullishStart then color.light_green else if bearishStart then color.dark_orange else color.LIGHT_GRAY);
addlabel(1, " ", color.black);
AddLabel(1, "bull count " + bullcnt, color.light_green);
addlabel(1, " ", color.black);
AddLabel(1, "bear count " + bearcnt, color.light_red);


#--------------------------

input show_lines = yes;

plot z1 = if show_lines then ema10 else na;
z1.setdefaultcolor(getcolor(1));
#z1.setlineweight(1);
z1.hidebubble();


plot z2 = if show_lines then ema50 else na;
z2.setdefaultcolor(getcolor(2));
#z2.setlineweight(1);
z2.hidebubble();

#plot z3 = if show_lines then ema100 else na;
#z3.setdefaultcolor(getcolor(3));
##z3.setlineweight(1);
#z3.hidebubble();

#--------------------------

# alerts , sounds
alert(bullishStart, "crossed up" ,alert.BAR, sound.DING);
alert(bearishStart, "crossed down" ,alert.BAR, sound.bell);

#--------------------------

input test1 = no;
addchartbubble(test1, low,
bullcnt
# + "\n" + 
#bearcnt
, (if bullcnt == 0 then color.green else color.gray), no);

addchartbubble(test1, low,
bearcnt
, (if bearcnt == 0 then color.red else color.gray), no);

#

Vn9qpjv.jpg
 
Solution
Code:
# barsback_to_golden_cross

#https://usethinkscript.com/threads/candle-count-to-goldencross-deathcross-in-chart-label.15823/
#Candle Count to GoldenCross/DeathCross in Chart Label

#---------------------------

def na = double.nan;
def bn = barnumber();

def price = close;

input ma1_len = 10;
input ma1_type =  AverageType.EXPONENTIAL;
def EMA10 = MovingAverage(ma1_type, price, ma1_len);

input ma2_len = 50;
input ma2_type =  AverageType.EXPONENTIAL;
def EMA50 = MovingAverage(ma2_type, price, ma2_len);

#input ma3_len = 100;
#input ma3_type =  AverageType.EXPONENTIAL;
#def EMA100 = MovingAverage(ma3_type, price, ma3_len);


def bullishStart = EMA10 crosses above EMA50;
def bearishStart = EMA10 crosses below EMA50;

def n = 1000;
def bullcnt = fold i1 = 0 to n
with p1
while !getvalue(bullishStart, i1)
do p1 + 1;

def bearcnt = fold i2 = 0 to n
with p2
while !getvalue(bearishStart, i2)
do p2 + 1;


addlabel(1, " ", color.black);
AddLabel(1, if bullishStart then “10>50 GC” else if bearishStart then “10<50 DC” Else “WAIT”, if bullishStart then color.light_green else if bearishStart then color.dark_orange else color.LIGHT_GRAY);
addlabel(1, " ", color.black);
AddLabel(1, "bull count " + bullcnt, color.light_green);
addlabel(1, " ", color.black);
AddLabel(1, "bear count " + bearcnt, color.light_red);


#--------------------------

input show_lines = yes;

plot z1 = if show_lines then ema10 else na;
z1.setdefaultcolor(getcolor(1));
#z1.setlineweight(1);
z1.hidebubble();


plot z2 = if show_lines then ema50 else na;
z2.setdefaultcolor(getcolor(2));
#z2.setlineweight(1);
z2.hidebubble();

#plot z3 = if show_lines then ema100 else na;
#z3.setdefaultcolor(getcolor(3));
##z3.setlineweight(1);
#z3.hidebubble();

#--------------------------

# alerts , sounds
alert(bullishStart, "crossed up" ,alert.BAR, sound.DING);
alert(bearishStart, "crossed down" ,alert.BAR, sound.bell);

#--------------------------

input test1 = no;
addchartbubble(test1, low,
bullcnt
# + "\n" +
#bearcnt
, (if bullcnt == 0 then color.green else color.gray), no);

addchartbubble(test1, low,
bearcnt
, (if bearcnt == 0 then color.red else color.gray), no);

#

Vn9qpjv.jpg
Thank you so much halcyonguy. As I was checking out what you wrote it became apparent to me that I didn't think of the best way to use this. So I am wondering is it possible to add the count number to the candle top or bottom (and I guess in it's appropriate color to represent a bullish/bearish move) and still leave the chart label as is? It would make it SO Much faster to find it.

FYI: I reinstated the 200 MA so that I have a correct Golden Cross 50ema > 200 and a Death Cross 50ema < 200.

Also I need this to be accurate regardless of the TimeFrame.

I use the Trendline tool to read the bars.

SCREEECCCCHHH.... I thought everything was perfect, and then I was on a 1y 1D and found some discrepancies (AAPL 6/30/23) then worse on a 1d 1m, the count seems to be off.

Please HELP

Here's the current code:

# barsback_to_golden_cross

#https://usethinkscript.com/threads/candle-count-to-goldencross-deathcross-in-chart-label.15823/
#Candle Count to GoldenCross/DeathCross in Chart Label

#---------------------------

def na = double.nan;
def bn = barnumber();

def price = close;

input ma1_len = 10;
input ma1_type = AverageType.EXPONENTIAL;
def EMA10 = MovingAverage(ma1_type, price, ma1_len);

input ma2_len = 50;
input ma2_type = AverageType.EXPONENTIAL;
def EMA50 = MovingAverage(ma2_type, price, ma2_len);

#I UNcommented
input ma3_len = 200;
input ma3_type = AverageType.EXPONENTIAL;
def EMA200 = MovingAverage(ma3_type, price, ma3_len);

def bullishStart = EMA10 crosses above EMA50;
def bearishStart = EMA10 crosses below EMA50;

#I added
def bull10up = EMA10 crosses above EMA200;
def bear10dn = EMA10 crosses below EMA200;
def GC = EMA50 crosses above EMA200;
def DC = EMA50 crosses below EMA200;

def n = 1000;
def bullcnt = fold i1 = 0 to n
with p1
while !getvalue(bullishStart, i1)
do p1 + 1;

def bearcnt = fold i2 = 0 to n
with p2
while !getvalue(bearishStart, i2)
do p2 + 1;

#I added
def bullcntGC = fold i3 = 0 to n
with p3
while !getvalue(GC, i3)
do p3 + 1;

def bearcntDC = fold i4 = 0 to n
with p4
while !getvalue(DC, i4)
do p4 + 1;

#I added
def bullcnt10 = fold i5 = 0 to n
with p5
while !getvalue(GC, i5)
do p5 + 1;

def bearcnt10 = fold i6 = 0 to n
with p6
while !getvalue(DC, i6)
do p6 + 1;



addlabel(1, " ", color.black);
#AddLabel(1, if bullcnt then “10>50” else if bearcnt then “10<50” Else “WAIT”, if bullcnt then color.light_green else if bearcnt then color.dark_orange else color.LIGHT_GRAY);

#AddLabel(1, if bullcnt then “50>200 GC” else if bearcnt then “50<200 DC” Else “WAIT”, if bullcnt then color.light_green else if bearcnt then color.dark_orange else color.LIGHT_GRAY);

addlabel(1, " ", color.black);
AddLabel(1, "10>50 " + bullcnt, color.light_green);
#addlabel(1, " ", color.black);
AddLabel(1, "10<50 " + bearcnt, color.light_red);

#I added
AddLabel(1, "10>200 GC " + bullcnt10, color.light_green);
AddLabel(1, "10<200 DC " + bearcnt10, color.light_red);

addlabel(1, " ", color.black);
AddLabel(1, "50>200 GC " + bullcntGC, color.light_green);
#addlabel(1, " ", color.black);
AddLabel(1, "50<200 DC " + bearcntDC, color.light_red);

#--------------------------

input show_lines = yes;

plot z1 = if show_lines then ema10 else na;
z1.setdefaultcolor(getcolor(1));
z1.setlineweight(1);
z1.hidebubble();


plot z2 = if show_lines then ema50 else na;
z2.setdefaultcolor(getcolor(2));
z2.setlineweight(1);
z2.hidebubble();

plot z3 = if show_lines then ema200 else na;
z3.setdefaultcolor(getcolor(3));
z3.setlineweight(1);
z3.hidebubble();

#--------------------------

# alerts , sounds
#alert(bullishStart, "crossed up" ,alert.BAR, sound.DING);
#alert(bearishStart, "crossed down" ,alert.BAR, sound.bell);

#--------------------------

input test1 = no;
addchartbubble(test1, low,
bullcnt
# + "\n" +
#bearcnt
, (if bullcnt == 0 then color.green else color.gray), no);

addchartbubble(test1, low,
bearcnt
, (if bearcnt == 0 then color.red else color.gray), no);

#
ChartLabelsHalcyonguy.png
 
Code:
# barsback_to_golden_cross

#https://usethinkscript.com/threads/candle-count-to-goldencross-deathcross-in-chart-label.15823/
#Candle Count to GoldenCross/DeathCross in Chart Label

#---------------------------

def na = double.nan;
def bn = barnumber();

def price = close;

input ma1_len = 10;
input ma1_type =  AverageType.EXPONENTIAL;
def EMA10 = MovingAverage(ma1_type, price, ma1_len);

input ma2_len = 50;
input ma2_type =  AverageType.EXPONENTIAL;
def EMA50 = MovingAverage(ma2_type, price, ma2_len);

#input ma3_len = 100;
#input ma3_type =  AverageType.EXPONENTIAL;
#def EMA100 = MovingAverage(ma3_type, price, ma3_len);


def bullishStart = EMA10 crosses above EMA50;
def bearishStart = EMA10 crosses below EMA50;

def n = 1000;
def bullcnt = fold i1 = 0 to n
with p1
while !getvalue(bullishStart, i1)
do p1 + 1;

def bearcnt = fold i2 = 0 to n
with p2
while !getvalue(bearishStart, i2)
do p2 + 1;


addlabel(1, " ", color.black);
AddLabel(1, if bullishStart then “10>50 GC” else if bearishStart then “10<50 DC” Else “WAIT”, if bullishStart then color.light_green else if bearishStart then color.dark_orange else color.LIGHT_GRAY);
addlabel(1, " ", color.black);
AddLabel(1, "bull count " + bullcnt, color.light_green);
addlabel(1, " ", color.black);
AddLabel(1, "bear count " + bearcnt, color.light_red);


#--------------------------

input show_lines = yes;

plot z1 = if show_lines then ema10 else na;
z1.setdefaultcolor(getcolor(1));
#z1.setlineweight(1);
z1.hidebubble();


plot z2 = if show_lines then ema50 else na;
z2.setdefaultcolor(getcolor(2));
#z2.setlineweight(1);
z2.hidebubble();

#plot z3 = if show_lines then ema100 else na;
#z3.setdefaultcolor(getcolor(3));
##z3.setlineweight(1);
#z3.hidebubble();

#--------------------------

# alerts , sounds
alert(bullishStart, "crossed up" ,alert.BAR, sound.DING);
alert(bearishStart, "crossed down" ,alert.BAR, sound.bell);

#--------------------------

input test1 = no;
addchartbubble(test1, low,
bullcnt
# + "\n" +
#bearcnt
, (if bullcnt == 0 then color.green else color.gray), no);

addchartbubble(test1, low,
bearcnt
, (if bearcnt == 0 then color.red else color.gray), no);

#

Vn9qpjv.jpg
Looks great so far. Thank YOU! I am looking at it afterhours, so I don't know if that is why I don't see the count number on top of the candles feature.

"...the count number to the candle top or bottom (and I guess in it's appropriate color to represent a bullish/bearish move) and still leave the chart label as is? It would make it SO Much faster to find it."

Please advise and again thanks for your help

UPDATE:
I found something you did another time that looks very promising for putting the count number in bubbles on top of the candles, I tried to see if I could put it together, but it's way over my head. I hope this helps make it easy. Thanks...https://usethinkscript.com/threads/difficulty-coding-coloring-inside-candle-formations.15820/post-127558
 
Last edited by a moderator:

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