Impulse column w/ color and consecutive bar count


Category:
0
0

Hi Pete,

I came across a script you created which displays the current bar’s impulse color in your watchlist.  Is it possible to also include in the column the number of consecutive bars that have closed with the same color?  Below I have provided the link to the previous post as well as the code.  Thank you!

https://www.hahn-tech.com/ans/could-toss-impulse-indicator-be-utilized-as-a-wathclist-scanner/

input length = 13;
input paintBars = yes;
def EMA = ExpAverage(close, length);
def MACD = ExpAverage(close, 12) – ExpAverage(close, 26);
def MACDHist = MACD – ExpAverage(MACD, 9);
def GreenPrice = EMA > EMA[1] and MACDHist > MACDHist[1];
def RedPrice = EMA < EMA[1] and MACDHist < MACDHist[1];
AssignBackgroundColor(if !paintBars then Color.CURRENT else if GreenPrice then Color.UPTICK else if RedPrice then Color.DOWNTICK else Color.BLUE);
plot data = if GreenPrice then 1 else if RedPrice then -1 else 0;

 

Marked as spam
Posted by (Questions: 19, Answers: 18)
Asked on July 2, 2020 12:01 pm
137 views
0
Private answer

I think this should do the trick. I have not tested this yet. Just mashed it together.

input length = 13;
input paintBars = yes;
def ema = ExpAverage(close, length);
def macd = ExpAverage(close, 12) – ExpAverage(close, 26);
def macdHist = macd – ExpAverage(macd, 9);
def greenPrice = ema > ema[1] and MACDHist > MACDHist[1];
def redPrice = ema < ema[1] and MACDHist < MACDHist[1];
AssignBackgroundColor(if !paintBars then Color.CURRENT else if greenPrice then Color.UPTICK else if redPrice then Color.DOWNTICK else Color.BLUE);
rec countGreen = if greenPrice and !greenPrice[1] then 1 else if greenPrice then countGreen[1] + 1 else countGreen[1];
rec countRed = if redPrice and !redPrice[1] then -1 else if redPrice then countRed[1] - 1 else countRed[1];
plot data = if greenPrice then countGreen else if redPrice then countRed else 0;

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on July 2, 2020 6:09 pm
0
Thank you Pete. Looks like I am getting a syntax error when I load the script. rec countGreen = if greenPrice and !greenPrice[1] then 1 else if greenPrice then countGreen[1] + 1; rec countRed = if redPrice and !redPrice[1] then -1 else if redPrice then countRed[1] - 1; plot data = if greenPrice then countGreen else if redPrice then countRed else 0; Syntax error: An 'else' block expected at 9:63 Syntax error: An 'else' block expected at 10:58
( at July 2, 2020 6:36 pm)
0
I have corrected the code in my answer to remove that error.
( at July 2, 2020 8:26 pm)