I’m glad to see you made an attempt to work this out on your own. Your second attempt is very close.
Which is this: AddLabel(yes ,”SSR”, if ssr1 < (close(period="day")[1]*.9) then color.GREEN );
You are only missing the “else” part of the “if/then/else” statement. All three elements are required. You begin by testing for your condition after the “if” element. You then assign a value after the “then” element. But if you don’t include the “else” element you will get an error. Because once you have the “if” and the “then” in place you must include the “else”.
You really don’t need this portion: def ssr1= last;
In fact that actually has the potential to create an error. In Thinkscript, you should always use “close” to get the current price. “last” really only exists as a built-in column used in watchlists.
So one of the updates we need to do is to get rid of that line, and replace your variable “ssr1” with “close”. The second update we need to do is to complete the “if/then/else” statement.
AddLabel(yes,”SSR”, if close < close(period = "day")[1] * 0.9 then Color.GREEN else Color.GRAY);
If you look carefully you will see I took the time to correct several other minor details. But none of those changes were required for proper functioning. Only for proper coding practices.
So to be clear, we check the close of the current bar on the chart and compare its value to the previous day’s close time 0.9. This code seems to be intended to run on an intraday chart. It will work on a daily chart. But it will fail on anything above the daily time frame.
We need to improve this title: “SSR Label”. Nobody is going to benefit from this post because that title will not mean anything to them at all.
How to write a really good question title: What are you actually trying to do? You are trying to set the color of a label based on a condition. So a great question title for this post looks like this: “dynamically set label color based on condition”
But we have already demonstrated this numerous times in the forum. But not in the “Alerts and Notifications” topic. Which is the topic you used for this post.
This post belongs in the “Chart Studies” topic. Take some time to search the Chart Studies topic for the term “Label” and see if you can find your solution. If you don’t find a solution, add a comment below mine and I will make the required changes explained above and provide a link to the solution, or a new piece of code fit to this request.
Hi Pete – sorry for misplacing this question. Also, tried editing the title but got a blank box non-editable.
I’ve done the research in the ‘Chart Studies’ section and found a few indications of how to re-write the code but I could not get it to work. Below is what I have updated. Hope you can help.
def ssr1= last;
AddLabel(yes ,”SSR”, if ssr1 < (close(period="day")[1]*.9) then color.GREEN );