How to define string variable?

marketstreet

New member
Can I add up-and-down arrows or other non-standard characters to chart labels?

I'm writing a multi time frame study which uses the AddLabel function to plot a set of minimal color-coded labels in the top left corner of the chart. To preserve chart real estate and make the data as intuitive and easy to analyze as possible, instead of text words, I wish to use up-&-down arrows ( ↑↓ ) to indicate when that aggregation period's candle meets certain conditions.

The time frame labels will appear simply as H D W M Q Y followed by the appropriate characters.

Now, I can copy-&-paste a unicode arrow directly into the AddLabel string, which looks perfect on my Mac; but for Windows users, any special character renders as either a question mark (? ?) or blank boxes ([] []).

The label's text is written into a string ("only text goes in this string") and is not generated using an operator of the thinkscript language itself, so I cannot just insert html/ascii/unicode/alt code instructions directly because it will render identically as text. I am writing this study for a group of non-technical trader friends and I do not have access to Windows at home, so I cannot test this on my own. Have you attempted this or found a solution that will render across operating systems? Thank you for your time.

Code:
#Example:
AddLabel(oneHour, "<<<insert character here>>>", color.UPTICK);
-

AddLabel(oneHour, "↑↓", color.UPTICK);
#Renders "??" or "[][]" on Windows

AddLabel(oneHour, "&uarr;", color.UPTICK);
#Renders "&uarr;" on Mac (and I assume any OS)

AddLabel(oneHour, "^ v ', `.", color.UPTICK);
#I've tried many standard character substitutes, but they are unnatural and confusing.
 
Hi folks- just started playing with thinkscript today, and came up with a script to show the ATR, 10% of ATR & 2% of ATR, with inputs for # of periods (starting at current period price, going backwards) and the type of average (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, 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:
Code:
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);
 
Last edited by a moderator:
Got it figured out, with the help of Pete at Hahn-Tech. I have a snapshot, but here's the code for anyone interested:

Code:
# dec DFKTMA == "SimpleScript";
# by DFK & Associates.  Copyright 2020. This code is distributed freely without restriction.  Use in any way absolves author of any and all liability.
def AvgTrueRange;
def AvgTrueRange10;
def AvgTrueRange2;

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);
 
Last edited by a moderator:
This gives an error. Expects a number

Code:
def myStr = "ASDF";

You can use

Code:
input myStr = "ASDF";

but this cannot be reassigned a new value within the script.
 
Is it possible to create nested IF like below?

Code:
If condition A met then
     if condition B met then
        plot signal = close;
     else
        DoNothing
else
    DoNothing

Basically I need to execute the logic in sequence. Condition 1 -> Condition 2 -> Condition 3.
If any condition is failed (ex- condition 2 then exit the loop and go to next item).
Thanks.
 
Last edited:
@mitz27 Why not just use "and"...???

Ruby:
If <condition A met> and <condition B met> then plot signal = close else DoNothing;

In your example above you wouldn't have a semicolon anywhere except at the end of the entire logic structure... There are many examples of complex "if . . . then . . . else . . ." and "if . . . then . . . else if . . . then . . . else . . ." conditional structures in Thinkscripts here in these forums as well as in the Learning Center... Specifically, check out the "if" expression... Hope this helps...

Also, you don't put a plot statement in a conditional structure...
 
@mitz27 The short answer is yes, TOS supports nested if statements. However, as @rad14733 pointed out reserved words such as plot and def cannot be 'nested'. Also, when using a plot statement unless plotting a line, we have to state when to plot and where to plot. Therefore:

Code:
def criteria =
if condition A met then
     if condition B met then
        1
     else
        0
else
    0;

plot signal = if criteria == 1 then close else Double_NaN;
 
Last edited:
I'm running into the same road block, were you able to find the answer?

There is no solution... TOS allows us to us input for symbol selection and a few other string related tasks but we cannot dynamically assign strings to def's within our scripts due to type-checking constraints...
 
There is no solution... TOS allows us to us input for symbol selection and a few other string related tasks but we cannot dynamically assign strings to def's within our scripts due to type-checking constraints...
So sad. Dynamically assigning strings to def's makes sense for many things people might try to do with the language.
 
You might be able to provide a user input in the script which allows them to select whether they are running PC or Mac. You would then create a pair of labels, one for PC, and one for Mac, and switch their visibility on or off depending on the setting. The issue would be getting the PC character's equivalents over to you on Mac, and possibly plugging them in. They might appear as boxes or question marks over on your end too, or it might not work at all. Worth a shot.

Try loading this and tell me what happens on your mac, its a script with a label for the up arrow, and a second label for the down arrow. You might be able to copy and paste what ever junk character it shows up as on your end.

https://tos.mx/byzjUpS

By the way, could you share a similar script that provides the mac characters? I want to see if I can paste them into visual studio and see if I can pull their character numbers, etc. Though, I really have no idea how Macs handle fonts and characters and the like. I always assumed it was more standardized.
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
231 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top