While discussing variables, we already used the simplest mathematical operations: addition, subtraction, multiplication, and division. These operations do not require any special notation and can be performed using basic characters: plus (+), minus (-), asterisk (*), and slash(/), respectively. Also, declaration of Boolean variables often implies using comparison operators: greater than (>), less than (<), greater than or equals to (>=), less than or equals to (<=), etc. There are also equality operators falling into the same group. To test if two values are equal, use the double equals sign (==). For the inequality, use either '!=' or '<>' signs.
Here is an example script for using equality/inequality operators:
def a = close==open;
def b = close!=open;
def c = close<>open;
Variables b and c are identical: they will both have values of 0 for bars closing at the Open price (sometimes called Doji candles) and 1 for the rest; values of variable a will be vice versa.
For more complex calculations you might need some of thinkScript® mathematical/trigonometric functions. Let's start with power-related ones. These functions allow you to raise a value to a power, or find an exponential value or a logarithm. Study the following script:
def val = close/open;
def data1 = Sqr(val);
def data2 = Sqrt(val);
def data3 = Power(val, 3);
def data4 = Exp(val);
def data5 = Lg(val);
def data6 = Log(val);
This script calculates six data values based on the initial value of the Close to Open ratio for each bar.
Data1 calculates the square of this value with
Data2 returns the square root using
Power function to raise the value to the power of 3.
Data4 calls the
Exp to raise Euler's number to the power of the value.
Data5 returns the common logarithm of the value and
data6, the natural logarithm; functions
Log are used for these purposes, respectively.
Let's study the syntax of calling functions in thinkScript®: first, you type the name of the function, then you specify its arguments in parentheses and terminate the line with semicolon. If a function requires several arguments, they need to be separated with a comma – see how it works with
data3 variable in the example above. Function
Power requires two arguments: value to be raised (in our case, it is variable
val) and the power to raise this value to (we used 3 in the example).
When working with power-related functions, you might want to use the Euler's number. There is a built-in constant in thinkScript® for this purpose:
Double in its name has nothing to do with multiplying by 2: it is just a common thinkScript® designation of floating-number data type. See how to use this constant in the following script:
def val = close/open;
def data1 = Exp(val);
def data2 = Power(Double.E, val);
data2 will always return identical values as they both raise Euler's number (
Double.E) to the power of
Next set of functions we are going to discuss is trigonometric. It consists of six functions:
Cos, Sin, Tan, ACos, ASin, and
ATan. All of these use a single argument for which the value will be returned. Note that functions
Cos, Sin, and
Tan use arguments expressed in radians so that
Double.Pi constant might be useful. Functions
ACos, ASin, and
ATan return values in radians; for details, see the corresponding articles in thinkScript® reference.
thinkScript® also provides you with a number of functions which round values if for some reason fractions or irrational numbers are not desired. These functions are:
Ceil, Floor, Round, RoundDown, and
RoundUp. Here is an example:
def data1 = Ceil(Double.E);
def data2 = Floor(Double.E);
def data3 = RoundUp(Double.E, 3);
def data4 = RoundDown(Double.E, 3);
def data5 = Round(Double.E, 3);
In this script, Euler's number is rounded using five different algorithms, thus the results will be different.
Data1 uses the
Ceil function rounding the number to the closest integer which is not less than the number itself; the result is 3.
Data2 uses the
Floor function which also rounds the number to the integer, however, this integer must not be greater than the number rounded, so the result is 2. Functions
RoundDown, RoundUp, and
Round reduce the length of the value to the specified number of digits after the decimal point (for this purpose we specified the second argument in parentheses, 3). While
RoundUp function rounds the number to the greater one,
RoundDown does towards zero, and
Round to whichever is closer. So, in the example above,
data3 will be equal to 2.719 while both
data5 will be equal to 2.718. However, if you round the values to four decimal places (using (Double.E, 4) as the arguments),
data5 will be both equal 2.7183 and
data4 will be equal to 2.7182.
The full list of available functions with detailed descriptions can be found in the Mathematical & Trigonometric section of thinkScript® reference, so feel free to research those in order to apply some complex math to your scripts if needed. Now we are going to move on to next chapter where we will discuss inputs which will make your script super flexible and adjustable via the Edit Studies and Strategies dialog window.