# Chapter 1. Defining Variables

In thinkScript®, variables are declared using reserved word `def`. Syntax of the declaration is quite simple: first, you type `def`, which will state that you are going to declare a variable, then you specify the name of the variable so that thinkScript® recognizes it, add the “equals” sign (=) to determine how the variable is going to be calculated, proceed with expression showing how to calculate it, and don’t forget to terminate the line with semicolon to let thinkScript® know that the calculation is over.

Here is an example of the most basic script:

`def price = close; `

This script will retrieve the Close price for each bar.

Was it too simple for you? Let’s make it a little bit more complex:

`def price = (high + low)/2; `

Now this script will calculate the midpoint of each bar. Note, however, that we already have a built-in function for calculation of this value, it is called `hl2`. Thus, the following script will calculate the same value:

`def price = hl2; `

A more interesting thing is that you can use logical expressions to declare a variable so that the latter has Boolean values. It is useful when you like to check if some condition is true for some bar:

`def condition = close > 700; `

In this script, variable `condition` will have value of 1 for those bars having Close price greater than 700 and 0 for all others.

Note that Boolean values are translated into numerical. In other words, you can perform arithmetical operations on them. Consider the following script:

`def condition1 = close > 700;def condition2 = close > 900;def condition3 = close > 1100;def res = condition1 + condition2 + condition3; `

In this script, variable `res` will have value of 3 for those bars having Close price higher than 1100, 2 for bars with Close price in between 900 and 1100, 1 for those closing in between 700 and 900, and 0 for the rest. This script is also a good example of using multiple variables: once you have declared a variable, you are free to use it further on.

Variables can be declared recursively, so that they use their own values in further calculation. Let’s start with a basic example:

`def vol = vol + volume; `

This script calculates cumulative volume starting from the first bar on chart. The logic behind it is quite simple: before the calculation, thinkScript® assigns 0 to the variable (it actually always does so), thus, its value at the first bar will be equal to volume. Then, for the second bar, it takes previous value and adds current volume to it; accumulation of volume proceeds until the last bar on chart. You must have noticed square brackets next to `vol` in the script. These brackets have thinkScript® take a certain value from the previous bar; we will study this notation more thoroughly in chapter 10, however, keep in mind that these square brackets indicate historical reference.

Once you declare all the variables, you might want to display results of calculation on chart. This is done by using the reserved word `plot`. This word commands thinkScript® to visualize values of a variable using numerous graphical instruments. In this chapter we will focus on the default one which happens to be line. This painting strategy connects points derived from calculation in the manner of a broken line; here is an example script:

`def a = close + open;def b = high + low; plot c = a/b;`

This script will plot the ratio of Close+Open to High+Low. Note that reserved word `plot` replaces `def`, and we could actually plot the same value using a single line of script:

`plot c = (close + open)/(high + low);`

However, this is not the case when you need some serious calculations using complicated mathematics.

Note that the value will be plotted on upper (price) subgraph, however, you are free to choose another subgraph on which the plot will be displayed; use reserved word `declare` for this purpose. This should be the first line in the script, study the example:

`declare lower;def a = close + open;def b = high + low;plot c = a/b;`

The previously discussed value will be plotted on lower subgraph. In order to plot values on volume subgraph, use command `declare on_volume` in the same way. For information on other ways of placing plots on chart, see the Declarations page of thinkScript® reference.

Now that you are keen on thinkScript® variables, let’s move on to arithmetical operations you might want to perform on them; these operations will be discussed in the next chapter.