Help with forming a string for AddLabel

raj2ras

New member
I am new to ThinkScript and trying to print a label for chart that says for e.g.

I want: $diff Close - EDMA21 = 4.50
I get: $diff 0-50 = 4.50

The challenge is, how do I create the string to display? Seems like I need some advanced conditional statements. If I try to concat the variables it just prints numbers?
:confused::unsure:


Code:
input price = close;
input Aggregation = AggregationPeriod.DAY;
input AvgType = AverageType.SIMPLE;
input Length = 50;

def diff = price - MovingAverage(AvgType, close(period = Aggregation), Length);
#def lname = concat(price, Aggregation + AvgType + Length );

#Labels
AddLabel(diff, Concat("$diff " ,AvgType + "-" + Length + " = " + Round(diff, 2)), CreateColor(107, 180, 4));

#AddLabel(diff, Concat("$diff " ,concat(price, Aggregation + AvgType + Length ) + " = " + Round(diff, 2)), CreateColor(107, 180, 4));
 

rad14733

Well-known member
VIP
@raj2ras I'm not quite getting what your trying to explain... It sounds like you are confused because you haven't adequately research the AddLabel() function in the Thinkscript Learning Center... The examples there thoroughly explain how to concatenate strings with data... And if you need more examples, simply review the hundreds, if not thousands, of Thinkscripts here in these forums to see how others have accomplished this task...

In all sincerity, the use of the Concat() function is awkwardly redundant as you can accomplish more without it than with it... That may be the source of your confusion... Just use the + sign between strings and numbers and be sure to add " " between numbers and operators to negate math calculations where you don't want them...

One problem you will encounter it that AvgType will display then entire AverageType.Type unless you add another line of code... Again, you can learn a lot by checking how others do it... I'm not going to give you the entire answer because I can see that you are working at this and I'm sure you can find the correct solution with a bit of effort on your part... Keep at it and post again if you need more help... Learning works better when you do the work...

Keep at it...
 

halcyonguy

New member
I am new to ThinkScript and trying to print a label for chart that says for e.g.

I want: $diff Close - EDMA21 = 4.50
I get: $diff 0-50 = 4.50

The challenge is, how do I create the string to display? Seems like I need some advanced conditional statements. If I try to concat the variables it just prints numbers?
:confused::unsure:


Code:
input price = close;
input Aggregation = AggregationPeriod.DAY;
input AvgType = AverageType.SIMPLE;
input Length = 50;

def diff = price - MovingAverage(AvgType, close(period = Aggregation), Length);
#def lname = concat(price, Aggregation + AvgType + Length );

#Labels
AddLabel(diff, Concat("$diff " ,AvgType + "-" + Length + " = " + Round(diff, 2)), CreateColor(107, 180, 4));

#AddLabel(diff, Concat("$diff " ,concat(price, Aggregation + AvgType + Length ) + " = " + Round(diff, 2)), CreateColor(107, 180, 4));
to display the type of average, i would add an if then in the addlabel, to compare avgtype to the possible values, to pick a text value
if AvgType == AverageType.EXPONENTIAL then "EMA"
 

rad14733

Well-known member
VIP
to display the type of average, i would add an if then in the addlabel, to compare avgtype to the possible values, to pick a text value
if AvgType == AverageType.EXPONENTIAL then "EMA"

Correct... You can either use a complex logic structure or simply use a separate line for each AverageType and AggregationPeriod so they display as strings...
 

Similar threads

Top