I moved this out of the "Alert and Notifications" topic and into the "Watch Lists" topic since that is what you are requesting.
I like the code you included because it taught me something new. I did not realize we could add more than one "AddLabel()" statement to a custom watchlist column. In the past I would use only one "AddLabel()" statement and then use the if/then/else logic to dynamically set the text or value of the label. So thanks for teaching me something. I always love to learn new things.
So if you wanted to modify that to only display the signal after it has locked in and cannot change we need to shift the code to look at the previous bar for the signal instead of the current bar. We do this using something called an index: [1]. If no index is applied, the default index value is zero [0]. Which means the current bar. Taking this concept a bit further, we have the following:
- Current bar: buy[0]
- Previous bar: buy[1]
- The bar before item 2: buy[2]
Next, I will modify the AddLabel() statements from your code to shift them one bar to the left (previous bar):
AddLabel(buy[1], “Buy Fired”, color.black);
AddLabel(sell[1], “Sell Fired”, color.black);
AddLabel(!buy[1] and !sell[1], ” “, color.black);
If you wanted to have the signal persist for several bars after the signal has locked in, we need to have the code look backwards a number of bars. This solution is a bit more complex. I won't take the time to explain this:
AddLabel(Highest(buy[1], 5) > 0, “Buy Fired”, color.black);
AddLabel(Highest(sell[1], 5) > 0, “Sell Fired”, color.black);
AddLabel(Highest(buy[1], 5) < 1 and Highest(sell[1], 5) < 1, ” “, color.black);
This example will look back at the previous 5 bars (excluding the current bar because [1]), and display if any signals were present within that span. What happens if you get a both a buy and a sell within those 5 bars? Well if you need to account for that we need to get much more complex with our solution. That is beyond the scope of what I can cover here in the Q&A forum.
In each of these examples I have only displayed the lines from your code that need to be modified. You will still need to include all the other lines in your code as they are.