Chapter 5. Conditional Expressions

So far, we have discussed functions which process price data and yield some output to be considered by chartists. However, few will argue that technical analysis is all about analyzing if data provided by studies or the price data itself conform to certain conditions. For example, there is a huge block of technical indicators called Japanese Candlestick Patterns which is entirely built on conditions whether certain candles have certain shape, color and position. There are also less exotic examples of employing conditions in analysis: Average Directional Movement Index (ADX), for instance, is believed to indicate strong trend upon a condition that its value is higher than 100.

Just like many other programming languages, thinkScript® offers users the standard if-then-else scheme, however, there are several ways to employ it in script. First of all, let’s discuss usage of if-expressions and if-statements in thinkScript®. Consider the following script:

input price = close;
input long_average = yes;
plot SimpleAvg = Average(price, if long_average then 26 else 12);

This script uses an if-expression to calculate SMA of Close price: if long_average input is set to “yes”, its period will be equal to 26; if it is set to "no", 12 period SMA will be calculated. If-expression is a simple way to assign one of the two possible values to an input parameter, but what should we do if we need to change the function itself? For example, we might want to use an EMA instead of SMA if the input is set to “no”. Here comes the if-statement which has a different syntax but keeps quite the same meaning:

input price = close;
input long_average = yes;
plot Avg;
if long_average {
Avg = Average(price, 26);
} else {
Avg = ExpAverage(price, 12);
}

Depending on whether or not long_average input is set to “yes”, this script will plot either 26 period SMA or 12 period EMA of Close price. Note how this script declares the plot and the math behind it: command plot precedes the if-statement. Using the if-statement is helpful when you need some serious mathematic calculation and keeping it in a single line of code could be very confusing. When you however need a simple switch like in the example above, the if-expression will do:

input price = close;
input long_average = yes;
plot Avg = if long_average then Average(close, 26) else ExpAverage(close, 12);

In chapter 1, we discussed assigning Boolean values to a variable with this example:

def condition = close>open;

Once this variable outputs Boolean values 1 or 0 (“yes” or “no”), we could use this variable as a condition for the if-expression:

def condition = close>open;
plot Maximum = if condition then close else open;

This script will plot a line connecting the higher of Close and Open for each bar. This can be also achieved using the third type of conditional expressions: if-function. The if-function is, perhaps, the simplest way of defining a condition when you just need to specify numerical true and false values. The syntax of this function is trivial: you just specify the condition, true value and false value as input parameters; true value will be returned if the condition is fulfilled and false value will be returned otherwise. See how the Maximum plot from the example above can be constructed using the if-function:

plot Maximum = If (close>open, close, open);

As it was stated before, only numerical data can be specified as true and false values. If you, however, need a certain constant (e.g., Color constants) or a string value, the if-expression should be used.

Let’s see all the three ways to specify conditions in a single script:

plot Maximum1 = If(close > open, close, open);
plot Maximum2 = if close > open then close else open;
plot Maximum3;
if close > open {
Maximum3 = close;
} else {
Maximum3 = open;
}

Here, plot Maximum1 is defined by if-function, Maximum2 by if-expression, and Maximum3 by if-statement, all the three plotting the higher of Open and Close prices. Use the function for simple conditions with numerical true and false values, the expression for numerical or non-numerical ones, and if-statement for beautification of complex formulas you might need when creating indicators.

In this chapter we only discussed simple comparison operators yielding Boolean values to form conditional expressions. However in thinkScript®, there are several more interesting logical operators which can be set using human-readable syntax. This kind of syntax will be explained in the next chapter.