Help > Equilla Program Structure and Syntax
The so-called meta properties define the global functions of your indicator, for example if the indicator should be displayed in a window of its own or within another chart. You can also add information about how the indicator is calculated (Synopsis) and define a ShortCode. The meta information area can usually be found in the beginning of the source code. You should adhere to this convention. The area has to be introduced with the tag
Meta:
The three most important pieces of meta information are:
Meta:
Synopsis( " Text " ),
Subchart(True/False),
ShortCode("MyIndicator");
When applying an indicator to a chart, one or more parameters are available in the indicator properties. These parameters can be calculation periods, multiplications or lists. User-specific parameters have to be declared in the input area of the source code. The area has to be introduced with the tag
Inputs:
Inputs:
Visuals (true, false),
Period(10, 1, 100),
note that the
FactorOffSet( 1.25, 1.0, 5.0 ),
UserName( "John Smith" ),
PreferredColor( red ),
PriceValue( Close ),
Weekdays( Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday );
If Visuals = True Then
DrawLine ( (High+Low)/2);
avgValue = Average( Close, Period );
Drawline( avgValue );
DrawLine( FactorOffSet * Close );
Print( "UserName" );
DrawLine( Close, "Close Line", StyleSolid, 2, PreferredColor );
DrawLine( PriceValue );
If Weekdays = "Monday" Then
Print( "Monday" );
If Weekdays = 0 Then
Print( "Monday" );
Variable Declaration
What are Variables?
Variables are data containers. To be able to use values like numbers, calculation results, user entries or prices, the values have to be saved temporarily in variables. This way, each value has its own storage space and can be called up by the variable name. Imagine variable to be like containers on a cargo ship. Each container has its own unique ID, with which to find the contents. When the booker needs to know the information in a container, he enters the unique ID into a computer, which returns the wanted information.
Validity of Variables
Variables are only valid within the script in a chart. It is not possible to use the value of variable "Period1" of the indicator "Indicator1" in "Indicator2", since the second script has no access to the variable - not even when both indicators are used in the same chart.
The only exceptions to the rule are
global variables.
Initialization of variables, and for how long do they keep their value?
Each Equilla script, e.g. an indicator, is processed once for each trading period. If the indicator is used in a daily chart, the indicator is calculated once for each day in the chart. The variable is initialized at the first trading day in the chart.
If in the days following that, no new value is set for the variable, the value is kept until the last trading day in the chart. If a new value is set for the variable in between, it is kept until a new value is assigned or until the last trading day.
Generating Variables
Variables are generated by being declared, i.e. given a name. Variables can only be defined within a certain code area. It has to be introduced with the tag
Variables:
You have the following possible methods to declare a variable:
Variables:
myFirstVariable, mySecondVariable;
Variables:
myFirstVariable( 10 ), mySecondVariable( myFirstVariable / 2 );
Variables:
myFirstString ( "" ), mySecondString( "String test" );
Variables:
myFirstColor( red ), mySecondColor( RGB (10, 20, 30) );
Global (Script-independent) Variables
As opposed to the statement above, you can use global variables under special circumstances. The limitation is that the scripts have to be in the same chart or portfolio. A sub chart, for example an indicator chart, does not count as a full chart. Therefore, you can pass data between main and sub charts and between several Equilla scripts that are used within the same main/sub charts.
To do so, global variables are available. A global variable consists of a "Namespace", which can be seen as the name of the cargo ship, and a variable name, which can be seen as the container ID. Since global variables are valid across several scripts, they do not have to be defined in the "Variables:" area. It is enough to name the global variable in one script and assign a value this way.
global::myFirstGlobalVar = 100;
Variables:
myNewVar;
myNewVar = global::myFirstGlobalVar ;
Print( myNewVar );
For variables as well as inputs, you can either enter several declarations in one line or each declaration in its own line. The declarations have to be separated by commas. After the last declaration, a semi-colon has to be entered.
Inputs:
MyName( "Caspar David" ), YourName( "Peter King" ), HerName ( "Barbara Myers" );
Inputs:
MyName( "Caspar David" ),
YourName( "Peter King" ),
HerName ( "Barbara Myers" );
Structuring Source Code
For a better readability and overview in the Equilla code, the source code should be well-structured. This also makes it easier to find errors in the coding. For structuring, make use of indentions, comments and empty lines. Declare the variables and inputs in separate lines instead of one line.
Indenting Source Code
It is a standard programming technique to indent certain parts of the code by using the
Tab key. This way it is much easier to understand the relations between code parts. This is especially useful in cases of loops or conditions.
If Bronto = "Saurus" Then
DrawText( Close, "Run away!" );
If condition = "met" Then
Begin
myFirstVar = 100;
mySecondVar = 7;
DrawLine( myFirstVar + mySecondVar );
End;
If condition = "met" Then
Begin
myFirstVar = 100;
mySecondVar = 7;
if Visuals = True Then
DrawLine( myFirstVar + mySecondVar );
End;
Bracketing Expressions
It is also recommended that you use bracketing for conditional queries or loops. This way, you avoid overly complicated code and malfunctioning which may result from logical errors.
If ( myFirstVar = 100 ) and ( mySecondVar = 100 ) Then
Begin
End;
If (myFirstVar = 100) AND ((mySecondVar = 100) OR (myThirdVar = 0)) Then
Begin
End;
Take a closer look at the second example. If the two brackets around the expressions linked by OR were not there, another meaning would result. The condition would be met if "myFirstVar = 100 and mySecondVar = 100" OR "myThirdVar = 0". In this case, the code would only be executed if "myFirstVar" were 100 and "mySecondVar" were 100 OR "myThirdVar" were 0, which was not meant to be according to the definition given in the comments.
In addition to the examples above, it may also be useful to set brackets around mathematical operators.
myFirstVar = 100 * ( x + y * ( 10 / z ) );
myFirstVar = 100 * ( x + ( y * ( 10 / z ) ) );
As already seen, you can enter comments in the code. This is especially useful for two reasons
- Even in case of longer source code and more complex structures, good readability and comprehensibility are given. This way, you can correct or edit the code even after years.
- Outsiders are able to understand the code much faster.
avgValue (Open)