You already know that TeleTrader WorkStation executes your program many times, once for every bar. It starts with calculating a result for the oldest bar of your time series (that means, the leftmost bar on the chart) and then repeats the program for every bar that is visible, until it reaches the latest bar.
In your program, you can use variables to store some data that you have calculated (you can find more on variables in the section Variables). When your program is executed for the first bar, TeleTrader WorkStation calculates a value for this variable. Then, the program is repeated for the next bar, and again a new value for your variable is calculated. TeleTrader WorkStation of course also does this for the special variable Result, that is included in every program and stores the end result of your study for the current bar.
You can use this concept to your advantage when writing a program: In some cases, you might want to take the result of a previous calculation and use it in your program. After all, these values have already been calculated by the computer, and it would be a waste of system resources to calculate them anew.
To access the last value of any variable, or of the special variable Result, you refer to it with VariableName or Result. To access values that are even further in the past, use VariableName or Result and so on.
Note For all variables and results, you can only access values that are in the past, because future values are not calculated yet.
When you use past results, you have to take into account what will happen when the program is executed for the first time and there are no past results yet that you can use. For these cases, the built-in function CurrentBar is available. It returns the index number of the bar that the program is executed for at the moment, and you can use it to find out how many bars (and results) have been calculated already.
Note For performance reasons, history for a variable is only created when it is accessed at some point in the program. For example, a variable myVar that is never used with square brackets behind it will have no stored history. If you need to force history for this variable, refer to it with myVar when you first assign a value to it ( indicates the current value of the variable). History will then be stored for myVar. This is only recommended in special cases, such as when you need to use myVar in a Crosses expression (see Expressions).
The function Sum was already introduced to illustrate how to access past elements of a time series and sum them up. Here, it is rewritten to illustrate how to use past results for the same calculations. It first uses an If condition statement to find out whether the program is executed for the first time: In this case, that is for the first bar, it does not rely on any past results. In the other cases, it uses the last calculated result Result and adds it to the current result.
Numeric Field source;
If CurrentBar = 1 Then
Result = source
Else If CurrentBar < period Then
Result = Result + source
Result = Result + source – source[period];
When for example the program is executed for the fifth bar (assuming that the parameter period is set to some value greater than 5), the result for this bar is calculated by taking the result for the last bar and adding the Close price of the current bar.