Script Framework
Startting from a simple script MovAvg2Line Cross LE to quickly understand the basic framework of the script: Declaration, Rules Calculation, Entry and Exit Condition and Buy or Sell.
The MovAvg2Line Cross LE is a signal script of a two moving average golden cross buying strategy provided by PowerLanguage Editor.
Open PowerLanguage Editor->File->Open->Study Type->Signal (checked)->MovAvg2Line Cross LE.
Source code of MovAvg2Line Cross LE:
inputs: Price( Close ), FastLength( 9 ), SlowLength( 18 ) ;
variables: var0( 0 ), var1( 0 ) ;
var0 = AverageFC( Price, FastLength ) ;
var1 = AverageFC( Price, SlowLength ) ;
condition1 = CurrentBar > 1 and var0 crosses over var1 ;
if condition1 then
Buy ( "MA2CrossLE" ) next bar at market ;
I. Declaration
Input and Variable
Input and variable can be thought as a container (Ex: bottle) that stores the desired value (Ex: 5 liters).
But before using this container, you need to declare it first. The declaration tells the compiler the type of the value to be stored and compiler, according to the type, allocates the required memory size.
Therefore, the using of input and variables can be divided into two phases:
The 1st phase – declaration: Declare the name of input or variable, give default values or initial values respectively which defines Type. Type can be Numerical, String or TrueFalse.
The 2nd phase – using: Use the name of the input or variable in the code. When the program executes to the input or variable name, it uses the stored value.
Input declaration syntax:
inputs: InputName1(DefaultValue1), InputName2(DefaultValue2), etc;
inputs
: declare input.InputName
: The name of the input. The name is not case-sensitive and cannot start with a number or a period.DefaultValue
: The default value of the input. The default value determines the Type. The Type can be Numerical, String or TrueFalse.
Note: A semicolon should be added at the end of the input declaration. If it is not added, a compilation error will occur.
You can set the default value of the input
of the signal script or indicator script from the MultiCharts.
If no new default value is set from MultiCharts, the DefaultValue
defined in the script will be set.
The new default value set form MultiCharts can be applied directly by executing it without recompilling it.
The input value can only be set and changed through the default value. After setting, the input value cannot be changed through the code in the script.
MovAvg2Line Cross LE, line 1:
inputs: Price( Close ), FastLength( 9 ), SlowLength( 18 ) ;
This line means:
Declare the inputs
respectively called , Price
which the default value is Close
(closing price), FastLength
which the default value is 9
and SlowLength
which the default value is 18
.
Variable declaration syntax:
variables: VariableName1(InitialValue1), VariableName2(InitialValue2),etc;
variables
: declare variable.VariableName
– The name of the variable. The name can be composed of letters, underscores, numbers, and periods. The name is not case-sensitive in English. The name cannot start with a number or a period.InitialValue
– The initial value of the variable. The initial value determines the Type. The Type can be Numeric, String or TrueFalse.
Note: A semicolon should be added at the end of the variable declaration. If it is not added, a compilation error will occur.
You can not set the initial value of variable from MultiCharts similar to the input does.
The variable value can be set through the initial value and also can be changed through the code in the script (assigned other values).
See Input and Variable.
MovAvg2Line Cross LE, line 2:
variables: var0( 0 ), var1( 0 ) ;
This line means:
Declare variables
respectively called, var0
and give the initial value of 0
, var1
and give the initial value of 0
.
Reserved Word
Reserved Word are words in PowerLanguage that have special meaning or are themselves syntactical words, they cannot be used as input or variable names.
The inputs
and variables
themselves are syntactical words, so they are reserved words.
In MovAvg2Line Cross LE, in the first line, the Close
that appears in the input declaration is also a reserved word, which means the closing price. Other similar reserved words include Open
(opening price), High
(highest price), Low
(lowest price).
II. Rules Calculation
Function
Function being similar to input and variable can be thought of as a container but stores a sequence of code instead of value. When we want to reuse these code, we don’t need to rewrite it all once, only the function name needs to be used and when the program executes the function name, it will know to execute the corresponding code.
Usually, the commonly used functionalities are written as function, so that there is no need to rewrite the same calculation or functionality every time, just use the corresponding function name.
Like input and variable, function are divided into two phases:
The first phase is called definition (implementation): the code that you want to use as a function is written in the function script and compiled.
The second phase is use (call): use the function name and provide the input value required by function, when the program executes the function name, it will substitute the provided input value into and execute the corresponding function code.
Since this article is a brief introduction, only the use (call) of the function will be explained first. For the definition (implementation) of the function, see Function.
Function use (call) syntax:
FunctionName(input1, input2, ...)
whereFunctionName
: The function name to use.input
: The value given to the function input.
PowerLanguage provides many built-in functions for common functionalities or calculations.AverageFC(PriceValue, Len)
is one of the built-in functions provided by PowerLanguage. It needs two inputs: PriceValue
and Len
and calculates the average value of PriceValue
over Len
bars.
MovAvg2Line Cross LE, line 4, line 5:
var0 = AverageFC( Price, FastLength ) ;
var1 = AverageFC( Price, SlowLength ) ;
AverageFC( Price, FastLength )
is:
Calculate the average value of Price
over FastLength
bars.
AverageFC( Price, SlowLength )
is:
Calculate the average value of Price
over SlowLength
bars.
The three words Price
, FastLength
and SlowLength
are the inputs declared in the first code line:
inputs: Price( Close ), FastLength( 9 ), SlowLength( 18 );
If you substitute the default values of Price
, FastLength
and SlowLength
,
Obtain AverageFC( Close, 9)
which means:
Calculate the average value of the closing prices over 9 bars.
Obtain AverageFC( Close, 18 )
which means:
Calculate the average value of the closing prices over 18 bars.
Since the script is calculated once for each bar as stated in the chapter: How MultiCharts executes the strategy, the calculation for each bar of average value of the closing prices over 9 bars each bar is also the moving average over 9 bars (9MA).
Therefore, the AverageFC( PriceValue, Len)
function, if the default value of PriceValue
is Close
, is actually calculating the moving average over Len
bars.
See Function.
Assignment
In PowerLanguage, the equal sign (=
) has two meanings: assignment or equal (that is, mathematical equality).
It means mathematical equality only when it appears with reserved words for conditional expression(Ex: if
, else if
, while
, etc.).
Otherwise, it means assignment that the calculation starts from the right side of the equal sign (=
), and then the reslult of the calculation is assigned to the left side.
Here is a simple example:
variables: BottleA(5), BottleB(3), BottleC(5);
BottleA=BottleB+BottleC;
The first line declares the variables.
The second line: BottleA=BottleB+BottleC;
If from a mathematical point of view it is strange, the value of BottleA
is 5, and the value of BottleB
(3) plus BottleC
(5) is 8, how can 5 be equal to 8?
Because the equal sign (=) here does not mean “mathematical equality”, but “assignment”, it is necessary to start the calculation from the right side of the equal sign (=
) and then assign the result of calcuation to the left.
Hence this sample code is:
Add BottleB
(5) and BottleC
(3), and then assign the result to BottleA
, the original value(5) of BottleA
is overwritten as the value of 8.
MovAvg2Line Cross LE, line 4, line 5:
var0 = AverageFC( Price, FastLength ) ;
var1 = AverageFC( Price, SlowLength ) ;
Substitute the default value of Price
as Close
. The meaning of these two lines is:
Assign the value of AverageFC( Close, FastLength )
to var0
Assign the value of AverageFC( Close, SlowLength )
to var1
From the previous section conclusion, the AverageFC( Close, Len)
is actually calculating the moving average over Len
bar.
Hence, var0
is the moving average over FastLength
bar, var1
is the moving average over SlowLength
bar.
The var0
and var1
are two moving averages with different lengths (short-term and longer-term).
III. Entry and Exit Condition
Branch
PowerLanguage provides an if
branch syntax to determine the direction of program execution. if certain conditions are met, the specific instructions are executed, otherwise other instructions are executed.
There are two steps here:
The first step is to use Logical Operators (and
, or
, etc) to form a Logical Expression of the conditions to be judged (satisfied).
The second step is to use the if
branch syntax with Logical Expression to determine the direction to be executed.
Logical Operator
and
: mathematical logic and, when both sides are true, return true
, otherwise return false
.or
: mathematical logic or, as long as one of the sides is true, return true
, otherwise return false
.
Logical Expression
A logical expression is an expression whose return value is true
or false
.
E.g:5<3
: It is a logical expression, 5 is less than 3, and the value is false
. 8<>9
: It is a logical expression, 8 is not equal to 9, the value is true
.8<>9
and 5<3
: It is a logical expression, 8 is not equal to 9 and 5 is less than 3, the value is false
.
The first step: Use logical operators to form a logical expression
MovAvg2Line Cross LE, line 7:
condition1 = CurrentBar > 1 and var0 crosses over var1 ;
The equal sign (=
) here means assignment, so the right side of the equal sign (=
) is evaluated first.CurrentBar > 1 and var0 crosses over var1
is logical expression.
CurrentBar
is a reserved word which returns the number of the current bar. The number of the bar counts from the first bar after number of the Maximum Bars Back.
Example: If Maximum Bars Back is set to 20, the 21st bar will be number 1.
For a detailed description of Maximum Bars Back, see section .
Currently, you can regard CurrentBar > 1
as true
and it does not affect the understanding of this strategy.
crosses over
is a reserved word, which means that one line crosses upwards the other.var0
and var1
known from the previous section represent two moving averages over fast length and slow length respectively.
Therefor, regarding CurrentBar > 1
as true
, the right side of the equal sign (=
) can be:var0
(fast length moving average) crosses va1
(slow length moving average) up
condition1
is a reserved word which is a built-in variable and because it is the built-in variable, it can be used directly without declaring it.
Therefore, the code of line 7 can be understood as:
Assigns the result of that var0
(fast length moving average) crosses
va1
(slow length moving average) up to condition1
Note: condition1
is a built-in variable. It is not recommended to use built-in variables because the names are not clear and meaningful. It is recommended to use meaningful variable names.
The second step: Use the if
branch syntax with logical expression
PowerLanguage provides an if
branch syntax, which allows to decide the direction to be executed according to the result of the logical expression.
if
syntax:
If E Then I1;
Where:E
– logical expression.I1
– instruction that will be executed when the logical expression is true.
MovAvg2Line Cross LE, line 8, line 9:
if condition1 then
Buy ( "MA2CrossLE" ) next bar at market ;
These two sentences are:
If condition1
is true
, then execute instruction Buy(“MA2CrossLE”) next bar at market;
From the previous section, condition1
the result of that var0
(fast length moving average) crosses va1
(slow length moving average) upwards.
So, obtain:
If var0
(fast length moving average) crosses va1
(slow length moving average) up is true
, then execute Buy(“MA2CrossLE”) next bar at market;
.
See Branch.
IV. Buy or Sell
Buy and sell instructions
PowerLanguage provides Buy
, Sell
, SellShort
and BuyToCover
to place orders.
Buy
Syntax
Buy[("EntryLabel")] [TradeSize] EntryType;
Where:EntryLabel
– Name the entry. The entry name will be displayed on the chart. When selling, you can use the entry name to specify which entry to sell.
If not specified, the preset name will be “Buy” for the first entry, “Buy#2” for the second entry, “Buy#3” for the third entry, and so on.
Square brackets represent optional parameters.TradeSize
– Specify the number of shares (contracts).
If not specified, the value setting in the Properties tab of the Strategy Properties is used.
Square brackets represent optional parameters.EntryType
– Specify the type of order placed.
Note: After the buy order is sent out, if the order is not filled at the end of the bar, the order will be cancelled.
MovAvg2Line Cross LE, line 9:
Buy ( "MA2CrossLE" ) next bar at market ;
next
is a reserved word, used in conjunction with bar
, indicating the next bar.at
is a reserved word, it simply increases the readability of the code and can be omitted.market
is a reserved word, representing a market order.
So this line means:Buy
at the market
price on the next bar
and give this buy entry a name called “MA2CrossLE
“.
See buy and sell.
Conclusion
This chapter using the script MovAvg2Line Cross LE as a sample briefly introduces the framework of the script: declaration, calculation rules, condition of entry and exit and buy or sell.
Reference
https://www.multicharts.com/trading-software/index.php/Using_Studies_(PowerLanguage_Editor)
https://www.multicharts.com/trading-software/index.php?title=Category:Declaration
https://www.multicharts.com/trading-software/index.php?title=Category:Comparisons_and_Loops
https://www.multicharts.com/trading-software/index.php?title=Category:Strategy_Orders
https://www.multicharts.com/trading-software/index.php?title=Input
https://www.multicharts.com/trading-software/index.php?title=Variable
https://www.multicharts.com/trading-software/index.php?title=If
https://www.multicharts.com/trading-software/index.php?title=Buy