In chapter 6, we discussed how to use some past data in technical indicators, that is when you need a previous value of a variable or function when calculating those for the current bar. For example,
close from 2 bars ago returns the Close price of the second last bar,
close from 1 bar ago returns the Close price of the bar prior to the current, etc.
What we did not discuss is how to move back to the future, i.e., use values of following bars in calculations which is also possible. Negative numbers are here to help: these refer to the future data. For example,
close from -1 bar ago returns the Close price one bar forward,
low from -2 bars ago returns the Low price two bars forward, etc. This might look awkward but it will surely work.
Using human-readable syntax for referring to historical data only deems possible when some simple math is employed, e.g.,
plot scan = close from 4 bars ago + high from 1 bar ago;
This script will plot the sum of the Close price 4 bars ago and the High price 1 bar ago. But what should we do if we need lots of past and future data to perform numerous mathematical operations on? There was actually a hint for that in chapter 1 when we declared recursive variables. The following script was discussed there:
def vol = vol + volume;
This script calculates cumulative volume starting from the first bar on chart. The square brackets next to
vol contain an index which tells thinkScript® how many bars before the current was the one whose value we need. Just like with the human-readable syntax, positive indexes are used to refer to data in the past and negative indexes refer to data in the future. For example, close returns the Close price of the previous bar, close returns the second last, etc. At the same time, close[-1] returns the Close of the next bar (of course, if there are any), close[-2] returns the Close price 2 bars forward, etc.
plot momentum = close - close;
The example will plot the difference between the current Close price and the Close price 5 bars ago.
Historical data is ubiquitously used in built-in studies and strategies. This is especially important when you need recursive variables as they need their previous values to be declared. A good example of recursively declared variables is Fibonacci sequence: each number in it is equal to the sum of previous to with first numbers being 1 and 1. Here is a script for that:
def x = CompoundValue(2, x + x, 1);
plot FibonacciNumbers = x;
Here we used the
CompoundValue function which has a very interesting calculation mechanism. First of all, let’s see what its arguments are:
CompoundValue(length, visible data, historical data);
So, being used to initialize studies with recursion, this function calculates a compound value according to following rule: if number of a bar is greater than
length then the
visible data value is returned, otherwise it returns the
historical data value. In our Fibonacci sequence script, it assigned value of one to the first two bars (using 2 as the value of parameter
length), and, starting from the third bar, each following number is calculated as the sum of the previous two. As a result, we get a plot with values of 1, 1, 2, 3, 5, 8, and so on.
Now that you’ve learned to move backward and forward all across the chart, let’s move on to discussing aggregation periods which actually form the bars on time charts.