Difference between revisions of "If"
Jump to navigation
Jump to search
(Created page with "Used in combination with Then to form a conditional statement that executes specific instructions if a logical expression is true, and with Else to form a conditional ...") |
|||
Line 1: | Line 1: | ||
Used in combination with [[Then]] to form a conditional statement that executes specific instructions if a logical expression is true, and with [[Else]] to form a conditional statement that executes specific instructions if a logical expression is false. | Used in combination with [[Then]] to form a conditional statement that executes specific instructions if a logical expression is true, and with [[Else]] to form a conditional statement that executes specific instructions if a logical expression is false. | ||
− | The conditional execution statement must contain both | + | The conditional execution statement must contain both '''If''' and [[Then]] - the use of [[Else]] is optional. |
− | + | == Usage == | |
− | |||
− | |||
− | |||
− | |||
<syntaxhighlight>If E Then I1 Else I2</syntaxhighlight> | <syntaxhighlight>If E Then I1 Else I2</syntaxhighlight> | ||
Line 13: | Line 9: | ||
<syntaxhighlight>If E Then Begin | <syntaxhighlight>If E Then Begin | ||
− | I1; | + | I1; |
− | I2; | + | I2; |
End | End | ||
Else Begin | Else Begin | ||
− | I3; | + | I3; |
− | I4; | + | I4; |
End;</syntaxhighlight> | End;</syntaxhighlight> | ||
− | Where: | + | Where: |
+ | |||
+ | :'''E''' - a true/false expression. | ||
+ | :'''I''' - conditional instructions. | ||
− | [[ | + | == Notes == |
+ | * [[Begin]] and [[End]] statements are used to group instructions for conditional execution; a [[Begin]] must always be followed by an [[End]]. | ||
+ | * [[Begin]] should not be followed by a semicolon (;). The code lines within an instruction group should end with a semicolon (;), and only the last instance of [[End]] within the same conditional execution statement should be followed by a semicolon (;). | ||
− | == | + | == Examples == |
− | If | + | If the condition ''UpTrend'' is false then sell: |
− | <syntaxhighlight>If UpTrend=False Then Sell Next Bar Market;</syntaxhighlight> | + | <syntaxhighlight>If UpTrend = False Then Sell Next Bar Market;</syntaxhighlight> |
− | If | + | If ''UpTrend'' is true then buy, otherwise sell short: |
− | <syntaxhighlight>If UpTrend Then Buy Next Bar Market Else SellShort Next Bar Market;</syntaxhighlight> | + | <syntaxhighlight>If UpTrend = True Then Buy Next Bar Market Else SellShort Next Bar Market;</syntaxhighlight> |
− | If | + | If ''UpTrend'' is true then buy, otherwise sell short: |
− | <syntaxhighlight>If UpTrend Then Begin | + | <syntaxhighlight>If UpTrend = True Then Begin |
− | Buy Next Bar Market; | + | Buy Next Bar Market; |
End | End | ||
Else Begin | Else Begin | ||
− | SellShort Next Bar Market; | + | SellShort Next Bar Market; |
End;</syntaxhighlight> | End;</syntaxhighlight> | ||
+ | Another example of using multiple [[Begin]] and [[End]] statements would be: | ||
+ | <syntaxhighlight> | ||
+ | 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"); | ||
+ | </syntaxhighlight> | ||
[[Category:Comparisons and Loops]] | [[Category:Comparisons and Loops]] |
Latest revision as of 10:53, 19 February 2012
Used in combination with Then to form a conditional statement that executes specific instructions if a logical expression is true, and with Else to form a conditional statement that executes specific instructions if a logical expression is false.
The conditional execution statement must contain both If and Then - the use of Else is optional.
Usage
If E Then I1 Else I2
or:
If E Then Begin
I1;
I2;
End
Else Begin
I3;
I4;
End;
Where:
- E - a true/false expression.
- I - conditional instructions.
Notes
- Begin and End statements are used to group instructions for conditional execution; a Begin must always be followed by an End.
- Begin should not be followed by a semicolon (;). The code lines within an instruction group should end with a semicolon (;), and only the last instance of End within the same conditional execution statement should be followed by a semicolon (;).
Examples
If the condition UpTrend is false then sell:
If UpTrend = False Then Sell Next Bar Market;
If UpTrend is true then buy, otherwise sell short:
If UpTrend = True Then Buy Next Bar Market Else SellShort Next Bar Market;
If UpTrend is true then buy, otherwise sell short:
If UpTrend = True Then Begin
Buy Next Bar Market;
End
Else Begin
SellShort Next Bar Market;
End;
Another example of using multiple Begin and End statements would be:
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");