We will now take a deeper look at some of the basic rules of the TeleTrader Language. For the basic structure of a TeleTrader Language program, see the previous section Introduction to the TeleTrader Language.

Obey the naming conventions

When you think of a name for your program, its parameters and variables, you have to follow the naming rules:

▫       The name can only use letters, numbers or the underscore sign ( _ )

▫       The name must start with a letter

▫       You are not allowed to use one of the reserved words as a name (see Naming Conventions)

The good news is that the TeleTrader Language is not case-sensitive. That means that you can define and use your names in uppercase, lowercase or mixed case, and the computer won’t even notice. Therefore, you can define a parameter with the name Source, and then use it in your program as source, SOURCE, SOurCe, or any other way you like.

Obey the punctuation rules

There are also some rules that you must follow regarding punctuation:

▫       A program must end with a full stop ( . )

▫       Each command statement must end with a semicolon ( ; )

Note             You must not confuse a statement with a line of your program, although it often is the same. A statement is one complete command, for example one calculation or one drawing operation. Do not blindly add a semicolon to the end of every line, because some statements stretch over more than one line. To learn more about the different types of statements and their syntax, see Execution Section.

▫       When you use a helper program, you usually follow it with brackets. Inside the brackets, you can pass input values to the helper program. Separate multiple values with a comma: Sum(source, period)

▫       You can modify your program text for better readability: Add more white space (blanks) between words and lines, or remove all white space if you like. You can also have more than one command in one line, as long as each statement is properly ended with a semicolon.

Declare parameters and variables before you use them

Every time you want to use a variable or a parameter in your program, you have to be sure that it is properly declared in the declaration section (or function header) of your program. If you decide later that you for example need another variable, you have to add it to the variables list and give it a name, or your program won’t work (because the computer does not know what you are referring to).

You also have to pay attention to the type of each variable and parameter. You cannot tell the computer that a variable will store a number, and then use it to store text. Again, your program won’t work if you try this.

Define at least one input parameter

You will most probably use your program to plot something on a chart. (Of course, all calculated results that can be plotted on a chart can also be inserted in a price page column.) What you plot should help you analyze the symbol (security) that is shown in the chart. Therefore, you should define at least one parameter that you can use to tell the program which security is the base for your study.

Calculate results with expressions

You will most probably use some formula to calculate the study that is then drawn on the screen. For calculating a result, you use mathematical expressions. These basically use the same syntax that you are used to from common calculators: For additions, use the + sign, for multiplications, use the * sign, for divisions, use the / sign and so on. For more information about expressions, see Expressions.

Store intermediate results in a variable

When you cannot calculate the end result in one step, use a variable to store the intermediate result. You can use as many variables as you like.

Store the end result in the result variable

The end result is always stored in a special variable called Result. This variable is automatically available in all TeleTrader Language programs – you do not have to declare it! This is the only exception to the rule that you have to declare every variable before you use it. However, you still have to declare the type that your end result will be. You also do this in the declaration section, in the following line:

Returns Numeric;

Be careful – if you tell the computer that your end result will be a number, it won’t accept anything else than a number! Also, you cannot use the variable Result if you have defined that the program will return Nothing.

Use helper programs

There are several helper programs available in the TeleTrader Language. Use them whenever you can – they make writing your study easier and reduce errors! You only have to be aware of two things:

▫       What does the helper program need to perform its task? These are the input values that you pass to the program in round brackets.

▫       What does the helper program return or do? Some programs just calculate a result and give it back to you, others draw something on the screen. You should know what the output of the program will be!

You can find list of all helper programs (also called functions) in the Function Reference.

Do it your way

While there are strict rules of how you must structure your program, and you must follow the language syntax down to the smallest details, there is generally more than one way to write a program.

Compare our example from the previous section:

Function movs
Returns Numeric;

Parameters
   Numeric Field source;
   Numeric period;

Variables
   Numeric s;

Begin
   s = Sum(source, period);
   Result = s / period;
   DrawLine("MovS", Result);
End.

 

… with the following modification of the same program:

Function movs
Returns Numeric;

Parameters
   Numeric Field source;
   Numeric period;

Begin
   Result = Sum(source, period) / period;
   DrawLine("MovS", Result);
End.

The second example does exactly the same thing, it is only written a bit simpler. For example, it does not need a variable to store the intermediate result, because it calculates the end result in one step.

You can develop your own style in writing programs – just do it in the way that seems most intuitive to you!

Experiment!

The best way to learn a programming language is to experiment! We encourage you to play around and make modifications to the existing studies, not only in terms of style, but more importantly, in terms of functionality. If you want to see some examples of possible modifications, just read on.