Count bars for stacked moving averages


Category:
0
0

Hello Pete,

I have a stacked EMA watchlist column script(file attached) which shows when the 8,21,34,55,&89 EMA are stacked in that order. I want to add a counter to show the number of consecutive stacked bars. You can remove the labels Bl and Br for bullish and bearish if necessary.

def EMA8 = ExpAverage(close, 8);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);
def EMA55 = ExpAverage(close, 55);
def EMA89 = ExpAverage(close, 89);

def bullish = EMA8 > EMA21 and EMA21 > EMA34 and EMA34 > EMA55 and EMA55 > EMA89;
def bearish = EMA8 < EMA21 and EMA21 < EMA34 and EMA34 < EMA55 and EMA55 < EMA89;

ADDlabel(bullish, “Bl”, color.blACK);
ADDlabel(bearish, “Br”, color.blACK);
ADDlabel(!bullish and!bearish, “”, color.black);

AssignBackgroundColor (if bullish then color.grEEN else if bearish then color.reD else color.wHITE);

 

Attachments:
Marked as spam
Posted by (Questions: 3, Answers: 0)
Asked on December 28, 2023 10:16 am
88 views
0
Private answer

Since you are requesting a solution for a custom watchlist column, I moved your question out of the "Frequently Asked Questions" topic and into the "Watch Lists" topic.

This topic of counting the number of bars that a condition has been true has already been covered multiple times in this forum. The solution is very much the same regardless of the method used to define the true/false condition.

def condition = <<code which defines true/false condition>>;
rec countCondition = if condition and !condition[1] then 1 else if condition then countCondition[1] + 1 else 0;

That's really all there is to it. You can copy and paste that into any code and substitute your own true/false condition in the first line. The solution becomes a bit more complex when you have two or more conditions you want to track. But the basic structure of counting bars since condition remains the same.

If you want to learn how to apply the technique to a broader range of scenarios you can search the forum and find many examples. But here are a couple that come very close to matching this current request:

https://www.hahn-tech.com/ans/display-number-of-bars-since-condition-was-true/

https://www.hahn-tech.com/ans/display-bars-since-cross-above-hma/

Here is the solution to your request. I have updated the variable names for the individual moving averages because other viewers might want to use a completely different set of values for each of those.

def emaOne = ExpAverage(close, 8);
def emaTwo = ExpAverage(close, 21);
def emaThree = ExpAverage(close, 34);
def emaFour = ExpAverage(close, 55);
def emaFive = ExpAverage(close, 89);
def bullish = emaOne > emaTwo and emaTwo > emaThree and emaThree > emaFour and emaFour > emaFive;
def bearish = emaOne < emaTwo and emaTwo < emaThree and emaThree < emaFour and emaFour < emaFive;
rec countBullish = if bullish and !bullish[1] then 1 else if bullish then countBullish[1] + 1 else 0;
rec countBearish = if bearish and !bearish[1] then -1 else if bearish then countBearish[1] - 1 else 0;
plot data = if countBullish <> 0 then countBullish else if countBearish <> 0 then countBearish else 0;
data.AssignValueColor(if data <> 0 then Color.BLACK else Color.CURRENT);
AssignBackgroundColor(if data > 0 then Color.GREEN else if data < 0 then Color.RED else Color.CURRENT);

I have included a screenshot below showing a grid of three charts which shows ticker symbols from the embedded watchlist.

 

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4117)
Answered on December 28, 2023 10:39 am