Hey all, I have a simple Stacked EMA script in which, I'm looking to add a label that displays number of periods (in this case period is days) that the StackedEMA condition was true.
Any ideas on how to accomplish that? I tried something like
sumBullish = Sum(bullish,365)
but that looked back and counted the total number of days in the last 365 where the EMAs were stacked. I only want to look back and count the total number of recent days where the stack was bullish... as soon as it finds a bearish day the count should stop.
Any ideas on how to accomplish that? I tried something like
sumBullish = Sum(bullish,365)
but that looked back and counted the total number of days in the last 365 where the EMAs were stacked. I only want to look back and count the total number of recent days where the stack was bullish... as soon as it finds a bearish day the count should stop.
Code:
def EMA8 = ExpAverage(close, 8);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);
def EMA55 = ExpAverage(close, 55);
def bullish = EMA8 > EMA21 and EMA21 > EMA34 and EMA34 > EMA55;
def bearish = EMA8 < EMA21 and EMA21 < EMA34 and EMA34 < EMA55;
## Replace label value with the number of consecutive days
## that the bullish/bearish stack has been in effect
## eg if the EMAs have been stacked for the last 10
## consecutive days than the label should show "10"
AddLabel(bullish,"Bullish", color.black);
AddLabel(bearish,"Bearish, color.black);
AddLabel(!bullish and !bearish," ", color.black);
AssignBackgroundColor(if bullish then color.green else if bearish then color.red else color.black);