Program formula language
As I mentioned in a previous post the formula language is array based. Almost all functions work on array and return an array as result. Using the available functions the user can write his own studies/indicators to perform simple or more complex calculations. More complex calculations can be performed by using the conditional statement iif ( expresion to evaluate, case true, case false) which is also know as the ternary operator in old programming languages or -if then else statement- found in most programming languages.
Aside from iif statement to perform more complex calculations a user can assign an array to a variable which can me then reused anywhere in the program. This variable will be an array as well . For example the function sma(close,20) calculates a simple moving average of the close price but if you want to reuse it in other calculation will be more simple to assing a shorter variable to reffer to it. So you would say
vrb1 : sma(close,20)
The above code line will allow you to use vrb1 instead of the whole function of the moving average anywhere you need in the program lines that follow after this line. This assignment is made using : character.
Aside from : character you can use ? character as AND logical operator and | character as OR logical operator. So in program formula language the operators that are unique to this program are these:
: Assigment operator
? Logical AND operator
| Logical OR operator
All other operators are the same as in any programming language:
+ plus
- minus
>greater
< less
=equal
>= greater or equal
<= less or equal
<> not equal
* multiply
/ divide
^ power
Now lets explain a simple formula for the chart image bellow and how it was created.
The formula for this chart is in the next image.
And here is how it was build and how it works:
First two lines of code use the set function which sets a user global variable in the chart window. To create the MACD indicator we need two moving averages of different lengths, these lengths are established here and can be changed by the user later. They are 14 and 26 in this formula.
set(periods1,26);
set(period2,14);
Third and fourth lines of code use the : assignment operator to assign letters a and b to the sma functions so they can be referred in the formula later to perform calculations with them. So letter a is a simple moving average on 26 bars and letter b is now a simple moving average on 14 bars.
a:sma(close,periods1);
b:sma(close,periods2);
Line number five tells the program to assign the word macd as the difference between the two moving averages which are now a and b.
macd:b-a;
Line number 6 (lines are not numerated in formula editor) creates the signal line, for which a simple average of 9 bars is used
sig:sma(macd,9);
Notice above how I reused the variable macd? The sma function is calculating on the variable previously assigned as macd which is the difference result between the two moving averages.
From here next lines of code draw the graphic of the chart. Function PlotOHLC is used to draw the price of the instrument and function plot is used to draw the indicators in various ways, as histogram area, line, dashed line as you can see in the chart image.
Notice in the last line of code I actually created a new calculation: I subtracted sig from macd to obtain the indicator represented as area in the chart image.
Comments
Post a Comment