The TeleTrader Language is a very simple but powerful language. It uses very few elements compared to other programming languages. We will explore the most important elements with a small example program written in the TeleTrader Language to calculate and plot the Moving Average Simple.
Numeric Field source;
s = Sum(source, period);
Result = s / period;
You can see that different words in the code have different colors and styles: This is a way to help you differentiate between different parts of the TeleTrader Language. These colors and styles are also used in TeleTrader WorkStation – for an overview, see Syntax Coloring.
When you write a program, it must have a name. This is important because you later want to tell the computer which program he should run, and you do this by using the name of the program. The name (or function title) of this program is defined in its very first line:
You can see that the name of the program, movs, is an abbreviation of the name of the study, Moving Average Simple. You cannot use its real name, because the naming rules of the TeleTrader Language do not allow blanks in a function name. You could, however, use the name moving_average_simple, if you wanted, because underscores are allowed.
The next six lines are the so-called declaration section of the program. Here, you define several names and properties that you want to use in your program. This is also due to a rule of the TeleTrader Language (and of most other programming languages): Before you use something in your program, you first have to tell the computer its name. Computers, as was already explained, do not handle ambiguity very well: So you first tell them the names for everything that you want to use, and later have to stick to these names exactly as you defined them here, so that the computer knows what you are talking about.
In the declaration section, you first tell the computer that your program will calculate a number:
Then, you describe that you will use two parameters (input values) in the program, called source and period. The parameter source will be a field of a security, such as its Open or Close value. The parameter period will be a simple number.
Numeric Field source;
Parameters are input values for your program. You probably already know them from the Parameters tab of the Insert Study dialog. Here, you tell the computer what parameters should be visible there.
The last part of the declaration section is optional: You can define variables here. Variables are similar to parameters; you only will never see them in the Insert Study dialog. This is because they are only used internally inside the program, most times to temporarily store a value that you have calculated. In our example, we tell the computer that we will only need one variable s, which will be used somewhere in the program to store a number.
Remember the names of the parameters and the variable that were shown here – they will soon be used in the section of the program were all the real work is done: The execution section.
Now the computer knows the names and types of all the things you want to use inside the program. Time to start the real work, which means calculating the moving average and then showing it on the screen. You can see that the start of the execution section is marked with a special word:
The next two lines of this section are mathematical calculations that calculate a simple moving average. To understand them, you only have to know how you would calculate the moving average for a security if you had to do it yourself, for example a 21-period moving average: Sum up the last 21 values of the security, and divide them by 21.
In the example program, we do this in two steps. First, sum up all values for a specific period, and store them in the variable s. To make the addition easier, we use a little helper program called Sum that calculates this for us. We tell this function which values it should sum up (the values from our source security), and how many values it should sum up (we use the parameter period for this). There are several other built-in helper programs in the TeleTrader Language, and you can use them in your own programs anytime you like.
s = Sum(source, period);
You can now see the need for the parameters that we defined above: The parameter source will tell the program from which security it should take the values, and the parameter period will tell the program for which period it should calculate the moving average. This way, you will only have to write one program, and can then use it with any security you like, and for any period you like. If you wanted to calculate a 100-period moving average, you would simply change the parameter period in the Insert Study dialog.
Now that we know the sum of the values, we only have to divide them by their number, and then already have the final result:
Result = s / period;
You now have the final result for a simple moving average. However, this result is only a number – remember that in the declaration section, we defined that the program would calculate a number as a result. Of course, we also want to show this result on the screen, so we add another line to the program:
Drawing on the screen is easy with the TeleTrader Language – again, a little helper function does all the work for you. You only have to tell it what it should draw: In this case, it is a line called "MovS" that should use the values from your end result.
All the work is done now. We can tell the computer that the program is over:
You now should know that a program written in the TeleTrader Language is composed of three parts:
▫ Function title, where you define a name for your program
▫ Declaration section, where you tell the computer the names of all parameters and variables you want to use, and define what type the end result of your program will be
▫ Execution section, where all the calculations and drawing are actually done, and on which you will probably spend the most time and thoughts
All programs written in the TeleTrader Language must follow this basic structure.
You also have learned some of the rules of the TeleTrader Language:
▫ Everything you use must have a name, and this name must obey certain rules, such as no blanks
▫ You often use mathematical calculations to produce an end result
▫ You can store intermediate results in a variable
▫ Once you have the end result, you store it in the special variable Result
▫ You can use helper programs for calculating results, or for drawing the results on the screen
In the next sections, you will learn some more of the rules of the TeleTrader Language, and see some step-by-step examples of writing a program.