Branch#
if-else and switch branch syntax allows the program to decide the direction of execution: when certain conditions are met, execute certain instructions, otherwise execute other instructions.
Using branch syntax involves two steps:
- Use
and,or, etc. logical operators (Logical Operator) to compose a logical expression (Logical Expression) for the conditions to be evaluated. - Combine the logical expression with if-else or switch syntax to determine the direction of execution.
Logical Operator#
PowerLanguage provides the following logical operators to compose logical expressions:
| Logical Operator | Meaning |
|---|---|
and | And |
or | Or |
not | Not |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
= | Equal to |
<> | Not equal to |
cross above / cross over | Cross above, interchangeable |
cross below / cross under | Cross below, interchangeable |
Except for cross above and cross below, other logical operators have the same meaning as in mathematical logic.
A cross above B (A crosses above B) is defined as:
- 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 equal bars, the value of A must be less than the value of B.
A cross below B (A crosses below B) is defined as:
- 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 equal bars, the value of A must be greater than the value of B.
Logical Expression#
Logical expression is a statement whose return value is a TrueFalse type. Examples:
5 < 3: 5 is less than 3, returnsfalse.8 <> 9: 8 is not equal to 9, returnstrue.8 <> 9 and 5 < 3: 8 is not equal to 9 and 5 is less than 3, returnsfalse(because5 < 3isfalse).
if-else#
if-else allows the program to choose which path to execute based on the result of the logical expression. If the result of the logical expression is true, execute True_Instruction; if it is false, execute False_Instruction.
if-else is divided into single-line instruction syntax and multi-line instruction syntax.
Single-Line Instruction Syntax#
True_Instruction or False_Instruction can only have one line, no need to add begin/end.
[] indicates optional, the else condition can be removed if not needed.
| |
Multi-Line Instruction Syntax#
True_Instruction or False_Instruction can be one line or multiple lines, and must be wrapped with begin/end.
[] indicates optional, the else condition can be removed if not needed.
The last
endrequires a semicolon (;), the rest of theenddo not need a semicolon.
| |
else if Syntax#
In addition to the two-way true or false syntax, if-else can also be combined with else if to evaluate multiple logical expressions, achieving multi-way branching.
[] indicates optional, the else condition can be removed if not needed.
The last
endrequires a semicolon (;), the rest of theenddo not need a semicolon.
| |
Example#
Single-line syntax: If UpTrend is False, sell at market on the next bar.
| |
Multi-line syntax: If UpTrend is True, buy at market on the next bar; otherwise, sell short at market on the next bar.
| |
else if syntax: Declare Variable EMA to calculate the 20-period exponential moving average, and print different messages based on the relationship between the closing price and EMA.
| |
switch#
switch determines the instruction to be executed based on the return value of the Expression. The return value of the Expression can be Numerical, String, or TrueFalse type.
Syntax#
When the program executes to switch, the following actions are performed:
- Compare the return value of
Expressionwith eachCase_Expression. If the condition is met, execute the correspondingCase_Instructions. - If none of the
Case_Expressionsare met, executedefault’sDefault_Instructions.
[] indicates optional, the default can be removed if not needed.
Unlike other programming languages, PowerLanguage does not require adding a
breakto exit theswitch. After executingCase_Instructions, the program automatically jumps toend;to end theswitch.
| |
Case_Expression#
The type of Case_Expression must match the type of the return value of the Expression.
Case_Expression can be:
| Usage | Example |
|---|---|
| A single value | case 1: |
| Multiple values | case 1, 2, 3: |
| A range | case 4 to 8: |
| With logical operators | case is > 40: (is is a skip word, can be omitted) |
| Mixed usage | case 9 to 15, 20: |
Example#
Numerical type: The value of var1 is 8 (5+3), which satisfies Case 4 to 8, so Print("if 4 to 8") is executed.
| |
String type: The value of str1 is "I", which satisfies Case "H", is >= "I", so Print("if H, is >= I") is executed.
| |
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