Apply if condition within an AddLabel statement


Category:
0
0

I have the following code but for some reason even if one of the conditions is not true in the if clause, its still displaying “A” in the chart instead of “”. For e.g. if Price_Var is 5.09, then none of the below conditions should be valid and it should return “” but its still showing “A”  as the label.. For some reason only A is displayed and not other ones. Can you please help in fixing this code? 

AddLabel(yes, If (Price_Var < 5 and Price_Low_Perc between 0 and 2) then “A” else “”);

AddLabel(yes, if (Price_Var < 5 and Price_Low_Perc between 2.01 and 10) then “B” else “”);

AddLabel(yes, if (Price_Var < 5 and Price_Low_Perc > 10.01) then “C” else “”);

AddLabel(yes, if (Price_Var < 5 and Price_Low_Perc < 0) then “D” else “”);

 

Please let me know if you need more details.

RESOLVED
Marked as spam
Posted by (Questions: 3, Answers: 7)
Asked on December 23, 2020 6:30 am
1385 views
0
Private answer

I updated the title of your question to reflect the true issue you are trying to overcome. Which is how to apply an if/then/else condition to an AddLabel() statement. You have four lines of code in your attempted solution. Those are not one label being controlled through several if/then/else conditions. Those are four separate AddLabel() statements which would add four separate labels to your chart.

AND

You did not include the rest of code. There is nothing here that I can fix because whatever variables you are using in those AddLabel() statements have not been declared.

You can spend about 15 minutes doing a search on this forum and find dozens of examples showing how to use the AddLabel() statement and dynamically set both the text as well as the color of the chart labels based on conditions.

But instead, let's go straight to the source. The language reference for Thinkorswim:

https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddLabel

In that page you will find an example of a chart label that changes the text displayed in the label based on relative position of the current price and the moving average. If you want to add more complexity to this you must do it all within the same AddLabel() statement.

AddLabel(yes, if close > open then if close > Average(close, 20) then "Uptrend Green" else "Downtrend Green" else if close > Average(close, 20) then "Uptrend RED" else "Downtrend RED");

That line of code creates an label that displays text based on four conditions. The candle color and the position of the close above or below the moving average.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on December 23, 2020 9:59 am
0
Thanks Pete. This is very helpful. Truly appreciate this.
( at December 29, 2020 6:55 am)