MACD column with background color and count


Category:
0
0

Hi Pete,

In a previous post you provided a code which allows us to view the current background color of the MACD histogram. Is it possible to plot the number of consecutive bars with the same color instead of plotting the histogram difference?

Below I have provided a link to the previous post as well as the code.  Thank you very much.

https://www.hahn-tech.com/ans/macd-watchlist-background-color/

plot diff = Value - avg;
AssignBackgroundColor(if diff >= 0 then if diff > diff[1] then
Color.GREEN else Color.DARK_GREEN else if diff < diff[1] then
Color.RED else Color.DARK_RED);

Marked as spam
Posted by (Questions: 19, Answers: 18)
Asked on August 26, 2021 9:17 am
377 views
0
Private answer

I'm glad you provided the link to the previous post so the rest of our viewers will be able to understand the context of that previous request. But more importantly because the code you provided is not complete. You left off the first six lines from the code in the previous solution and it will not work without those lines.

Here is the code you requested:

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
def value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def avg = MovingAverage(averageType, value, MACDLength);
def diff = Value - avg;
def colorGreen = diff >= 0 and diff > diff[1];
def colorDarkGreen = diff >= 0 and diff < diff[1];
def colorRed = diff < 0 and diff < diff[1];
def colorDarkRed = diff < 0 and diff > diff[1];
rec countGreen = if colorGreen and !colorGreen[1] then 1 else if colorGreen then countGreen[1] + 1 else 0;
rec countDarkGreen = if colorDarkGreen and !colorDarkGreen[1] then 1 else if colorDarkGreen then countDarkGreen[1] + 1 else 0;
rec countRed = if colorRed and !colorRed[1] then 1 else if colorRed then countRed[1] + 1 else 0;
rec countDarkRed = if colorDarkRed and !colorDarkRed[1] then 1 else if colorDarkRed then countDarkRed[1] + 1 else 0;
plot data = if colorGreen then countGreen else if colorDarkGreen then countDarkGreen else if colorRed then countRed else if colorDarkRed then countDarkRed else 0;
data.AssignValueColor(if data <> 0 then Color.BLACK else Color.CURRENT);
AssignBackgroundColor(if colorGreen then Color.GREEN else if colorDarkGreen then Color.DARK_GREEN else if colorRed then Color.Red else if colorDarkRed then Color.DARK_RED else Color.CURRENT);

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on August 26, 2021 12:06 pm