display a string of text in a label based on function value


Category:
0
0

Hi Pete – I started my move over to TOS, and watched your videos on multi monitor setups – very helpful!

I just started playing with thinkscript today, as I have a few studies I want to create.  My first is rather simple – it is a script to show a label with the ATR, 10% of ATR & 2% of ATR.

The script has inputs for # of periods (starting at current period price, going backwards) and the type of average (averageType = Simple, Exponential, Weighted, Wilders and Hull). It seems to match the TOS provided ATR study at the last candle (which is what I want), and it takes up a lot less screen space than a chart over time for the ATR, which is the reason for the script.

I have it all working, except when I echo the averageType, it shows a number, which I think is based on an internal conversion the interpreter is doing (Simple = 0, Exponential = 1, Weighted = 2, Wilders = 3 and Hull = 4), when I change inputs in the Customizing Study dialog.

I am trying to build the logic with a variable to display a string that is set, based on the number that averageType contains, but for the life of me I can’t get the syntax right… (for example, if the average type = Simple, display “Simple” in the label, rather than the “0” that is displayed)

most recent effort was to declare the DFKTMA variable, and then set it to equal the string I want, using nested If thens, but I can’t get even a basic 2 conditional to work.

am I attacking this the right way?

the commented lines are just the last iteration of about 8 different ways I have tried to set the variable DFKTMA to a string that says “Simple”, rather than the “0” that is is displayed in the label.

here is the script:

*******************************
def AvgTrueRange1;
def AvgTrueRange2;
def AvgTrueRange3;
#def DFKTMA = “Simple”;

input length = 14;
input averageType = AverageType.WILDERS;
# DFKTMA=Wilders if averageType=0 then DFKTMA==Wilders else DFKTMA==test;

#if (averageType == “3”, DFKTMA == “Wilders”, “Test”) ;
# def DFKTMA = “Simple” If (averageType = AverageType.SIMPLE, Else DFKTMA == “test”;

AvgTrueRange1 = MovingAverage(averageType, TrueRange(high, close, low), length);
AvgTrueRange2 = AvgTrueRange1 / 10;
AvgTrueRange3 = AvgTrueRange1 / 50;

AddLabel(yes, Concat(” Type of MovingAverage: “, Concat(averageType, Concat(” # of Periods: “, Concat(length, Concat(” ATR: “, Concat(AvgTrueRange1, Concat(” ATR/10%: “, Concat(AvgTrueRange2, Concat(” ATR/2%: “, AvgTrueRange3))))))))), Color.WHITE);
*******************************

Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on April 5, 2020 3:12 pm
218 views
1
Private answer

thanks much Pete

yet another quirky interpreted environment :)

Here's the working code:

<pre>

def AvgTrueRange;
def AvgTrueRange10;
def AvgTrueRange2;
# dec DFKTMA == "SimpleScript";

input length = 14;
input AverageType = AverageType.WILDERS;

AvgTrueRange = MovingAverage(AverageType, TrueRange(high, close, low), length);
AvgTrueRange10 = AvgTrueRange / 10;
AvgTrueRange2 = AvgTrueRange / 50;

AddLabel(yes, Concat(" Type of Moving Average: ", Concat((if AverageType == AverageType.Wilders then "Wilders" else if AverageType == AverageType.Simple then "Simple" else if AverageType == AverageType.Hull then "Hull" else if AverageType == AverageType.Exponential then "Exponential" else "Weighted"), Concat(" # of Periods: ", Concat(length, Concat(" ATR: ", Concat(AvgTrueRange, Concat(" 10% ATR: ", Concat(AvgTrueRange10, Concat(" 2% ATR: ", AvgTrueRange2))))))))), Color.WHITE);

<pre/>

 

Marked as spam
Posted by (Questions: 2, Answers: 1)
Answered on April 5, 2020 7:36 pm
0
Private answer

There are tons of examples in the Q&A forum showing how to set chart labels. Included in these examples are several that show how to set the text of a label dynamically. I suggest you take some time to review these examples by searching for the term "label" in the forum.

The following link is the results you get when you search for that time in the Chart Studies topic:

https://www.hahn-tech.com/ans/cat/studies/?question_type=all&search=label

You cannot use the input named "averageType" in your expression. That will result in what the language refers to as a "Constant". Which are gernally expressed as numerical values. So you will need to include language within your code to handle every possible selection for the average type.

For example:

AddLabel(yes, if averageType == AverageType.EXPONENTIAL then "Exponential" else if averageType = AverageType.SIMPLE then "Simple" else "Other", Color.WHITE);

There is not simple solution to your problem. You have to employ what I call "brute force" techniques.

Marked as spam
Posted by (Questions: 37, Answers: 4086)
Answered on April 5, 2020 4:08 pm