When you write a program, you tell it how to calculate a value for one bar, and TeleTrader WorkStation then performs this calculation for each bar of the time series that is visible on the chart. For almost all indicators or signals, these calculations are based on one or more of the values of a time series. For example, you could write a study that takes into account the Open and Close prices of a security.
To access a time series, you have to first define it. In the TeleTrader Language, you do this with a special kind of input parameter that is called a "base" in the Insert Study dialog. This parameter is normally called source (or similar) and is of the type Field or Security. You can have more than one base: The study Crush Spread, for example, needs three bases.
The available length of the time series is determined by the time region that you have defined for the chart where the study is inserted. For example, if you insert a study in a chart that show only one day of Intraday data, you only have access to the time series of this day. For studies that are inserted in price pages, the length of the time series can be defined in the properties of the inserted study. Per default, an appropriate setting is chosen automatically for each study (often depending on the currently set study parameters such as the period of a Moving Average). If you do not get the appropriate results for your study in the price page, you can change this setting by selecting Use custom time region on the Parameters tab of the study and entering the appropriate amount of bars.
Once you have defined your base, you can then use it in your program:
▫ For bases of the type Field, use the name of the time series, for example source. The program will use the field value that is defined in the Insert Study dialog, for example the Close price.
▫ For bases of the type Security, use the name of the time series plus the name of the field (element) that you want to use, for example source.Close or source.Open. If you use just source, the Close price will be used automatically.
For calculations including a time series or base, you will normally use the value of the current bar (the bar that the program is executed for at the moment), but you can also use the values of past bars and future bars.
For simple programs, most of the times you only need to know the value of the current bar to calculate your result. When you refer to the current bar, you can do this by just using the name of your parameter, like source, or by writing source, which means the same. For parameters of the type Security, you can for example write source.Close.
When you start to write more complex programs, you can make use of the fact that the whole time series is available to you. This means that you can use the values of the bars near your current bar and take them into account for your calculations.
As was already mentioned, the current bar (for example, today’s bar) is represented by source. If you want to use a value from yesterday’s bar (that is, the next bar to the left), you can refer to this bar with source. For bars even further in the past, use source, source etc. For parameters of the type Security, you can for example write source.Open.
Keep in mind that for the very first bars of your time series, no past values might be available.
You can also use values from bars that are to the right of your current bar with source[-1], source[-2] and so on. As these bars are "in the future", there is a natural limit for using these kinds of calculations – when the study reaches the latest bar of the time series, no values for future bars are available yet, and you might get invalid results for this bar (which means that the study is not drawn for these bars).
The function Sum uses a number of past elements of the time series source and sums them up. This example uses a For loop and accesses each element one by one with source[i], where the variable i is a changing value from zero to the value of period. As source equals the current bar, and source, source … up to source[period] equal past bars, the end result is the sum of all the last period bars (for example, the last 5 bars when period is set to 5).
Numeric Field source;
Result = 0;
For i=0 To period
Result = Result + source[i];
Consider what happens when for example the result for the fifth bar is calculated. The program first takes the current bar sourceand then repeats the For loop. The value of i thus changes to 1, and the program gets the value for yesterday’s bar source and adds it to the result. This is repeated until all bars are summed up.