Summary

In MQL4, control structures govern the flow of program code, particularly through decisions made using if statements. The primary types of control structures are the if statement, the else statement, and the switch statement.

If statements evaluate conditions and execute code when the condition is met. They can be combined with else statements to define alternative actions if the condition is not satisfied.

Else-if statements allow for the evaluation of multiple conditions and the execution of different actions depending on which condition is met.

Switch statements provide an alternative way to check different conditions and respond to them by branching the program flow.

Top 3 Key Takeaways

  1. Importance of the if Statement: The if statement is the central control structure in MQL4 for making decisions and controlling programmatic responses based on conditions.
  2. Combined Conditions: Else-if statements allow for elegant evaluation of multiple conditions, making the code more flexible and easier to read.
  3. Use of Switch Statements: The switch statement offers a structured method for selecting between various alternatives and controlling program flow, which is useful in more complex decision-making scenarios.
Text written in chalk on a blackboard Control structures in MQL4

The if Statement

The if statement is used in MQL4 to check a condition and execute a predetermined section of program code in response to that condition. To better understand, an if statement can be colloquially described as "if this happens - then do the following."

For example, a requirement can be described as follows: If the closing price is greater than 100, then set the stop-loss at 90. The following code example translates this sentence into the MQL4 programming language.

Example

MQL Code

double closingPrice = Close; double targetPrice = 100; double stopLossPrice = 90; if(closingPrice > targetPrice){ ChangeStopLoss(stopLossPrice); }

The variable "closingPrice" is compared to the value of the variable "targetPrice." If the check is true, the code within the curly braces is executed. In this example, a fictional function is called to adjust the stop-loss price.

Combined if Statements

In the previous example, the code within the if statement is executed when the condition of the expression is met. If the condition is not met, no specific code is executed, and the program continues its flow. If you want to execute code in any case, even if the condition is not met, this can be achieved by extending the if statement. An alternative is provided to the if statement using the "else statement." To better understand, an if-else statement can be colloquially described as "if this happens - then do the following - otherwise do that."

For example, a requirement can be described as follows: If the closing price is greater than 100, then set the stop-loss at 90. Otherwise, set the stop at 80. The following code example translates this sentence into the MQL4 programming language.

Example

MQL Code

double closingPrice = Close; double targetPrice = 100; double stopLossPrice = 90; double alternativeStop = 80; if(closingPrice > targetPrice){ ChangeStopLoss(stopLossPrice); } else{ ChangeStopLoss(alternativeStop); }

The variable "closingPrice" is compared to the value of the variable "targetPrice." If the check is true, the code within the curly braces is executed. Otherwise, the code within the curly braces after the else statement is executed. In this example, a fictional function is called to adjust the stop-loss price.

Combined else-if Statements

The conditions to be checked can be further expanded by using the else-if statement. To better understand, an if-else-if statement can be colloquially described as "if this happens - then do the following - if something else happens - then do something else - otherwise do that."

For example, a requirement can be described as follows: If the RSI indicator has a value of 90, then open a short position. If the RSI indicator has a value of 10, then open a long position. If the RSI indicator has a value of 80, then close a long position. If the RSI indicator has a value of 20, then close a short position. Otherwise, inform the user that no action has been taken. The following code example translates these requirements into the MQL4 programming language.

Example

MQL Code

double indicatorValue = iRSI(Symbol(),0,14,PRICE_CLOSE,0); if(indicatorValue == 90){ OpenShort(); } else if(indicatorValue == 10){ OpenLong(); } else if(indicatorValue == 80){ CloseLong(); } else if(indicatorValue == 20){ CloseShort(); } else{ Print("No action was taken"); }

The variable "indicatorValue" is compared to various conditions. If one of the checks is true, the respective code within the curly braces is executed. In this example, fictional functions are called that either open or close a position.

The switch Statement

The switch statement is also used to decide between different alternatives and thereby branch the program flow.

Let's first consider the structure of the switch statement.

Syntax

MQL Code

switch(expression) { case constant: statement; break; case constant: statement; break; default: statement; }

The switch statement checks whether the constant matches the expression through the keyword "case." If so, the statement is executed, and the keyword "break" terminates the switch. If the condition in the first case is not met, the next case is checked using the same principle. The switch ends with the keyword "default." The statement belonging to "default" is executed if no match has been found previously.

Now, a practical example of a switch statement. The same scenario as in the else-if statement from the previous example is assumed.

Example of a switch Statement

MQL Code

double indicatorValue = iRSI(Symbol(),0,14,PRICE_CLOSE,0); switch(indicatorValue){ case 90: OpenShort(); break; case 10: OpenLong(); break; case 80: CloseLong(); break; case 20: CloseShort(); break; default: Print("No action was taken"); }

The first case checks if the indicator value equals 90 and executes the corresponding statement if this is the case. Otherwise, the next case is checked, and the statement is executed if that condition is met, and so on. If none of the conditions are met, the default case is executed.

Questions and Answers

What is the main purpose of control structures in MQL4?

Control structures in MQL4 are used to control the flow of program code based on decisions. They allow for the evaluation of conditions and the execution of corresponding code to achieve flexible programming functions.

How does the if statement differ from the switch statement in MQL4?

The if statement is used to evaluate one or more conditions and respond accordingly. In contrast, the switch statement is used to evaluate a variable against different values, allowing the program flow to branch based on specific conditions, which is useful when checking multiple specific cases.

Can I nest multiple if statements?

Yes, in MQL4, it is possible to nest if statements to evaluate complex conditions. This allows for more detailed control over the program flow but requires careful structuring to ensure the readability of the code.