I have made a few modifications to your code. One thing I noted is that you have a few lines that were not used anywhere in the final result:
def previousBarAbove = low[1] > Lower_Band;
def piercingBar = open > Lower_Band and low < Lower_Band and close > Lower_Band;
plot scan = previousBarAbove and piercingBar;
So I removed them entirely. The other thing I found was that you had three plot statements, one for each of three conditions:
plot condition = close crosses Lower_Band ;
plot condition2 = close crosses Upper_Band ;
plot condition3 = close crosses average;
Only one plot statement is permitted in a custom watchlist column. So we’ll change all of those from plot to def. Since we are using the AddLabel() method to display the value for the custom column we can’t have any plot statements at all.
Having done that, I was then able to use your three conditions in the logical if/then/else statement which dynamically sets both the text and the color of the text.
Oh, last thing. I noticed in your AddLabel() statements you applied an index value [3] to the Keltner lines. Which has the affect of reading the value of that line, three bars ago. However you did not apply this same index to the close. Which means you are checking the current bar’s close in relation to one of the Keltner lines from 3 bars ago. Seems to me that is a mistake. But I left it in there, by updating the three condition statements you were not using in your original code.
I know from experience some of our viewers are going to completely ignore all that I just said, try to use this code as is, and get unexpected results. I call this sort of declaration as an Easter Egg. Something hidden in plan site to which I can later refer when it actually happens. Gotta have some fun.
Ok, here is the final result. Screenshots included below to show what the code does.
input factor = 1.5;
input length = 20;
input price = close;
def shift = factor * Average(TrueRange(high, close, low), length);
def average = ExpAverage(price, length);
def Avg = average;
def Upper_Band = average + shift;
def Lower_Band = average – shift;
def condition = close crosses Lower_Band[3] ;
def condition2 = close crosses Upper_Band[3] ;
def condition3 = close crosses average[3];
AddLabel(yes, if condition then "Lower" else if condition2 then "Upper" else if condition3 then "20 MA" else "None", if condition then Color.YELLOW else if condition2 then Color.CYAN else if condition3 then Color.MAGENTA else Color.CURRENT);
input factor = 1.5;
input length = 20;
input price = close;
def shift = factor * Average(TrueRange(high, close, low), length);
def average = ExpAverage(price, length);
def Avg = average;
def Upper_Band = average + shift;
def Lower_Band = average – shift;
def previousBarAbove = low[1] > Lower_Band;
def piercingBar = open > Lower_Band and low < Lower_Band and close > Lower_Band;
plot scan = previousBarAbove and piercingBar;
plot condition = close crosses Lower_band ;
plot condition2 = close crosses Upper_band ;
plot condition3 = close crosses average;
AddLabel(yes, if close crosses Lower_Band[3] then “Lower ” else ” “, color.yellow);
#AddLabel(yes, if close crosses Upper_Band[3] then “Upper” else ” “, color.Cyan);
#AddLabel(yes, if close crosses average[3] then “20 MA” else ” “, color.magenta);
I am going to have to change the title of your question. “Keltner Watchlist Alerts”. This question title does not capture the entire context of your request. (and to be clear, it is not possible to generate alerts from within a watchlist column). It is certain that other viewers will be interested in finding the solution to your request. The question title will need to include more details to be sure those viewers will be able to search for and locate this post.
When I change the question title, this will break the URL of this post. I will send you an email with the new URL once the changes have been made.
The new question title will be: “Dynamically set text for multiple signals”
We have already demonstrated how to do this in at least two previous posts. However since you have provided your source code I will use that to provide a custom fit solution for your request.