Expressions calculate values that can then be used by other parts of your program. You can use expressions in the following places of the execution section of your program:

▫       Value Assignment: You can use numeric or Boolean expressions to calculate a value. This value is then stored in a variable.

▫       Function Call: You can use numeric or Boolean expressions to calculate an input value for a function.

▫       Conditions: In the If part of the statement, use a Boolean expression to decide whether the following statements will be executed.

▫       Loops: Use Boolean expressions for the starting and breaking conditions of While and Repeat loops.

There are two kinds of expressions, based on the type of results that they calculate:

▫       Numeric expressions have a number as their result

▫       Boolean expressions have a logical value as their result (true or false)

Expressions can be rather simple, like assigning a number to a variable, or quite complex. Complex expressions make use of other expressions. For example, many Boolean expressions make use of simple numeric expressions.

Additionally, you can also use conditional expressions. Those can return a numeric or Boolean value, depending on the values that you use inside the expression. Conditional expressions are similar to If statements, but in a condensed form.

Numeric expressions

A numeric expression returns a number as a result. As an input, it takes numbers, numeric parameters, numeric variables and function calls that return a number. To calculate a result, a numeric expression can contain the following operators:

+              Addition

-              Subtraction

*              Multiplication

/              Division (you can also use Div instead)

^              Power

%            Modulo (you can also use Mod instead)

Note             Please note that for rational numbers, the syntax of the TeleTrader Language requires the use of a decimal point in the source code (for example 0.5), while on the Parameter tab of the Insert Study dialog you might be expected to use a decimal comma (for example 0,5) – depending on the configuration of your operating system.

The simplest form of a numeric expression is the use of a fixed value, that means that no calculation takes place. For example, the following line assigns a simple numeric expression (a number) to the numeric variable NumOfMonths:

NumOfMonths = 12

In more complex expressions, you can use numbers (constants) or variables and parameters that represent numbers to calculate a result:

adiffHtCy - 0.5 * adiffLtCy + 0.25 * adiffCy

The values you use for a numeric calculation can also be calculated by another program:

funcMove(source, period) * (1 + shift/100)

Numeric expressions are also often used inside of Boolean expressions (see below).

Boolean expressions

A Boolean expression returns the logical value True or False as a result. As an input, it takes variables, parameters, function calls or other expressions (numeric or Boolean).

A simple Boolean expression is a comparison of two values or numeric expressions with the following relational operators:

=              True when first value equals second value

<      True when first value is smaller than second value (you can also use Less instead)

<=           True when first value is smaller than or equal to second value

>              True when first value is greater than second value (you can also use Greater instead)

>=           True when first value is greater than or equal to second value

<>           True when first value is different than second value

You often compare the value of a variable or parameter with a fixed value – these kinds of expressions will return different results based on the current value of the variable and are often used as conditions for If statements or loops:

limitMove Greater 0

You can also use a numeric expression to calculate the value that you want to compare:

cNum[1] < period + 1

You can build even more complex Boolean expressions with logical operators. You typically have two or more conditions or Boolean variables that can each have a value of True or False. You then use the following logical operators to get a result:

And         True when both conditions are true

Or           True when one of the conditions or both of them are true

Xor         True when either one of the conditions is true, but not both of them

Not         Reverses the condition – if the condition was true, the result will be false and vice versa

You normally use this kind of expression for If conditions, where you want to make sure that several conditions are met at once before you proceed:

largest >= 0 And limitMove <> 0 And IsValid(source)

If you want to make sure that one of the condition is not met, you use the Not operator:

IsValid(source) And Not smallest <= 0

Another possibility to build a Boolean expression is the check for crossovers. You typically compare two numeric fields and check if they cross on a certain bar or not. The following syntax is available:

Crosses                True when there is an intersection between the two values on this bar

Crosses Over    True when there is an intersection, and the first value is now higher than the second value

                                (you can also use Crosses Above instead)

Crosses Under  True when there is an intersection, and the first value is now lower than the second value

                                (you can also use Crosses Below instead)

You can also use this kind of expression when one of the sides is a constant value:

If a Crosses Over 0 Then ...

Note             At least one of the sides of a Crosses expression has to be of the type Numeric Field, or must be a Numeric variable with history. History for a variable is only created when it is needed in the program. To force history for a Numeric variable, refer to it once with var_name[0]when you first assign a value. See also Accessing the History of Variables and Results.

Conditional Expressions

A conditional expression returns a numeric or Boolean value as a result, depending on the values that you use inside the expression. Depending on the condition you use, one or the other value will be returned. Conditional expressions are rather complex, as you have to use a Boolean expression inside of it to formulate your condition:

Iff Condition Then Value1 Else Value2

The Condition is a Boolean expression that decides if Value1 or Value2 is returned by the expression. Value1 and Value2 can be either numeric or Boolean values, but they both have to be of the same type. Like any other expression, you can use the conditional expression for value assignment:

a = Iff b < c Then 1 Else 2;

The above line is identical to using the following If statement (for details about the syntax, see If Conditions):

If b < c Then
   a = 1
Else
   a = 2;

You can see that the Iff expression helps you save some code lines. The main difference between If and Iff is that the first one is a command statement (executes code), while the second one is an expression (just returns a value). Therefore, you can for example also use Iff expressions inside function calls, like any other expression:

Sum(Iff a < b Then Source.Close Else Source.Open, period)

To get the same results as above using an If statement, you would have to introduce an additional variable and add several lines of code.