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);
#
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);
#