Watchlist color for MACDTwoLines


Category:
0
0

I want to expand on this MACD watchlist column code that shows the when the value line is greater than the average line, it shows a green background color and vice versa(average greater than vale is red background).

I want to show  green background when value and average > 0 ; and value > average

Red when value and average < 0 ; and value < average

Light Red when value and average > 0 ; and value < average

Light green when value and average < 0 ; and value > average

 

Attachments:
Marked as spam
Posted by Tyrell Morris (Questions: 1, Answers: 1)
Asked on June 4, 2026 8:14 am
5 views
0
Private answer

I'm surprised this one hasn't been done before. Should be a very popular solution. The color scheme you describe is similar to the rules used to color the MACD histogram. But you have applied that to the two lines of the MACD instead. The Value line and the Average line. Interesting.

Here is the solution:

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 colorGreen = value > 0 and avg > 0 and value > avg;
def colorRed = value < 0 and avg < 0 and value < avg;
def colorLightRed = value > 0 and avg > 0 and value < avg;
def colorLightGreen = value < 0 and avg < 0 and value > avg;
plot sortValue = if colorGreen then 2 else if colorRed then -2 else if colorLightGreen then 1 else if colorLightRed then -1 else 0;
sortValue.AssignValueColor(if sortValue <> 0 then Color.BLACK else Color.CURRENT);
AssignBackgroundColor(if colorGreen then Color.GREEN else if colorRed then Color.RED else if colorLightGreen then Color.LIGHT_GREEN else if colorLightRed then Color.LIGHT_RED else Color.CURRENT);

Edit: It was brought to my attention that I did not include the logic for counting the bars in a similar method to how the original source code counted bars. The original request lacked a clear definition of how those bars should be counted using this new color scheme. So I came up with a method that seemed right to me.

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 colorGreen = value > 0 and avg > 0 and value > avg;
def colorRed = value < 0 and avg < 0 and value < avg;
def colorLightRed = value > 0 and avg > 0 and value < avg;
def colorLightGreen = value < 0 and avg < 0 and value > avg;
rec countGreen = if colorGreen and !colorGreen[1] then 1 else if colorGreen then countGreen[1] + 1 else 0;
rec countLightGreen = if colorLightGreen and !colorLightGreen[1] then 1 else if colorLightGreen then countLightGreen[1] + 1 else 0;
rec countRed = if colorRed and !colorRed[1] then 1 else if colorRed then countRed[1] + 1 else 0;
rec countLightRed = if colorLightRed and !colorLightRed[1] then 1 else if colorLightRed then countLightRed[1] + 1 else 0;
plot sortValue = Max(countGreen, Max(countLightGreen, Max(countRed, countLightRed)));
sortValue.AssignValueColor(if sortValue <> 0 then Color.BLACK else Color.CURRENT);
AssignBackgroundColor(if colorGreen then Color.GREEN else if colorRed then Color.RED else if colorLightGreen then Color.LIGHT_GREEN else if colorLightRed then Color.LIGHT_RED else Color.CURRENT);

Marked as spam
Posted by Pete Hahn (Questions: 37, Answers: 4154)
Answered on June 4, 2026 8:33 am