The syntax of the Formula, Boolean expression and String expression blocks.
Last updated
Last updated
An expression is a block designed to perform arithmetic operations on input data.
The window mode of the expression editor supports the IntelliSense technology. When typing a block name, a list of blocks suitable for the typed text appears.
Switching to the hint list is possible either by pressing the Down key or by clicking the left mouse button on the name. Navigation through the list is carried out either with the Up and Down keys, or with the mouse.
The selection of a block from the list is carried out either by pressing Enter or by double-clicking the mouse. After selecting a block from the list, the expression is supplemented with the selected block, and a connection is established with the selected block, if it has not been established yet.
In the expression editor, you can use the SHIFT + ENTER keys to move to the next line.
Formula is used to evaluate the values of expressions. The output is always a numeric value. In practice, it is usually used for intermediate calculations, as well as for calculating entry / exit levels from a position.
In the "Formula" block, the following operations can be used:
Name | Name | Example |
Addition | + | Open + Close |
Subtraction | – | EMA – EMA1 |
Multiplication | * | Close * Constant |
Division | / | Close / Close[i-1] |
A feature of the "Formula" block is the ability to use the IF statements. The result of the execution will still be a number. The If statement in the "Formula" block looks like this:
As a result of checking the IF Statement, if it is true, the output of the "Formula" block will be "Expression, _if_true", otherwise - "Expression, _if_false."
Usage example:
Boolean expression is an expression used in programming languages that produces a Boolean value when evaluated. A Boolean value is either true or false.
The output of a Boolean expression blocks is always 0 (i.e. "false") or 1 (i.e. "true"). In practice, it is usually used to set the condition for entering / exiting a position. Let's consider the syntax that can be used in the “Boolean expression” block. The following comparison operations can be used in the “Boolean expression” block (an example is given for each operation):
Name | Operator | Example |
Greater than | > | Close>Open |
Less than | < | Open[i]<Open[i-1] |
Greater than or equal to | >= | Close>=Open |
Less than or equal to | <= | Close<=EMA |
Equal to | == | Close==High |
Equal to true (Is true) | ==true | Boolean expression==true (Brief version: Boolean expression) |
Equal to false (Is false) | ==false | Boolean expression1==false (Brief version: !Boolean expression1) |
Not equal to | != | Close!=High |
Not equal to true (Not true) | !=true | Boolean expression!=true (Brief version: !Boolean expression) |
Not equal to false (Not false) | !=false | Boolean expression1!=false (Brief version: Boolean expression1) |
In addition to comparison operations, a number of conditions can be checked simultaneously in the "Boolean Expression" block. These conditions should be combined by the following operators:
Name | Operator | Example |
Boolean «AND» | && | Close>Open && Open[i]<Open[i-1] |
Boolean «OR» | || | EMA==EMA1 || Close<=EMA |
When using "And", the output of the "Boolean Expression" block will be true only if all the conditions written in the block are true. When using "OR", the output of the block will be true if at least one of the conditions written in the block is true.
Note: In the " Boolean Expression" block it is possible to use mathematical functions: Addition, Subtraction, Multiplication, Division.
A “String expression” implies the use of any conditions within the C# syntax to set the output message. The output is displayed on the control panel.
A “String expression” is similar to a Boolean expression, but the output is not Boolean, but String.
Example:
Output the expression to the Control panel. Thus, when the bar is rising, the Control panel will display the inscription Rising.
To check the output of a Formula for NaN, in the Formula block, you can write:
For example, in the Formula2 block, you can check the output of the previous formula and assign a value:
If the output of Formula1 is NaN, then Formula2 returns 0.
1️⃣ Referring to the previous values of a certain sequence (price, indicator, etc.).
In blocks, it is possible to refer to the previous values of prices or indicators. The current value is always indicated by the index i. In order to refer, for example, to the previous value of the Closing Price, you should write:
When using references to the previous elements in calculations or tests, you should not forget about the "Begin with" parameter. The easiest way to remember which value to fill in the "Begin with" field is to look at the maximum number used in the square brackets and subtracted from the i index.
Example. The Boolean expression contains the expression:
Looking at this entry, we see that the maximum value of the number in square brackets is 9. Therefore, we put 9 in the "Begin with" field.
Why is it necessary to change the value in the "Begin with" field when accessing the previous elements? The sequence of values has a beginning or a boundary. There is nothing beyond the beginning. All calculations are based on the current value of i, so when we start counting the value for i=0, i.e. for the first value of the sequence, and try to refer, for example, to the previous value, the program will say that it is empty, and it will not be able to calculate. However, when the entry is basic, for example [i-9], it is not necessary to set the "Begin with" – the program will detect it automatically.
When using the entry EMA[i-Constant] or EMA[i-Formula], the parameter "Begin with" must be set. This parameter must be obviously greater than a Constant or Formula, but not greater than the number of bars loaded into the script (or Agent). Put this number in the script properties in the "Trade from (bar)" setting. The “Begin with" parameter shows which bar to start the calculation from.
Axiom: • The entries Close[i] and Close are equivalent
In the Formula block, you can refer to your own value
Execution returns the sum of the Close value and the previous value of the Formula.
2️⃣ Using Math library functions.
In the expressions written in the "Formula" and "Boolean expression" blocks, it is possible to use functions from the Math library. A list of functions and a description is given on the website:
The need for these functions appears when the standard mathematical operations discussed above are not enough. Using functions from the Math library allows to:
Calculate the absolute value
Raise the number to a power
Calculate the sine, cosine, etc.
Round the value
Calculate the square root of the value
Etc.
How to use these functions in blocks?
To calculate the value using the function, you must write:
Example of calculating the absolute value for the expression (EMA-EMA1):
The use of other functions is similar, the difference can only occur in the number of function parameters that are specified in brackets.
Is there a rounding up/down function in TSLab?
In the Formula blocks menu, the option to select the Math method is added. Hover the cursor over the Math button. Switching to the hint-list is carried out by clicking the left mouse button on the name Math. Navigation through the list of Math methods is carried out either with the Up and Down keys, or with the mouse. The selection of a block from the list is carried out either by pressing Enter or by clicking the left mouse button.
When dividing an Integer by an integer, we get an integer. Therefore, if you write 13/3 in the Formula, the Formula will display 4 on the graph.
Fix: Add .0 to the numerator or to the denominator (i.e. write 13 / 3.0, not 13/3) to get decimal instead of integer.
Getting the value of the first bar of the loaded history:
The Formula will return the Close price of the first candle of the loaded history.