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:

  1. Use and, or, etc. logical operators (Logical Operator) to compose a logical expression (Logical Expression) for the conditions to be evaluated.
  2. 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 OperatorMeaning
andAnd
orOr
notNot
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
=Equal to
<>Not equal to
cross above / cross overCross above, interchangeable
cross below / cross underCross 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, returns false.
  • 8 <> 9: 8 is not equal to 9, returns true.
  • 8 <> 9 and 5 < 3: 8 is not equal to 9 and 5 is less than 3, returns false (because 5 < 3 is false).

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.

1
2
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 must be wrapped with begin/end.

[] indicates optional, the else condition can be removed if not needed.

The last end requires a semicolon (;), the rest of the end do not need a semicolon.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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 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 end requires a semicolon (;), the rest of the end do not need a semicolon.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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#

Single-line syntax: If UpTrend is False, sell at market on the next bar.

1
If UpTrend = False Then Sell Next Bar Market;

Multi-line syntax: If UpTrend is True, buy at market on the next bar; otherwise, sell short at market on the next bar.

1
2
3
4
5
6
If UpTrend = True Then Begin
  Buy Next Bar Market;
End
Else Begin
  SellShort Next Bar Market;
End;

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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#

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:

  1. Compare the return value of Expression with each Case_Expression. If the condition is met, execute the corresponding Case_Instructions.
  2. If none of the Case_Expressions are met, execute default’s Default_Instructions.

[] indicates optional, the default can be removed if not needed.

Unlike other programming languages, PowerLanguage does not require adding a break to exit the switch. After executing Case_Instructions, the program automatically jumps to end; to end the switch.

1
2
3
4
5
Switch (Expression)
Begin
  Case Case_Expression: Case_Instructions;
  [Default: Default_Instructions;]
End;

Case_Expression#

The type of Case_Expression must match the type of the return value of the Expression.

Case_Expression can be:

UsageExample
A single valuecase 1:
Multiple valuescase 1, 2, 3:
A rangecase 4 to 8:
With logical operatorscase is > 40: (is is a skip word, can be omitted)
Mixed usagecase 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Variables: var1(5 + 3);
Switch(var1)
Begin
  Case 1, 2, 3:
    Print("if 1, 2, 3");

  Case 4 to 8:
    Print("if 4 to 8");

  Case 9 to 15, 20:
    Print("if 9 to 15, 20");

  Case is > 40:
    Print("if is > 40");

  Default:
    Print("if default");
End;

String type: The value of str1 is "I", which satisfies Case "H", is >= "I", so Print("if H, is >= I") is executed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Variables: str1("I");
Switch(str1)
Begin
  Case "A", "B", "C":
    Print("if A or B or C");

  Case "D" to "G":
    Print("if D to G");

  Case "H", is >= "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

© 2026 CodeReindeer. All rights reserved.