Branch
The if-else and switch branch syntax allows the program to decide the direction of execution. If specific conditions are met, specified instructions are executed, otherwise other instructions are executed.
There are two steps here:
The first step: Use and, or, etc. logical operators compose a logical expression for the conditions to be satisfied.
The second step: Combine the logical expression with the if-else and switch syntax to determine the direction to be executed.
Logical Operator
Following is logical operators provided by PowerLanguage to form logical statements.
邏輯運算子 | 意義 |
and | and |
or | or |
not | not |
> | greater than |
< | less then |
>= | greater equal or equal to |
<= | less then or equal to |
= | equal to |
<> | not equal to |
cross above/ | cross above same as cross over , interchangeable |
cross below/cross under | cross below same as cross under , interchangeable |
Except for cross above and cross below, other logical operators have the same meaning as mathematical logic.
A cross above
B is defined as follows:
- At the current bar, the value of A is greater than the value of B and at the previous bar, the value of A is less than the value of B.
- At the current bar, the value of A is greater than the value of B but at the previous one or more bars, the value of A is equal to the value of B, then before these equals, the value of A is less than the value of B.
A cross below
B is defined as follows:
- At the current bar, the value of A is less than the value of B and at the previous bar, the value of A is greater than the value of B.
- At the current bar, the value of A is less than the value of B but at the previous one or more bars, the value of A is equal to the value of B, then before these equals, the value of A is greater than the value of B.
Logical Expression
A logical statement is a statement that returns a TrueFalse
value.
E.g:5<3
: It is a logical statement, 5 is less than 3, which returns false
.8<>9
: It is a logical statement, 8 is not equal to 9, which returns true
.8<>9 and 5<3
: It is a logical statement, 8 is not equal to 9 and 5 is less than 3, which returns false
.
if-else
if-else
allows the program to choose which way to execute based on the result of the logical expression.
If the result of the logical expression is true
, execute the True_Instruction
, and if it is false
, execute False_Instruction
.
There are two kinds of if-else syntax: single-line instruction syntax and multi-line instruction syntax.
Single-line instruction syntax
True_Instruction
or False_Instruction
can only have one line and no need begin/end
.[]
indicates the else
instructions can be deleted if it is not needed.
if Logical_Expression then True_Instruction
[else False_Instruction];
Multi-line instruction syntax
True_Instruction
or False_Instruction
can be one line or multiple lines and they must be wrapped with begin/end
.[]
indicates the else
instructions can be deleted if it is not needed.
The last end
requires a semicolon, and the rest of the end
do not need a semicolon.
if Logical_Expression then begin
True_Instruction_1;
True_Instruction_2;
...
end
[else begin
False_Instruction_1;
False_Instruction_2;
...
end];
else if syntax
In addition to the two-way usage, if-else
can also be used in combination with else if
as multi-way usage to judge multiple logical statements and decide which way to execute.[]
indicates the else
instructions can be deleted if it is not needed.
The last end
requires a semicolon, and the rest of the end
do not need a semicolon.
if Logical_Expression_1 then begin
True_Instruction_1;
...
end
else if Logical_Expression_2 then begin
True_Instruction_2;
...
end
.
.
.
else if Logical_Expression_N then begin
True_Instruction_N;
...
end
[else begin
Instruction_For_All_False;
...
end];
Example
If UpTrend = False Then Sell Next Bar Market;
If UpTrend = True Then Begin
Buy Next Bar Market;
End
Else Begin
SellShort Next Bar Market;
End;
Variables:
EMA(0);
EMA = XAverage(Close, 20);
if Close > EMA then begin
Print("Close is above the EMA");
end else if (Close < EMA) then begin
Print("Close is below the EMA");
end else
Print("Close is equal to the EMA");
switch
The switch
determines the instruction to be executed by the return value of the Expression
which ca be Numerical
, String
or TrueFalse
type.
switch syntax
When the program executes to switch
, the following actions are performed:
- Compare the returned value of Expression with each
Case_Expression
, and executeCase_Instructions
if the conditions ofCase_Expression
are met. - If all
Case_Expressions
do not meet, the program executes theDefault_Instructions
.[]
indicates thedefault
instructions can be deleted if it is not needed.
The PowerLanguage, unlike other programming languages, there is no need to add a break
to jump to the end;
.
The program jumps to the end;
of switch
after finish execution of the Case_Instructions
.
switch ( Expression )
begin
case Case_Expression: Case_Instructions;
[default: Default_Instructions;]
end;
Case_Expression
The type of Case_Expression
must be as same as the type of the returned value of the Expression
.
Case_Expression
can be a single value, the multiple values, a range, or used with logical operators.
Example
The value of var1
is 8, which satisfies Case 4 to 8
, so Print("if 4 to 8");
is executed.
var: var1(5+3);
Switch(var1)
Begin
Case 1, 2, 3: //the multiple values: 1, 2, 3
Print("if 1, 2, 3");
Case 4 to 8: //a range: 4 to 8
Print("if 4 to 8");
Case 9 to 15, 20: //9 to 15 or 20
Print("if 9 to 10, 20");
Case is > 40: //greater than 40,is is a skip word,can be omitted
Print("if is > 40");
Default:
Print("if default");
End;
The value of str1
is "I"
, which satisfies Case "H", is >= "I"
, so print("if H, is >= I");
is executed.
var: str1("I");
Switch(str1)
Begin
Case "A", "B", "C": //the multiple values: A, B, C
Print("if A or B or C");
Case "D" to "G": //a range: D to G
Print("if D to G");
Case "H", is >= "I": //H or greater then I
Print("if H, is >= I");
Default:
Print("if default");
End;
Reference
https://www.multicharts.com/trading-software/index.php?title=Category:Comparisons_and_Loops
https://www.multicharts.com/trading-software/index.php?title=Above
https://www.multicharts.com/trading-software/index.php?title=Below
https://www.multicharts.com/trading-software/index.php?title=If
https://www.multicharts.com/trading-software/index.php?title=Switch