1

I am a beginning thinkscript programmer and I am learning the syntax of thinkscript pretty fast. However, I am having trouble with the if statements. I understand you can have one statement inside of an if block but is it possible to have multiple statements in an if block?

Not: if (condition) then (this) else (that);

but: if (condition) then { (this); (that);};

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Programmer MJM
  • 87
  • 1
  • 10
  • 1
    Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders May 11 '15 at 05:18
  • 2
    Thank you for the edit John Saunders – Programmer MJM May 11 '15 at 20:06

2 Answers2

3

thinkScript essentially has three forms of if usage. All three forms require an else branch as well. One form allows for setting or plotting one or more values. The other two only allow one value to be set or plotted.

  1. if statement: can set one or more values, for plot or def variables, within the brackets.
def val1;
plot val2;
if (cond) {
  val1 = <value>;
  val2 = <value>;
} else {
  # commonly used options:

  # sets the variable to be Not a Number
  val1 = Double.NaN;

  # sets the variable to what it was in the previous bar
  # commonly used for recursive counting or retaining a past value across bars
  val2 = val2[1];
}
  1. if expression: all-in-one expression for setting a value. Can only set one value based on a condition, but can be used within other statements. This version is used commonly for recursively counting items across bars and for displaying different colors, say, based on a condition.
def val1 = if <condition> then <value if true> else <value if false>;
  1. if function: similar to the above, but more compact, this is thinkScript(r)'s version of a ternary conditional statement. It's difference is that the true and false values must be double values. Therefore, it can't be used in setting colors, say, or other items that don't represent double values.
def var1 = if(<condition>, <value if true>, <value if false>);

The following example, modified from the thinkScript API doc for the if function, demonstrates using all three versions. I've added a 2nd variable to demonstrate how the if statement can set multiple values at once based on the same condition:

# using version 3, "if function"
plot Maximum1 = if(close > open, close, open);

# using version 2, "if expression"
plot Maximum2 = if close > open then close else open;

# using version 1, "if statement", with two variables, a `plot` and a `def`
plot Maximum3;
def MinimumThing;
if close > open {
    Maximum3 = close;
    MimimumThing = open;
} else {
    Maximum3 = open;
    MinimumThing = close;
}

As a side note, though the example doesn't show it, one can use the def keyword as well as the plot keyword for defining variables' values with these statements.

leanne
  • 7,940
  • 48
  • 77
  • Thank you for the clarification on the topic! I am marking this as the answer. Think script had my mind jumbled in knots but taught me how to think outside the box when trying new languages that have principles and setups that I have never seen before. The fault falls on me for not looking up the API and attempting to work off of prior experience (programming languages such as C#, C++, Java at the time of writing the question) I already knew, to compare and contrast the possible language differences that would help understand the uniqueness ThinkScript. – Programmer MJM Feb 09 '22 at 15:42
0

It is possible. Just mistaken plot variables for conditional statements since they, in turn, use conditional statements to draw out the graphics.

patrick
  • 111
  • 6
Programmer MJM
  • 87
  • 1
  • 10