分支(Branch)#

if-elseswitch 分支語法,讓程式可以決定執行方向: 當滿足某些條件時,執行某些指令,否則執行其他指令。

使用分支語法分成兩個步驟:

  1. 將要判斷的條件,利用 andor邏輯運算子(Logical Operator)組成邏輯陳述句(Logical Expression)。
  2. 邏輯陳述句搭配 if-elseswitch 語法,決定要執行的方向。

邏輯運算子(Logical Operator)#

PowerLanguage 提供了以下的邏輯運算子來組成邏輯陳述句:

邏輯運算子意義
and
or
not否定
>大於
<小於
>=大於等於
<=小於等於
=等於
<>不等於
cross above / cross over向上穿越,兩者可替換
cross below / cross under向下穿越,兩者可替換

除了 cross abovecross below 外,其他邏輯運算子的意義都和數學的邏輯意義相同。

A cross above B (A 向上穿越 B)的定義:

  • 在當根K棒,A 的值大於 B 的值,且在前一根K棒,A 的值小於 B 的值。
  • 在當根K棒,A 的值大於 B 的值,但前一根或多根K棒 A 的值等於 B 的值,則在這些等於的K棒之前,A 的值需小於 B 的值。

A cross below B (A 向下穿越 B)的定義:

  • 在當根K棒,A 的值小於 B 的值,且在前一根K棒,A 的值大於 B 的值。
  • 在當根K棒,A 的值小於 B 的值,但前一根或多根K棒 A 的值等於 B 的值,則在這些等於的K棒之前,A 的值需大於 B 的值。

邏輯陳述句(Logical Expression)#

邏輯陳述句是回傳值為真假值型別的陳述句。範例:

  • 5 < 3: 5 小於 3,回傳值為 false
  • 8 <> 9: 8 不等於 9,回傳值為 true
  • 8 <> 9 and 5 < 3: 8 不等於 9 5 小於 3,回傳值為 false(因為 5 < 3false)。

if-else#

if-else 讓程式可以根據邏輯陳述句的結果,選擇要執行哪一條路。如果邏輯陳述句的結果為 true,執行 True_Instruction; 如果是 false,執行 False_Instruction

if-else 分成單行指令語法多行指令語法

單行指令語法#

True_InstructionFalse_Instruction 只能有一行,不用加 begin/end

[] 表示可省略,else 條件如果不需要,可以刪除。

1
2
If Logical_Expression Then True_Instruction
[Else False_Instruction];

多行指令語法#

True_InstructionFalse_Instruction 可以是一行也可以是多行,必須用 begin/end 包起來。

[] 表示可省略,else 條件如果不需要,可以刪除。

最後一個 end 需要加分號(;),其餘的 end 不需要加分號。

 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 語法#

if-else 除了決定 truefalse 的兩路語法,還可以搭配 else if 判斷多個邏輯陳述句,實現多路分支。

[] 表示可省略,else 條件如果不需要,可以刪除。

最後一個 end 需要加分號(;),其餘的 end 不需要加分號。

 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];

範例#

單行語法: 如果 UpTrendFalse,在下一根K棒市價單賣出。

1
If UpTrend = False Then Sell Next Bar Market;

多行語法: 如果 UpTrendTrue,在下一根K棒市價單買進; 否則在下一根K棒市價單賣空。

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 語法: 宣告變數 EMA 計算 20 日指數移動平均線,根據收盤價與 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 透過運算式(Expression)的回傳值來決定要執行的指令,運算式的回傳值可以是數值、字串或真假值型別。

語法#

當程式執行到 switch 時,會執行以下動作:

  1. Expression 的回傳值和每一個 Case_Expression 比對,如果滿足條件,則執行對應的 Case_Instructions
  2. 如果所有的 Case_Expression 都沒有滿足,則執行 defaultDefault_Instructions

[] 表示可省略,default 如果不需要,可以刪除。

PowerLanguage 與其他程式語言不同,不需要額外加 break 來跳出 switch。程式在執行完 Case_Instructions 後,會自動跳到 end; 結束 switch

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

Case_Expression#

Case_Expression型別必須和 Expression 回傳值的型別一致。

Case_Expression 可以是:

用法範例
一個值case 1:
多個值case 1, 2, 3:
一個範圍case 4 to 8:
搭配邏輯運算子case is > 40: (is 是 skip word,可省略)
混合使用case 9 to 15, 20:

範例#

數值型別: var1 的值為 8(5+3),滿足 Case 4 to 8,因此執行 Print("if 4 to 8")

 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;

字串型別: str1 的值為 "I",滿足 Case "H", is >= "I",因此執行 Print("if H, is >= I")

 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.